Skip to content

Commit 8f8a6fe

Browse files
committed
fix(md): dont break on nesting blockquotes inside gfm alerts
closes #3512
1 parent 963b3b8 commit 8f8a6fe

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

Diff for: src/node/markdown/markdown.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ export interface MarkdownOptions extends MarkdownIt.Options {
177177
*/
178178
math?: boolean | any
179179
image?: ImageOptions
180+
/**
181+
* Allows disabling the github alerts plugin
182+
* @default true
183+
* @see https://vitepress.dev/guide/markdown#github-flavored-alerts
184+
*/
185+
gfmAlerts?: boolean
180186
}
181187

182188
export type MarkdownRenderer = MarkdownIt
@@ -209,7 +215,6 @@ export const createMarkdownRenderer = async (
209215
.use(preWrapperPlugin, { hasSingleTheme })
210216
.use(snippetPlugin, srcDir)
211217
.use(containerPlugin, { hasSingleTheme }, options.container)
212-
.use(gitHubAlertsPlugin, options.container)
213218
.use(imagePlugin, options.image)
214219
.use(
215220
linkPlugin,
@@ -218,6 +223,10 @@ export const createMarkdownRenderer = async (
218223
)
219224
.use(lineNumberPlugin, options.lineNumbers)
220225

226+
if (options.gfmAlerts !== false) {
227+
md.use(gitHubAlertsPlugin)
228+
}
229+
221230
// 3rd party plugins
222231
if (!options.attrs?.disable) {
223232
md.use(attrsPlugin, options.attrs)

Diff for: src/node/markdown/plugins/githubAlerts.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,19 @@ export const gitHubAlertsPlugin = (
2222
const tokens = state.tokens
2323
for (let i = 0; i < tokens.length; i++) {
2424
if (tokens[i].type === 'blockquote_open') {
25-
const open = tokens[i]
2625
const startIndex = i
27-
while (tokens[i]?.type !== 'blockquote_close' && i <= tokens.length)
28-
i += 1
29-
const close = tokens[i]
30-
const endIndex = i
26+
const open = tokens[startIndex]
27+
let endIndex = i + 1
28+
while (
29+
!(
30+
tokens[endIndex].type === 'blockquote_close' &&
31+
tokens[endIndex].level === open.level
32+
) &&
33+
endIndex < tokens.length
34+
)
35+
endIndex++
36+
if (endIndex === tokens.length) continue
37+
const close = tokens[endIndex]
3138
const firstContent = tokens
3239
.slice(startIndex, endIndex + 1)
3340
.find((token) => token.type === 'inline')

0 commit comments

Comments
 (0)