Skip to content

Commit bf4bdae

Browse files
authored
Merge pull request #338 from code-hike/next
v0.8.1
2 parents 7c1e552 + 80cb8c1 commit bf4bdae

File tree

9 files changed

+232
-42
lines changed

9 files changed

+232
-42
lines changed

packages/mdx/dev/content/markdown.mdx

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,33 @@
11
Test nested syntax highlighting
22

3-
````md hi.md
4-
# Hi
3+
````mdx hi.md
4+
<CH.Section>
55

6-
```js
7-
export default {
8-
data() {
9-
return {
10-
greeting: "Hello World!",
11-
}
12-
},
6+
```python
7+
def lorem(ipsum):
8+
ipsum + 1
9+
```
10+
11+
Something `def lorem(ipsum)`
12+
13+
Hello, [hover me](focus://1[5:16])
14+
15+
<CH.Code>
16+
17+
```python one.py
18+
def lorem(ipsum):
19+
ipsum + 1
20+
```
21+
22+
```js two.js
23+
function lorem(ipsum) {
24+
return ipsum + 1
1325
}
1426
```
1527

16-
[lorem](https://loremipsum.com)
28+
</CH.Code>
29+
30+
</CH.Section>
1731
````
1832

1933
```js hi.js

packages/mdx/dev/content/slideshow-autoplay.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Slideshow Auto
22

3-
This is how to use the `<CH.Sildeshow>` component. Lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam, quia! Quidem, quisquam.
3+
This is how to use the `<CH.Slideshow>` component. Lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam, quia! Quidem, quisquam.
44

55
<CH.Slideshow preset="https://codesandbox.io/s/rfdho" code={{minZoom: 0.5}} autoPlay={3000} loop>
66

packages/mdx/dev/content/slideshow-demo.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Slideshow with preview
22

3-
This is how to use the `<CH.Sildeshow>` component. Lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam, quia! Quidem, quisquam.
3+
This is how to use the `<CH.Slideshow>` component. Lorem ipsum dolor sit amet consectetur adipisicing elit. Quisquam, quia! Quidem, quisquam.
44

55
<CH.Slideshow preset="https://codesandbox.io/s/rfdho" code={{minZoom: 0.5}}>
66

packages/mdx/package.json

Lines changed: 2 additions & 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.5.2",
4848
"node-fetch": "^2.0.0",
4949
"shiki": "^0.10.1"
5050
},
@@ -72,6 +72,7 @@
7272
"react-dom": "^18.1.0",
7373
"react-json-view": "^1.21.3",
7474
"rollup": "^2.41.2",
75+
"rollup-plugin-banner2": "^1.2.2",
7576
"rollup-plugin-delete": "^2.0.0",
7677
"rollup-plugin-dts": "^4.2.1",
7778
"rollup-plugin-postcss": "^4.0.0",

packages/mdx/rollup.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import dts from "rollup-plugin-dts"
1212
import { nodeResolve } from "@rollup/plugin-node-resolve"
1313
// import { terser } from "rollup-plugin-terser"
1414
import commonjs from "@rollup/plugin-commonjs"
15+
import banner2 from "rollup-plugin-banner2"
1516

1617
const clientExternal = [
1718
"react",
@@ -136,6 +137,7 @@ export default function makeConfig(commandOptions) {
136137
compilerOptions: { jsx: "react" },
137138
},
138139
}),
140+
banner2(() => `"use client";\n`),
139141
],
140142
},
141143
{

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
</>

0 commit comments

Comments
 (0)