Skip to content

Commit 6becc50

Browse files
committed
Upgrade lighter
1 parent 5cf61c0 commit 6becc50

File tree

9 files changed

+66
-99
lines changed

9 files changed

+66
-99
lines changed

packages/mdx/dev/content/comment-annotations.mdx

+7-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,13 @@ body {
129129
```
130130

131131
```mdx
132-
# Hello {/* mark */}
132+
{/* mark(2) */}
133+
134+
# Lorem
135+
136+
## Foo
137+
138+
# Ipsum {/* mark */}
133139
```
134140

135141
```jsonc

packages/mdx/dev/content/simple-code.mdx

+7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ graph LR
1414
--> End1(End)
1515
```
1616

17+
```foobar
18+
// unknown lang
19+
function lorem(ipsum, dolor = 1) {
20+
return ipsum + dolor;
21+
}
22+
```
23+
1724
```js
1825
function lorem(ipsum, dolor = 1) {}
1926
```

packages/mdx/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"coverage": "vitest run --coverage"
4545
},
4646
"dependencies": {
47-
"@code-hike/lighter": "0.7.0",
47+
"@code-hike/lighter": "0.7.3",
4848
"node-fetch": "^2.0.0"
4949
},
5050
"devDependencies": {

packages/mdx/src/highlighter/index.tsx

-50
This file was deleted.

packages/mdx/src/index.tsx

-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
export { attacher as remarkCodeHike } from "./remark/transform"
2-
3-
export { highlight } from "./highlighter"

packages/mdx/src/remark/code.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { highlight } from "../highlighter"
1+
import { highlight } from "./lighter"
22
import { NodeInfo, splitChildren } from "./unist-utils"
33
import { CodeStep } from "../smooth-code"
44
import { EditorProps } from "../mini-editor"

packages/mdx/src/remark/lighter.ts

+45-39
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import {
2-
highlight,
2+
highlight as light,
33
extractAnnotations,
44
Annotation,
5+
LANG_NAMES,
56
} from "@code-hike/lighter"
7+
import { Code } from "utils"
68
import { annotationsMap } from "../mdx-client/annotations"
79
import { CodeAnnotation } from "../smooth-code"
810

@@ -20,48 +22,11 @@ export async function extractLighterAnnotations(
2022
) {
2123
return await extractAnnotations(
2224
codeWithAnnotations,
23-
lang,
25+
warnIfUnknownLang(lang),
2426
annotationNames
2527
)
2628
}
2729

28-
export async function extractAnnotationsFromCode(
29-
codeWithAnnotations: string,
30-
lang: string,
31-
names?: string[]
32-
) {
33-
const { code, annotations } = await extractAnnotations(
34-
codeWithAnnotations,
35-
lang,
36-
names || annotationNames
37-
)
38-
39-
const focusList = [] as string[]
40-
41-
const codeAnnotations = [] as CodeAnnotation[]
42-
43-
annotations.forEach(({ name, query, ranges }) => {
44-
ranges.forEach(range => {
45-
const focus = rangeString(range)
46-
if (name === "focus") {
47-
focusList.push(focus)
48-
} else {
49-
const Component = annotationsMap[name]
50-
if (Component) {
51-
codeAnnotations.push({
52-
Component,
53-
focus: focus,
54-
data: query,
55-
})
56-
}
57-
}
58-
})
59-
})
60-
61-
return { code, annotations: codeAnnotations }
62-
}
63-
64-
// lighter annotations to CodeAnnotations
6530
export function parseLighterAnnotations(
6631
annotations: LighterAnnotation[]
6732
) {
@@ -102,3 +67,44 @@ function rangeString(range: Annotation["ranges"][0]) {
10267
return `${range.fromLineNumber}:${range.toLineNumber}`
10368
}
10469
}
70+
71+
const warnings = new Set()
72+
73+
function warnIfUnknownLang(lang: string) {
74+
if (!LANG_NAMES.includes(lang)) {
75+
if (!warnings.has(lang)) {
76+
console.warn(
77+
"[Code Hike warning]",
78+
`${lang} isn't a valid language, no syntax highlighting will be applied.`
79+
)
80+
warnings.add(lang)
81+
}
82+
return "text"
83+
}
84+
return lang
85+
}
86+
87+
export async function highlight({
88+
code,
89+
lang,
90+
theme,
91+
}: {
92+
code: string
93+
lang: string
94+
theme: any // TODO type this
95+
}): Promise<Code> {
96+
const r = await light(
97+
code,
98+
warnIfUnknownLang(lang),
99+
theme
100+
)
101+
102+
const lines = r.lines.map(line => ({
103+
tokens: line.map(token => ({
104+
content: token.content,
105+
props: { style: token.style },
106+
})),
107+
}))
108+
109+
return { lines, lang: r.lang }
110+
}

packages/mdx/src/remark/transform.inline-code.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { visitAsync, toJSX } from "./unist-utils"
2-
import { highlight } from "../highlighter"
2+
import { highlight } from "./lighter"
33
import { EditorStep } from "../mini-editor"
44
import { Code } from "../utils"
55
import { SuperNode, visit } from "./nodes"

yarn.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -455,10 +455,10 @@
455455
"@babel/helper-validator-identifier" "^7.19.1"
456456
to-fast-properties "^2.0.0"
457457

458-
"@code-hike/lighter@0.7.0":
459-
version "0.7.0"
460-
resolved "https://registry.yarnpkg.com/@code-hike/lighter/-/lighter-0.7.0.tgz#7bb7d59631237d7d2e82434c3ea6fe1875813cb0"
461-
integrity sha512-64O07rIORKQLB+5T/GKAmKcD9sC0N9yHFJXa0Hs+0Aee1G+I4bSXxTccuDFP6c/G/3h5Pk7yv7PoX9/SpzaeiQ==
458+
"@code-hike/lighter@0.7.3":
459+
version "0.7.3"
460+
resolved "https://registry.yarnpkg.com/@code-hike/lighter/-/lighter-0.7.3.tgz#729a7ab484f11069d258c34dfe39be918f093e39"
461+
integrity sha512-IW3nBrnQRSoYDY2suXUjNZegCGVH3ljEH5w51chazy30s103CdpwSwvFya3e6GbY/xfbyAVj+Uyg6HH4L5T6tw==
462462

463463
"@codesandbox/sandpack-client@^0.19.0":
464464
version "0.19.0"

0 commit comments

Comments
 (0)