Skip to content

Commit 627457f

Browse files
committed
Write a rehype plugin to fix live code blocks
1 parent f36fb24 commit 627457f

File tree

8 files changed

+166
-8
lines changed

8 files changed

+166
-8
lines changed

packages/docs/gatsby-config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const remarkGfm = require('remark-gfm-1')
22
const remarkSlug = require('remark-slug')
3+
const { rehypeMetaAsAttributes } = require('./src/rehype-meta-props.cjs')
34

45
module.exports = {
56
siteMetadata: {
@@ -16,6 +17,7 @@ module.exports = {
1617
extensions: ['mdx', 'md'],
1718
mdxOptions: {
1819
remarkPlugins: [remarkGfm, remarkSlug],
20+
rehypePlugins: [rehypeMetaAsAttributes],
1921
},
2022
},
2123
},

packages/docs/gatsby-node.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const presets = require('@theme-ui/presets')
22
const fs = require('fs')
33
const path = require('path')
4+
const { rehypeMetaAsAttributes } = require('./src/rehype-meta-props.cjs')
45

56
const Preset = require.resolve('./src/templates/preset')
67

@@ -58,6 +59,7 @@ module.exports.onCreateWebpackConfig = ({ actions, getConfig }) => {
5859
require('remark-slug'),
5960
require('remark-gfm-1'),
6061
],
62+
rehypePlugins: [rehypeMetaAsAttributes],
6163
},
6264
},
6365
],

packages/docs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"icon": "npx repng src/components/logo.js -d static -f icon.png -w=32 -h=32 --props='{\"size\":32}'"
1919
},
2020
"dependencies": {
21+
"unist-util-visit-2": "npm:unist-util-visit@^2.0.2",
2122
"@babel/helper-string-parser": "^7.22.5",
2223
"@emotion/react": "^11.11.1",
2324
"@mdx-js/loader": "^2.3.0",

packages/docs/src/components/code.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Prism, { ThemeUIPrismProps } from '@theme-ui/prism'
55
import { LiveProvider, LiveEditor, LivePreview, LiveError } from 'react-live'
66
import * as themeUI from 'theme-ui'
77
import { ComponentPropsWithoutRef } from 'react'
8+
import { PrismTheme } from 'prism-react-renderer'
89

910
const posts = [
1011
{
@@ -67,7 +68,7 @@ const transformCode = (src) => {
6768
return `<>${src}</>`
6869
}
6970

70-
const liveTheme = { styles: [] }
71+
const liveTheme: PrismTheme = { plain: {}, styles: [] }
7172

7273
export const LiveCode = ({ children, preview, xray }) => {
7374
const code = stripTrailingNewline(children)
@@ -112,7 +113,12 @@ export const LiveCode = ({ children, preview, xray }) => {
112113
/>
113114
</div>
114115
<Themed.pre sx={{ p: 0, mt: 0, mb: 3 }}>
115-
<LiveEditor padding="1rem" />
116+
<LiveEditor
117+
padding="1rem"
118+
style={{
119+
fontFamily: 'inherit',
120+
}}
121+
/>
116122
</Themed.pre>
117123
</LiveProvider>
118124
)
@@ -136,7 +142,14 @@ const CodeBlock = (props: CodeBlockProps) => {
136142
}
137143

138144
if (props.live) {
139-
return <LiveCode {...props} />
145+
return (
146+
<LiveCode
147+
{...props}
148+
style={{
149+
fontFamily: 'Menlo',
150+
}}
151+
/>
152+
)
140153
} else {
141154
const { live: _, filename, ...rest } = props as UsualCodeBlockProps
142155
if (filename) {

packages/docs/src/pages/components/avatar.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { Avatar } from 'theme-ui'
99
```
1010

1111
```jsx live=true
12-
<Avatar src={images.logo} />
12+
<Avatar src={images.logo} sx={{ backgroundColor: 'white' }} />
1313
```
1414

1515
## Variants

packages/docs/src/pages/components/badge.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { Badge } from 'theme-ui'
2323
```
2424

2525
```jsx live=true
26-
<Avatar src={images.logo} />
26+
<Avatar src={images.logo} sx={{ backgroundColor: 'white' }} />
2727
<Badge variant='circle' ml={-3} mt={-3}>16</Badge>
2828
```
2929

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const visit = require('unist-util-visit-2')
2+
3+
const META_ATTR_REGEX = /\b([-\w]+)(?:=(?:"([^"]*)"|'([^']*)'|([^"'\s]+)))?/g
4+
5+
/** @type {import('unified').Plugin<Array<void>, import('hast').Root>} */
6+
function rehypeMetaAsAttributes() {
7+
return (tree) => {
8+
visit(tree, 'element', (node) => {
9+
if (node.tagName === 'code' && node.data && node.data.meta) {
10+
META_ATTR_REGEX.lastIndex = 0 // Reset regex.
11+
12+
let match
13+
while ((match = META_ATTR_REGEX.exec(node.data.meta))) {
14+
node.properties[match[1]] = match[2] || match[3] || match[4] || ''
15+
}
16+
}
17+
})
18+
}
19+
}
20+
21+
module.exports = { rehypeMetaAsAttributes }

pnpm-lock.yaml

Lines changed: 122 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)