Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions examples/browser/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!DOCTYPE html>
<html>
<head>
<title>Code Hike in the browser</title>
<link
rel="stylesheet"
href="https://esm.sh/@code-hike/mdx@0.4.0--canary.154.285c03a.0/dist/index.css"
/>
</head>
<body>
<div id="root"></div>

<script type="module">
import React from "https://esm.sh/react"
import ReactDOM from "https://esm.sh/react-dom"
import { evaluate } from "https://esm.sh/@mdx-js/mdx?dev"
import * as runtime from "https://esm.sh/react/jsx-runtime.js"

import { remarkCodeHike } from "https://esm.sh/@code-hike/mdx@0.4.0--canary.154.285c03a.0/dist/index.browser.mjs"
import { CH } from "https://esm.sh/@code-hike/mdx@0.4.0--canary.154.285c03a.0/components"
import theme from "https://esm.sh/shiki@0.10.1/themes/nord.json" assert { type: "json" }

const mdx = `# Test

~~~python hello.py mark=1[22:30]
print("Rendered with Code Hike")
~~~
`

const { default: Content } = await evaluate(mdx, {
...runtime,
remarkPlugins: [[remarkCodeHike, { autoImport: false, theme }]],
})

const App = () => {
return React.createElement(Content, { components: { CH } })
}

ReactDOM.render(React.createElement(App), document.getElementById("root"))
</script>
</body>
</html>
4 changes: 4 additions & 0 deletions packages/mdx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
"default": "./dist/components.cjs.js"
}
},
"browser": {
"./dist/index.esm.js": "./dist/index.browser.mjs"
},
"scripts": {
"dev": "next",
"build": "rollup -c rollup.config.js",
Expand All @@ -50,6 +53,7 @@
"devDependencies": {
"@mdx-js/mdx": "^2.0.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-replace": "^4.0.0",
"@types/diff": "^5.0.2",
"@types/node": "^17.0.21",
"@types/node-fetch": "^2.6.1",
Expand Down
30 changes: 30 additions & 0 deletions packages/mdx/rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import typescript from "rollup-plugin-typescript2"
import path from "path"
import json from "@rollup/plugin-json"
import del from "rollup-plugin-delete"
import replace from "@rollup/plugin-replace"
// import { terser } from "rollup-plugin-terser"

export default function makeConfig(commandOptions) {
return [
Expand Down Expand Up @@ -46,6 +48,34 @@ export default function makeConfig(commandOptions) {
}),
],
},
// for the browser esm we need to replace shiki with shiki/dist/index.browser.mjs
// https://github.com/shikijs/shiki/issues/131#issuecomment-1094281851
{
input: `src/index.tsx`,
output: [
{
file: `./dist/index.browser.mjs`,
format: "es",
},
],
// external: ["react"],
plugins: [
replace({
delimiters: ["", ""],
values: {
'from "shiki"':
'from "shiki/dist/index.browser.mjs"',
},
}),
json({ compact: true }),
typescript({
tsconfigOverride: {
compilerOptions: { jsx: "react" },
},
}),
// terser(),
],
},
{
input: `src/components.tsx`,
output: [
Expand Down
1 change: 1 addition & 0 deletions packages/mdx/src/highlighter/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export async function highlight({
}
}
if (highlighterPromise === null) {
// TODO add version
setCDN("https://unpkg.com/shiki/")
highlighterPromise = getHighlighter({
theme: theme as IShikiTheme,
Expand Down
Loading