(
data: RawCode,
theme: Theme,
config: { annotationPrefix?: string } = {},
)
| 18 | type AnyToken = Token | Whitespace |
| 19 | |
| 20 | export async function highlight( |
| 21 | data: RawCode, |
| 22 | theme: Theme, |
| 23 | config: { annotationPrefix?: string } = {}, |
| 24 | ): Promise<HighlightedCode> { |
| 25 | let { value, lang = "txt" } = data |
| 26 | |
| 27 | if (!LANG_NAMES.includes(lang)) { |
| 28 | console.warn(`Code Hike warning: Unknown language "${lang}"`) |
| 29 | lang = "txt" |
| 30 | } |
| 31 | |
| 32 | const { code, annotations } = await splitAnnotationsAndCode( |
| 33 | value, |
| 34 | lang, |
| 35 | config.annotationPrefix || "!", |
| 36 | ) |
| 37 | const { |
| 38 | lines, |
| 39 | lang: lighterLang, |
| 40 | style, |
| 41 | } = await lighter(code, lang, theme as any, { |
| 42 | annotations: [], |
| 43 | scopes: false, // true for better token transitions, but breaks css themes |
| 44 | }) |
| 45 | |
| 46 | const tokens = joinLines(lines) |
| 47 | // split surrounding whitespace for each token |
| 48 | const splitTokens = splitWhitespace(tokens) |
| 49 | // join consecutive whitespace tokens |
| 50 | const joinedTokens = joinWhitespace(splitTokens) |
| 51 | return { |
| 52 | ...data, |
| 53 | code, |
| 54 | tokens: joinedTokens, |
| 55 | lang: lighterLang, |
| 56 | annotations: compatAnnotations(annotations), |
| 57 | themeName: typeof theme === "string" ? theme : theme?.name || "unknown", |
| 58 | style, |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | function compatAnnotations(annotations: any[]): CodeAnnotation[] { |
| 63 | const newAnnotations: CodeAnnotation[] = [] |
no test coverage detected
searching dependent graphs…