Skip to content

Commit 8cc8db8

Browse files
authored
Merge pull request #154 from code-hike/browser-example
Browser example
2 parents ff73d54 + ea2f836 commit 8cc8db8

File tree

5 files changed

+124
-483
lines changed

5 files changed

+124
-483
lines changed

examples/browser/index.html

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Code Hike in the browser</title>
5+
<link
6+
rel="stylesheet"
7+
href="https://esm.sh/@code-hike/mdx@0.4.0--canary.154.285c03a.0/dist/index.css"
8+
/>
9+
</head>
10+
<body>
11+
<div id="root"></div>
12+
13+
<script type="module">
14+
import React from "https://esm.sh/react"
15+
import ReactDOM from "https://esm.sh/react-dom"
16+
import { evaluate } from "https://esm.sh/@mdx-js/mdx?dev"
17+
import * as runtime from "https://esm.sh/react/jsx-runtime.js"
18+
19+
import { remarkCodeHike } from "https://esm.sh/@code-hike/mdx@0.4.0--canary.154.285c03a.0/dist/index.browser.mjs"
20+
import { CH } from "https://esm.sh/@code-hike/mdx@0.4.0--canary.154.285c03a.0/components"
21+
import theme from "https://esm.sh/shiki@0.10.1/themes/nord.json" assert { type: "json" }
22+
23+
const mdx = `# Test
24+
25+
~~~python hello.py mark=1[22:30]
26+
print("Rendered with Code Hike")
27+
~~~
28+
`
29+
30+
const { default: Content } = await evaluate(mdx, {
31+
...runtime,
32+
remarkPlugins: [[remarkCodeHike, { autoImport: false, theme }]],
33+
})
34+
35+
const App = () => {
36+
return React.createElement(Content, { components: { CH } })
37+
}
38+
39+
ReactDOM.render(React.createElement(App), document.getElementById("root"))
40+
</script>
41+
</body>
42+
</html>

packages/mdx/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
"default": "./dist/components.cjs.js"
2929
}
3030
},
31+
"browser": {
32+
"./dist/index.esm.js": "./dist/index.browser.mjs"
33+
},
3134
"scripts": {
3235
"dev": "next",
3336
"build": "rollup -c rollup.config.js",
@@ -50,6 +53,7 @@
5053
"devDependencies": {
5154
"@mdx-js/mdx": "^2.0.0",
5255
"@rollup/plugin-json": "^4.1.0",
56+
"@rollup/plugin-replace": "^4.0.0",
5357
"@types/diff": "^5.0.2",
5458
"@types/node": "^17.0.21",
5559
"@types/node-fetch": "^2.6.1",

packages/mdx/rollup.config.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import typescript from "rollup-plugin-typescript2"
77
import path from "path"
88
import json from "@rollup/plugin-json"
99
import del from "rollup-plugin-delete"
10+
import replace from "@rollup/plugin-replace"
11+
// import { terser } from "rollup-plugin-terser"
1012

1113
export default function makeConfig(commandOptions) {
1214
return [
@@ -46,6 +48,34 @@ export default function makeConfig(commandOptions) {
4648
}),
4749
],
4850
},
51+
// for the browser esm we need to replace shiki with shiki/dist/index.browser.mjs
52+
// https://github.com/shikijs/shiki/issues/131#issuecomment-1094281851
53+
{
54+
input: `src/index.tsx`,
55+
output: [
56+
{
57+
file: `./dist/index.browser.mjs`,
58+
format: "es",
59+
},
60+
],
61+
// external: ["react"],
62+
plugins: [
63+
replace({
64+
delimiters: ["", ""],
65+
values: {
66+
'from "shiki"':
67+
'from "shiki/dist/index.browser.mjs"',
68+
},
69+
}),
70+
json({ compact: true }),
71+
typescript({
72+
tsconfigOverride: {
73+
compilerOptions: { jsx: "react" },
74+
},
75+
}),
76+
// terser(),
77+
],
78+
},
4979
{
5080
input: `src/components.tsx`,
5181
output: [

packages/mdx/src/highlighter/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export async function highlight({
3333
}
3434
}
3535
if (highlighterPromise === null) {
36+
// TODO add version
3637
setCDN("https://unpkg.com/shiki/")
3738
highlighterPromise = getHighlighter({
3839
theme: theme as IShikiTheme,

0 commit comments

Comments
 (0)