-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unescaped code blocks syntax highlight doesn't work #4300
Comments
See #2315 return html from your loader and use |
@brc-dd I wasn't lucky with the import { readFileSync } from 'node:fs'
import { createMarkdownRenderer } from 'vitepress'
const config = globalThis.VITEPRESS_CONFIG
const md = await createMarkdownRenderer(config.srcDir, config.markdown, config.site.base, config.logger)
function getScriptCodeBlock(scriptText) {
return md.render(
`
\`\`\`ts
${scriptText}
\`\`\`
`,
)
}
export default {
watch: ['./scripts/*.ts'],
load(watchedFiles) {
return watchedFiles.reduce((acc, file) => {
const fileName = file.match(/([^/]+)(?=\.ts$)/)[0]
// const fileName = file.match(/[^/]+\.ts$/)[0]
return {
...acc,
[`${fileName}`]: getScriptCodeBlock(readFileSync(file, 'utf-8')),
}
}, {})
},
} but I found that in my case import the scripts as code snippets works perfectly <<< ./scripts/01-first-scene.ts I want to have the examples scrips on separated files so I can import and implement them or use them as text in a code block
|
it should be: return md.render(
`
\`\`\`ts
${scriptText}
\`\`\`
`
)
// or simply
return md.render("\n```ts\n" + scriptText + "\n```\n") (remove leading spaces from the code block, otherwise it will be considered an indented code block)
Ah, it is stable and public API, but not exactly user friendly. We probably should just expose the markdown render function for these use cases, which should use a global markdown-it instance. Currently, hmr is bit slow if you update your data loader file and it has createMarkdownRenderer calls (because it needs to re-initialize shiki). |
Describe the bug
When using Unescaped code blocks with the content coming from a string variable or a data loader, the code blocks aren't highlighted
Reproduction
https://stackblitz.com/edit/vite-ucyboc?file=docs%2Fexamples%2Ftest-example.md
Expected behavior
System Info
The text was updated successfully, but these errors were encountered: