Skip to content

Commit bdb0800

Browse files
authoredOct 7, 2023
feat(md): allow customizing container titles globally (#3044)
1 parent 2bf0d0b commit bdb0800

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed
 

‎src/node/markdown/markdown.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import attrsPlugin from 'markdown-it-attrs'
2020
import emojiPlugin from 'markdown-it-emoji'
2121
import type { ILanguageRegistration, IThemeRegistration } from 'shiki'
2222
import type { Logger } from 'vite'
23-
import { containerPlugin } from './plugins/containers'
23+
import { containerPlugin, type ContainerOptions } from './plugins/containers'
2424
import { highlight } from './plugins/highlight'
2525
import { highlightLinePlugin } from './plugins/highlightLines'
2626
import { imagePlugin } from './plugins/image'
@@ -57,6 +57,7 @@ export interface MarkdownOptions extends MarkdownIt.Options {
5757
cache?: boolean
5858
component?: ComponentPluginOptions
5959
math?: boolean | any
60+
container?: ContainerOptions
6061
}
6162

6263
export type MarkdownRenderer = MarkdownIt
@@ -95,7 +96,7 @@ export const createMarkdownRenderer = async (
9596
.use(highlightLinePlugin)
9697
.use(preWrapperPlugin, { hasSingleTheme })
9798
.use(snippetPlugin, srcDir)
98-
.use(containerPlugin, { hasSingleTheme })
99+
.use(containerPlugin, { hasSingleTheme }, options.container)
99100
.use(imagePlugin)
100101
.use(
101102
linkPlugin,

‎src/node/markdown/plugins/containers.ts

+36-6
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,34 @@ import {
99
type Options
1010
} from './preWrapper'
1111

12-
export const containerPlugin = (md: MarkdownIt, options: Options) => {
13-
md.use(...createContainer('tip', 'TIP', md))
14-
.use(...createContainer('info', 'INFO', md))
15-
.use(...createContainer('warning', 'WARNING', md))
16-
.use(...createContainer('danger', 'DANGER', md))
17-
.use(...createContainer('details', 'Details', md))
12+
export const containerPlugin = (
13+
md: MarkdownIt,
14+
options: Options,
15+
containerOptions?: ContainerOptions
16+
) => {
17+
md.use(...createContainer('tip', containerOptions?.tipLabel || 'TIP', md))
18+
.use(...createContainer('info', containerOptions?.infoLabel || 'INFO', md))
19+
.use(
20+
...createContainer(
21+
'warning',
22+
containerOptions?.warningLabel || 'WARNING',
23+
md
24+
)
25+
)
26+
.use(
27+
...createContainer(
28+
'danger',
29+
containerOptions?.dangerLabel || 'DANGER',
30+
md
31+
)
32+
)
33+
.use(
34+
...createContainer(
35+
'details',
36+
containerOptions?.detailsLabel || 'Details',
37+
md
38+
)
39+
)
1840
// explicitly escape Vue syntax
1941
.use(container, 'v-pre', {
2042
render: (tokens: Token[], idx: number) =>
@@ -104,3 +126,11 @@ function createCodeGroup(options: Options): ContainerArgs {
104126
}
105127
]
106128
}
129+
130+
export interface ContainerOptions {
131+
infoLabel?: string
132+
tipLabel?: string
133+
warningLabel?: string
134+
dangerLabel?: string
135+
detailsLabel?: string
136+
}

0 commit comments

Comments
 (0)
Please sign in to comment.