Skip to content

Commit 87b8f37

Browse files
committed
update docs syntax theme
1 parent 18ba1f8 commit 87b8f37

File tree

5 files changed

+31
-13
lines changed

5 files changed

+31
-13
lines changed

remark/utils.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const syntaxTheme = require('../src/utils/syntaxTheme')
2+
const dlv = require('dlv')
13
const Prism = require('prismjs')
24
const loadLanguages = require('prismjs/components/')
35
loadLanguages()
@@ -27,10 +29,20 @@ module.exports.addExport = function addExport(tree, name, value) {
2729
}
2830

2931
module.exports.highlightCode = function highlightCode(code, language) {
30-
const highlighted = Prism.languages[language]
32+
let highlighted = Prism.languages[language]
3133
? Prism.highlight(code, Prism.languages[language], language)
3234
: code
3335

36+
highlighted = highlighted.replace(/class="token([^"]+)"/g, (_, p1) => {
37+
const types = p1.trim().split(/\s/)
38+
if (types.length === 0) return ''
39+
const classes = types
40+
.map((type) => dlv(syntaxTheme, [language, type], syntaxTheme[type]))
41+
.filter(Boolean)
42+
if (classes.length === 0) return ''
43+
return `class="${classes.join(' ')}"`
44+
})
45+
3446
return language === 'html'
3547
? highlighted.replace(
3648
/\*\*(.*?)\*\*/g,

src/components/CodeSample.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,12 @@ export function CodeSample({ preview, snippet, previewClassName, color = 'gray'
4141
})}
4242
>
4343
<pre
44-
className={clsx('scrollbar-none overflow-x-auto p-6 text-sm leading-snug language-html', {
45-
'bg-black bg-opacity-75': codeBackground[color],
46-
})}
44+
className={clsx(
45+
'scrollbar-none overflow-x-auto p-6 text-sm leading-snug language-html text-white',
46+
{
47+
'bg-black bg-opacity-75': codeBackground[color],
48+
}
49+
)}
4750
>
4851
<code className="language-html" dangerouslySetInnerHTML={{ __html: snippet }} />
4952
</pre>

src/components/CodeWindow.js

+2-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { forwardRef, Fragment, useMemo } from 'react'
33
import tokenize from '../macros/tokenize.macro'
44
import { Code } from './Code'
55
import styles from './CodeWindow.module.css'
6+
import syntaxTheme from '@/utils/syntaxTheme'
67

78
const { tokens: defaultTokens } = tokenize.html(`<div class="flex pa-2 bg-white rounded-lg shadow">
89
<div class="w-32 rounded-md overflow-hidden">
@@ -90,21 +91,13 @@ CodeWindow.Code = forwardRef(({ tokens = defaultTokens, initialLineNumber = 1, .
9091
)
9192
})
9293

93-
const themeDict = {
94-
punctuation: 'text-code-punctuation',
95-
tag: 'text-code-tag',
96-
'attr-name': 'text-code-attr-name',
97-
'attr-value': 'text-code-attr-value',
98-
class: 'text-code-attr-value',
99-
}
100-
10194
export function getClassNameForToken({ types, empty }) {
10295
const typesSize = types.length
10396
if (typesSize === 1 && types[0] === 'plain') {
10497
return empty ? 'inline-block' : undefined
10598
}
10699
const className = (
107-
(themeDict[types[typesSize - 1]] || '') + (empty ? ' inline-block' : '')
100+
(syntaxTheme[types[typesSize - 1]] || '') + (empty ? ' inline-block' : '')
108101
).trim()
109102
return className === '' ? undefined : className
110103
}

src/utils/syntaxTheme.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
punctuation: 'text-code-punctuation',
3+
tag: 'text-code-tag',
4+
'attr-name': 'text-code-attr-name',
5+
'attr-value': 'text-code-attr-value',
6+
class: 'text-code-attr-value',
7+
string: 'text-code-string',
8+
comment: 'opacity-25',
9+
}

tailwind.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ module.exports = {
9595
},
9696
pre: {
9797
backgroundColor: '-',
98+
color: theme('colors.white'),
9899
borderRadius: 0,
99100
marginTop: 0,
100101
marginBottom: 0,

0 commit comments

Comments
 (0)