Skip to content
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

feat(build): allow specifying default language for syntax highlighter #1296

Merged
merged 3 commits into from
Dec 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions docs/config/app-configs.md
Original file line number Diff line number Diff line change
@@ -69,7 +69,10 @@ Additional elements to render in the `<head>` tag in the page HTML. The user-add
```ts
export default {
head: [
['link', { rel: 'preconnect', href: 'https://fonts.gstatic.com', crossorigin: '' }]
[
'link',
{ rel: 'preconnect', href: 'https://fonts.gstatic.com', crossorigin: '' }
]
// would render: <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
]
}
@@ -164,6 +167,9 @@ interface MarkdownOptions extends MarkdownIt.Options {
disable?: boolean
}

// specify default language for syntax highlighter
defaultHighlightLang?: string

// @mdit-vue/plugin-frontmatter plugin options.
// See: https://github.com/mdit-vue/mdit-vue/tree/main/packages/plugin-frontmatter#options
frontmatter?: FrontmatterPluginOptions
@@ -298,6 +304,7 @@ Don't mutate anything inside the `ctx`.
```ts
export default {
async transformHead(ctx) {
// ...
}
}
```
@@ -327,6 +334,7 @@ Don't mutate anything inside the `ctx`. Also, modifying the html content may cau
```ts
export default {
async transformHtml(code, id, context) {
// ...
}
}
```
@@ -337,7 +345,6 @@ export default {

`transformPageData` is a hook to transform the `pageData` of each page. You can directly mutate `pageData` or return changed values which will be merged into PageData.


```ts
export default {
async transformPageData(pageData) {
@@ -362,6 +369,7 @@ export default {
```ts
export default {
async buildEnd(siteConfig) {
// ...
}
}
```
5 changes: 4 additions & 1 deletion src/node/markdown/markdown.ts
Original file line number Diff line number Diff line change
@@ -40,6 +40,7 @@ export interface MarkdownOptions extends MarkdownIt.Options {
allowedAttributes?: string[]
disable?: boolean
}
defaultHighlightLang?: string
frontmatter?: FrontmatterPluginOptions
headers?: HeadersPluginOptions
sfc?: SfcPluginOptions
@@ -60,7 +61,9 @@ export const createMarkdownRenderer = async (
const md = MarkdownIt({
html: true,
linkify: true,
highlight: options.highlight || (await highlight(options.theme)),
highlight:
options.highlight ||
(await highlight(options.theme, options.defaultHighlightLang)),
...options
}) as MarkdownRenderer

5 changes: 3 additions & 2 deletions src/node/markdown/plugins/highlight.ts
Original file line number Diff line number Diff line change
@@ -52,7 +52,8 @@ const errorLevelProcessor = defineProcessor({
})

export async function highlight(
theme: ThemeOptions = 'material-palenight'
theme: ThemeOptions = 'material-palenight',
defaultLang: string = ''
): Promise<(str: string, lang: string, attrs: string) => string> {
const hasSingleTheme = typeof theme === 'string' || 'name' in theme
const getThemeName = (themeValue: IThemeRegistration) =>
@@ -76,7 +77,7 @@ export async function highlight(

return (str: string, lang: string, attrs: string) => {
const vPre = vueRE.test(lang) ? '' : 'v-pre'
lang = lang.replace(vueRE, '').toLowerCase()
lang = lang.replace(vueRE, '').toLowerCase() || defaultLang

const lineOptions = attrsToLines(attrs)
const cleanup = (str: string) =>