MCPcopy
hub / github.com/code-hike/codehike / highlight

Function highlight

packages/codehike/src/code/highlight.ts:20–60  ·  view source on GitHub ↗
(
  data: RawCode,
  theme: Theme,
  config: { annotationPrefix?: string } = {},
)

Source from the content-addressed store, hash-verified

18type AnyToken = Token | Whitespace
19
20export 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
62function compatAnnotations(annotations: any[]): CodeAnnotation[] {
63 const newAnnotations: CodeAnnotation[] = []

Callers 15

MyCodeFunction · 0.90
InlineCodeFunction · 0.90
MyCodeFunction · 0.90
MyCodeFunction · 0.90
MyCodeFunction · 0.90
MyCodeFunction · 0.90
MyCodeFunction · 0.90
MyCodeFunction · 0.90
MyCodeFunction · 0.90
InlineCodeFunction · 0.90
CodeFunction · 0.90
CodeWithNotesFunction · 0.90

Calls 5

splitAnnotationsAndCodeFunction · 0.85
joinLinesFunction · 0.85
splitWhitespaceFunction · 0.85
joinWhitespaceFunction · 0.85
compatAnnotationsFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…