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: allow to use custom syntax highlighting themes #992

Merged
merged 4 commits into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
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
17 changes: 10 additions & 7 deletions docs/config/app-configs.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,18 @@ export default {
}
```

Below shows the the full option you may define within this object.
Below are all the options that you can have in this object:

```ts
interface MarkdownOptions extends MarkdownIt.Options {
// Syntax highlight theme for Shiki.
// Custom theme for syntax highlighting.
// You can use an existing theme.
// See: https://github.com/shikijs/shiki/blob/main/docs/themes.md#all-themes
theme?: Shiki.Theme | { light: Shiki.Theme, dark: Shiki.Theme }
// Or add your own theme.
// See: https://github.com/shikijs/shiki/blob/main/docs/themes.md#loading-theme
theme?:
| Shiki.IThemeRegistration
| { light: Shiki.IThemeRegistration; dark: Shiki.IThemeRegistration }

// Enable line numbers in code block.
lineNumbers?: boolean
Expand All @@ -136,11 +141,10 @@ interface MarkdownOptions extends MarkdownIt.Options {
}

// markdown-it-toc-done-right plugin options
// https://github.com/nagaozen/markdown-it-toc-done-right
// See: https://github.com/nagaozen/markdown-it-toc-done-right
toc?: any

// Configure the Markdown-it instance to fully customize
// how it works.
// Configure the Markdown-it instance.
config?: (md: MarkdownIt) => void
}
```
Expand Down Expand Up @@ -172,4 +176,3 @@ export default {
titleTemplate: 'Vite & Vue powered static site generator'
}
```

6 changes: 4 additions & 2 deletions src/node/markdown/markdown.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import MarkdownIt from 'markdown-it'
import { Theme } from 'shiki'
import { IThemeRegistration } from 'shiki'
import { parseHeader } from '../utils/parseHeader'
import { highlight } from './plugins/highlight'
import { slugify } from './plugins/slugify'
Expand All @@ -19,7 +19,9 @@ import attrs from 'markdown-it-attrs'
import emoji from 'markdown-it-emoji'
import toc from 'markdown-it-toc-done-right'

export type ThemeOptions = Theme | { light: Theme; dark: Theme }
export type ThemeOptions =
| IThemeRegistration
| { light: IThemeRegistration; dark: IThemeRegistration }

export interface MarkdownOptions extends MarkdownIt.Options {
lineNumbers?: boolean
Expand Down
19 changes: 12 additions & 7 deletions src/node/markdown/plugins/highlight.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
import { getHighlighter } from 'shiki'
import { IThemeRegistration, getHighlighter } from 'shiki'
import type { ThemeOptions } from '../markdown'

export async function highlight(theme: ThemeOptions = 'material-palenight') {
const themes = typeof theme === 'string' ? [theme] : [theme.dark, theme.light]
const highlighter = await getHighlighter({ themes })
const hasSingleTheme = typeof theme === 'string' || 'name' in theme
const getThemeName = (themeValue: IThemeRegistration) =>
typeof themeValue === 'string' ? themeValue : themeValue.name

const highlighter = await getHighlighter({
themes: hasSingleTheme ? [theme] : [theme.dark, theme.light]
})
const preRE = /^<pre.*?>/

return (str: string, lang: string) => {
lang = lang || 'text'

if (typeof theme === 'string') {
if (hasSingleTheme) {
return highlighter
.codeToHtml(str, { lang, theme })
.codeToHtml(str, { lang, theme: getThemeName(theme) })
.replace(preRE, '<pre v-pre>')
}

const dark = highlighter
.codeToHtml(str, { lang, theme: theme.dark })
.codeToHtml(str, { lang, theme: getThemeName(theme.dark) })
.replace(preRE, '<pre v-pre class="vp-code-dark">')

const light = highlighter
.codeToHtml(str, { lang, theme: theme.light })
.codeToHtml(str, { lang, theme: getThemeName(theme.light) })
.replace(preRE, '<pre v-pre class="vp-code-light">')

return dark + light
Expand Down