Skip to content

Commit 7089cc8

Browse files
authored
Merge pull request #318 from code-hike/handle-lighter-errors
Catch lighter error
2 parents bc2b6ac + eb851a8 commit 7089cc8

File tree

5 files changed

+91
-30
lines changed

5 files changed

+91
-30
lines changed

packages/mdx/package.json

Lines changed: 1 addition & 1 deletion
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.2.0",
47+
"@code-hike/lighter": "^0.2.3",
4848
"node-fetch": "^2.0.0",
4949
"shiki": "^0.10.1"
5050
},

packages/mdx/src/highlighter/index.tsx

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,23 @@ export async function highlight({
2222
}
2323
}
2424

25-
const r = await light(code, lang as any, theme)
25+
try {
26+
const r = await light(code, lang as any, theme)
2627

27-
const lines = r.lines.map(line => ({
28-
tokens: line.map(token => ({
29-
content: token.content,
30-
props: { style: token.style },
31-
})),
32-
}))
28+
const lines = r.lines.map(line => ({
29+
tokens: line.map(token => ({
30+
content: token.content,
31+
props: { style: token.style },
32+
})),
33+
}))
3334

34-
return { lines, lang }
35+
return { lines, lang }
36+
} catch (e) {
37+
// TODO check error is "missing grammar"
38+
console.warn(
39+
"[Code Hike warning]",
40+
`${lang} is not a valid language, no syntax highlighting will be applied.`
41+
)
42+
return highlight({ code, lang: "text", theme })
43+
}
3544
}

playground/src/index.css

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,26 @@ main {
214214
display: block;
215215
padding: 4px;
216216
}
217+
218+
.loading-border {
219+
position: absolute;
220+
top: 0;
221+
border-top: 6px solid rgb(96 165 250);
222+
transition: opacity 0.5s;
223+
animation: loading-border 2.5s linear infinite;
224+
}
225+
226+
@keyframes loading-border {
227+
from {
228+
right: 100%;
229+
left: 0%;
230+
}
231+
50% {
232+
right: 0%;
233+
left: 0%;
234+
}
235+
to {
236+
right: 0%;
237+
left: 100%;
238+
}
239+
}

playground/src/preview.jsx

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@ function ErrorFallback({ error }) {
2929
);
3030
}
3131

32-
function InnerPreview({ input, standalone, refreshKey }) {
33-
const [Content, setContent] = useState(undefined);
34-
const [error, setError] = useState(undefined);
35-
useEffect(() => {
36-
compile(input.mdx, {
32+
async function compileAndRun(input) {
33+
try {
34+
const c = await compile(input.mdx, {
3735
outputFormat: "function-body",
3836
remarkPlugins: [
3937
[
@@ -45,19 +43,49 @@ function InnerPreview({ input, standalone, refreshKey }) {
4543
},
4644
],
4745
],
48-
})
49-
.then((c) => {
50-
return run(String(c), runtime);
51-
})
52-
.then((x) => {
53-
setContent(() => x.default);
54-
setError(undefined);
55-
})
56-
.catch((e) => {
57-
setError(e.message);
58-
console.error({ e });
59-
});
46+
});
47+
const x = await run(String(c), runtime);
48+
return { content: x.default, error: undefined };
49+
} catch (e) {
50+
return { content: undefined, error: e.message };
51+
}
52+
}
53+
54+
let effectId = 0;
55+
56+
function useInput(input) {
57+
const [{ Content, error }, setState] = useState({
58+
Content: undefined,
59+
error: undefined,
60+
});
61+
const [loading, setLoading] = useState(true);
62+
useEffect(() => {
63+
const id = effectId;
64+
// console.log("compiling...", id);
65+
setLoading(true);
66+
compileAndRun(input).then(({ content, error }) => {
67+
// console.log("compiled", id, error);
68+
if (id !== effectId) {
69+
// console.log("skipping", id);
70+
return;
71+
}
72+
setState((state) => ({
73+
Content: content || state.Content,
74+
error,
75+
}));
76+
setLoading(false);
77+
});
78+
return () => {
79+
// console.log("cancelling", id);
80+
effectId++;
81+
};
6082
}, [input.mdx, input.css, input.config]);
83+
84+
return { Content, error, loading };
85+
}
86+
87+
function InnerPreview({ input, standalone, refreshKey }) {
88+
const { Content, error, loading } = useInput(input);
6189
// console.log(error);
6290
return (
6391
<>
@@ -78,6 +106,7 @@ function InnerPreview({ input, standalone, refreshKey }) {
78106
</a>
79107
)}
80108
<div className={`preview-container ${error ? "with-error" : ""}`}>
109+
<div style={{ opacity: loading ? 1 : 0 }} className="loading-border" />
81110
{Content ? <Content components={{ CH }} key={refreshKey} /> : null}
82111
</div>
83112
</>

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2844,10 +2844,10 @@
28442844
resolved "https://registry.yarnpkg.com/@builder.io/partytown/-/partytown-0.5.4.tgz#1a89069978734e132fa4a59414ddb64e4b94fde7"
28452845
integrity sha512-qnikpQgi30AS01aFlNQV6l8/qdZIcP76mp90ti+u4rucXHsn4afSKivQXApqxvrQG9+Ibv45STyvHizvxef/7A==
28462846

2847-
"@code-hike/lighter@^0.2.0":
2848-
version "0.2.0"
2849-
resolved "https://registry.yarnpkg.com/@code-hike/lighter/-/lighter-0.2.0.tgz#8ebcd23fbeba020850b5a5e7f61a116dd8175a60"
2850-
integrity sha512-THj5c7M6TS/d2t8MUiwkoA3H4qN4L1gr7O8SC0TyM4m6n3y7vsfVYp8LsHi+ksa66kkzyo8Mxo0OqBdL08d7FA==
2847+
"@code-hike/lighter@^0.2.3":
2848+
version "0.2.3"
2849+
resolved "https://registry.yarnpkg.com/@code-hike/lighter/-/lighter-0.2.3.tgz#bab489b309c70ddc741a660f146312e47e014e62"
2850+
integrity sha512-9Kn/FHTXHEzB5GKsFPUjLqFgWWRGA6sKivOGZ1+3kKGWiKCsPtMwbLUwQeYwcOihPE+7SNLBlE0wpDkG62B2jQ==
28512851

28522852
"@codesandbox/sandpack-client@^0.19.0":
28532853
version "0.19.0"

0 commit comments

Comments
 (0)