rn-shiki 0.0.31 → 0.0.33
Sign up to get free protection for your applications and to get access to all the features.
package/package.json
CHANGED
@@ -1,31 +1,19 @@
|
|
1
|
-
import type {
|
1
|
+
import type { SyntaxHighlighterProps } from 'src/types/shiki'
|
2
2
|
import React from 'react'
|
3
3
|
import { Text, View } from 'react-native'
|
4
|
-
import typescript from 'shiki/langs/typescript.mjs'
|
5
|
-
import githubDark from 'shiki/themes/github-dark.mjs'
|
6
4
|
import { useSyntaxHighlighter } from '../../hooks/useSyntaxHighlighter'
|
7
5
|
|
8
|
-
|
9
|
-
text: string
|
10
|
-
lang?: LanguageInput
|
11
|
-
langName?: string
|
12
|
-
theme?: ThemeInput
|
13
|
-
themeName?: string
|
14
|
-
fontSize?: number
|
15
|
-
}
|
16
|
-
|
17
|
-
function SyntaxHighlighter({ text, lang = typescript, langName = 'typescript', theme = githubDark, themeName = 'github-dark', fontSize }: SyntaxHighlighterProps) {
|
6
|
+
function SyntaxHighlighter({ text, language, languageId, theme }: SyntaxHighlighterProps) {
|
18
7
|
const { tokens, error, isLoading } = useSyntaxHighlighter({
|
19
8
|
text,
|
20
|
-
|
21
|
-
|
9
|
+
language,
|
10
|
+
languageId,
|
22
11
|
theme,
|
23
|
-
themeName,
|
24
12
|
})
|
25
13
|
|
26
14
|
if (error) {
|
27
15
|
return (
|
28
|
-
<Text style={{ color: '#ff6b6b', fontFamily: 'monospace'
|
16
|
+
<Text style={{ color: '#ff6b6b', fontFamily: 'monospace' }}>
|
29
17
|
Error:
|
30
18
|
{error}
|
31
19
|
</Text>
|
@@ -33,7 +21,7 @@ function SyntaxHighlighter({ text, lang = typescript, langName = 'typescript', t
|
|
33
21
|
}
|
34
22
|
|
35
23
|
if (isLoading) {
|
36
|
-
return <Text style={{ color: '#
|
24
|
+
return <Text style={{ color: '#666666', fontFamily: 'monospace' }}>Loading...</Text>
|
37
25
|
}
|
38
26
|
|
39
27
|
return (
|
@@ -44,16 +32,21 @@ function SyntaxHighlighter({ text, lang = typescript, langName = 'typescript', t
|
|
44
32
|
<Text
|
45
33
|
key={`${lineIndex}-${tokenIndex}`}
|
46
34
|
style={{
|
47
|
-
color: token.color || '#
|
35
|
+
color: token.color || '#000000',
|
48
36
|
fontFamily: 'monospace',
|
49
|
-
fontSize,
|
50
37
|
fontStyle: token.fontStyle as unknown as 'normal' | 'italic',
|
51
38
|
}}
|
52
39
|
>
|
53
40
|
{token.content.replace(/ /g, '\u00A0')}
|
54
41
|
</Text>
|
55
42
|
))}
|
56
|
-
<Text
|
43
|
+
<Text
|
44
|
+
style={{
|
45
|
+
fontFamily: 'monospace',
|
46
|
+
}}
|
47
|
+
>
|
48
|
+
{'\n'}
|
49
|
+
</Text>
|
57
50
|
</View>
|
58
51
|
))}
|
59
52
|
</View>
|
@@ -1,8 +1,8 @@
|
|
1
|
-
import type {
|
1
|
+
import type { SyntaxHighlighterProps, ThemedToken, TokensResult } from '../types/shiki'
|
2
2
|
import { useEffect, useState } from 'react'
|
3
3
|
import { createHighlighter } from '../syntax'
|
4
4
|
|
5
|
-
export function useSyntaxHighlighter({ text,
|
5
|
+
export function useSyntaxHighlighter({ text, language, languageId, theme }: SyntaxHighlighterProps) {
|
6
6
|
const [tokens, setTokens] = useState<ThemedToken[][]>([])
|
7
7
|
const [error, setError] = useState<string>()
|
8
8
|
const [isLoading, setIsLoading] = useState(true)
|
@@ -20,14 +20,14 @@ export function useSyntaxHighlighter({ text, lang, langName, theme, themeName }:
|
|
20
20
|
|
21
21
|
try {
|
22
22
|
const highlighter = await createHighlighter({
|
23
|
-
langs: [
|
24
|
-
themes: [theme],
|
23
|
+
langs: [language],
|
24
|
+
themes: theme ? [theme] : [],
|
25
25
|
})
|
26
26
|
|
27
27
|
const result = highlighter.codeToTokens(text, {
|
28
|
-
lang:
|
29
|
-
theme:
|
30
|
-
}) as
|
28
|
+
lang: languageId,
|
29
|
+
theme: theme || 'none',
|
30
|
+
}) as TokensResult
|
31
31
|
|
32
32
|
if (mounted && result.tokens) {
|
33
33
|
setTokens(result.tokens)
|
@@ -52,7 +52,7 @@ export function useSyntaxHighlighter({ text, lang, langName, theme, themeName }:
|
|
52
52
|
return () => {
|
53
53
|
mounted = false
|
54
54
|
}
|
55
|
-
}, [text,
|
55
|
+
}, [text, language, languageId, theme])
|
56
56
|
|
57
57
|
return { tokens, error, isLoading }
|
58
58
|
}
|
package/src/types/shiki.ts
CHANGED
@@ -1,17 +1,19 @@
|
|
1
|
-
import type { LanguageInput,
|
1
|
+
import type { LanguageInput, ThemeRegistrationAny } from 'shiki'
|
2
2
|
|
3
|
-
export
|
3
|
+
export interface ThemedToken {
|
4
|
+
content: string
|
5
|
+
color?: string
|
6
|
+
fontStyle?: string
|
7
|
+
fontWeight?: string
|
8
|
+
}
|
4
9
|
|
5
|
-
export interface
|
10
|
+
export interface TokensResult {
|
6
11
|
tokens: ThemedToken[][]
|
7
|
-
bg: string
|
8
|
-
fg: string
|
9
12
|
}
|
10
13
|
|
11
14
|
export interface SyntaxHighlighterProps {
|
12
15
|
text: string
|
13
|
-
|
14
|
-
|
15
|
-
theme
|
16
|
-
themeName: string
|
16
|
+
language: LanguageInput
|
17
|
+
languageId: string
|
18
|
+
theme?: ThemeRegistrationAny
|
17
19
|
}
|
package/src/types/shiki.d.ts
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
import type { LanguageInput, ThemeInput } from 'shiki'
|
2
|
-
|
3
|
-
declare module 'vendor/shiki/core/core' {
|
4
|
-
export interface HighlighterCoreOptions {
|
5
|
-
engine: any
|
6
|
-
langs: LanguageInput[]
|
7
|
-
themes: ThemeInput[]
|
8
|
-
}
|
9
|
-
|
10
|
-
export function createHighlighterCore(options: HighlighterCoreOptions): any
|
11
|
-
}
|
12
|
-
|
13
|
-
declare module 'vendor/shiki/core/engine-javascript' {
|
14
|
-
export function createJavaScriptRegexEngine(): any
|
15
|
-
}
|