Skip to content

Commit

Permalink
feat(md): allow customizing container titles globally (#3044)
Browse files Browse the repository at this point in the history
  • Loading branch information
djdjz7 authored Oct 7, 2023
1 parent 2bf0d0b commit bdb0800
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/node/markdown/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import attrsPlugin from 'markdown-it-attrs'
import emojiPlugin from 'markdown-it-emoji'
import type { ILanguageRegistration, IThemeRegistration } from 'shiki'
import type { Logger } from 'vite'
import { containerPlugin } from './plugins/containers'
import { containerPlugin, type ContainerOptions } from './plugins/containers'
import { highlight } from './plugins/highlight'
import { highlightLinePlugin } from './plugins/highlightLines'
import { imagePlugin } from './plugins/image'
Expand Down Expand Up @@ -57,6 +57,7 @@ export interface MarkdownOptions extends MarkdownIt.Options {
cache?: boolean
component?: ComponentPluginOptions
math?: boolean | any
container?: ContainerOptions
}

export type MarkdownRenderer = MarkdownIt
Expand Down Expand Up @@ -95,7 +96,7 @@ export const createMarkdownRenderer = async (
.use(highlightLinePlugin)
.use(preWrapperPlugin, { hasSingleTheme })
.use(snippetPlugin, srcDir)
.use(containerPlugin, { hasSingleTheme })
.use(containerPlugin, { hasSingleTheme }, options.container)
.use(imagePlugin)
.use(
linkPlugin,
Expand Down
42 changes: 36 additions & 6 deletions src/node/markdown/plugins/containers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,34 @@ import {
type Options
} from './preWrapper'

export const containerPlugin = (md: MarkdownIt, options: Options) => {
md.use(...createContainer('tip', 'TIP', md))
.use(...createContainer('info', 'INFO', md))
.use(...createContainer('warning', 'WARNING', md))
.use(...createContainer('danger', 'DANGER', md))
.use(...createContainer('details', 'Details', md))
export const containerPlugin = (
md: MarkdownIt,
options: Options,
containerOptions?: ContainerOptions
) => {
md.use(...createContainer('tip', containerOptions?.tipLabel || 'TIP', md))
.use(...createContainer('info', containerOptions?.infoLabel || 'INFO', md))
.use(
...createContainer(
'warning',
containerOptions?.warningLabel || 'WARNING',
md
)
)
.use(
...createContainer(
'danger',
containerOptions?.dangerLabel || 'DANGER',
md
)
)
.use(
...createContainer(
'details',
containerOptions?.detailsLabel || 'Details',
md
)
)
// explicitly escape Vue syntax
.use(container, 'v-pre', {
render: (tokens: Token[], idx: number) =>
Expand Down Expand Up @@ -104,3 +126,11 @@ function createCodeGroup(options: Options): ContainerArgs {
}
]
}

export interface ContainerOptions {
infoLabel?: string
tipLabel?: string
warningLabel?: string
dangerLabel?: string
detailsLabel?: string
}

0 comments on commit bdb0800

Please sign in to comment.