Skip to content

Commit

Permalink
fix(build): use addClass from shiki-processor (#1557)
Browse files Browse the repository at this point in the history
reverts #1546 (fixed in upstream)
fixes #1555
  • Loading branch information
brc-dd authored Oct 30, 2022
1 parent 8cd1f7c commit 4b0b1ef
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@
"@vueuse/core": "^9.4.0",
"body-scroll-lock": "4.0.0-beta.0",
"shiki": "^0.11.1",
"shiki-processor": "^0.1.0",
"vite": "^3.1.8",
"vue": "^3.2.41"
},
Expand Down Expand Up @@ -157,6 +156,7 @@
"semver": "^7.3.8",
"simple-git-hooks": "^2.8.1",
"sirv": "^2.0.2",
"shiki-processor": "^0.1.1",
"supports-color": "^9.2.3",
"typescript": "~4.8.4",
"vitest": "^0.24.3",
Expand Down
13 changes: 5 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 26 additions & 18 deletions src/node/markdown/plugins/highlight.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { IThemeRegistration, HtmlRendererOptions } from 'shiki'
import type { HtmlRendererOptions, IThemeRegistration } from 'shiki'
import {
addClass,
createDiffProcessor,
createFocusProcessor,
createHighlightProcessor,
createRangeProcessor,
defineProcessor,
getHighlighter,
Processor,
defineProcessor
type Processor
} from 'shiki-processor'
import type { ThemeOptions } from '../markdown'

Expand Down Expand Up @@ -68,7 +69,6 @@ export async function highlight(
processors
})

const classRE = /<pre[^>]*class="(.*?)"/
const styleRE = /<pre[^>]*(style=".*?")/
const preRE = /^<pre(.*?)>/
const vueRE = /-vue$/
Expand All @@ -93,21 +93,29 @@ export async function highlight(
)
}

const dark = cleanup(
highlighter.codeToHtml(str, {
lang,
lineOptions,
theme: getThemeName(theme.dark)
})
).replace(classRE, (_, cls) => _.replace(cls, 'vp-code-dark'))
const dark = addClass(
cleanup(
highlighter.codeToHtml(str, {
lang,
lineOptions,
theme: getThemeName(theme.dark)
})
),
'vp-code-dark',
'pre'
)

const light = cleanup(
highlighter.codeToHtml(str, {
lang,
lineOptions,
theme: getThemeName(theme.light)
})
).replace(classRE, (_, cls) => _.replace(cls, 'vp-code-light'))
const light = addClass(
cleanup(
highlighter.codeToHtml(str, {
lang,
lineOptions,
theme: getThemeName(theme.light)
})
),
'vp-code-light',
'pre'
)

return dark + light
}
Expand Down

0 comments on commit 4b0b1ef

Please sign in to comment.