Add inline code highlighting

This commit is contained in:
Misode
2022-05-11 18:17:46 +02:00
parent 5599cf1b4f
commit ad13cc131b
5 changed files with 140 additions and 105 deletions

View File

@@ -65,6 +65,40 @@ export function Guide({ id }: Props) {
const headings: marked.Tokens.Heading[] = []
let insertedToc = false
marked.use({
extensions: [
{
name: 'styledCode',
level: 'inline',
start(src) {
return src.match(/\b[fsnj]`/)?.index ?? -1
},
tokenizer(src) {
const match = src.match(/^([fsnj])`([^`]+)`/)
if (match) {
return {
type: 'styledCode',
raw: match[0],
prefix: match[1],
text: match[2],
}
}
return undefined
},
renderer(token) {
let content = token.text
let c = {
f: 'hljs-attr',
s: 'hljs-string',
n: 'hljs-number',
}[token.prefix as string]
if (token.prefix === 'j') {
content = hljs.highlight('json', token.text).value
c = 'language-json'
}
return `<code${c ? ` class="${c}"` : ''}>${content}</code>`
},
},
],
walkTokens(token) {
if (token.type === 'heading') {
headings.push(token)