fix(md): dont break on nesting blockquotes inside gfm alerts

closes #3512
pull/3542/head
Divyansh Singh 5 months ago
parent 963b3b81bc
commit 8f8a6feb05

@ -177,6 +177,12 @@ export interface MarkdownOptions extends MarkdownIt.Options {
*/
math?: boolean | any
image?: ImageOptions
/**
* Allows disabling the github alerts plugin
* @default true
* @see https://vitepress.dev/guide/markdown#github-flavored-alerts
*/
gfmAlerts?: boolean
}
export type MarkdownRenderer = MarkdownIt
@ -209,7 +215,6 @@ export const createMarkdownRenderer = async (
.use(preWrapperPlugin, { hasSingleTheme })
.use(snippetPlugin, srcDir)
.use(containerPlugin, { hasSingleTheme }, options.container)
.use(gitHubAlertsPlugin, options.container)
.use(imagePlugin, options.image)
.use(
linkPlugin,
@ -218,6 +223,10 @@ export const createMarkdownRenderer = async (
)
.use(lineNumberPlugin, options.lineNumbers)
if (options.gfmAlerts !== false) {
md.use(gitHubAlertsPlugin)
}
// 3rd party plugins
if (!options.attrs?.disable) {
md.use(attrsPlugin, options.attrs)

@ -22,12 +22,19 @@ export const gitHubAlertsPlugin = (
const tokens = state.tokens
for (let i = 0; i < tokens.length; i++) {
if (tokens[i].type === 'blockquote_open') {
const open = tokens[i]
const startIndex = i
while (tokens[i]?.type !== 'blockquote_close' && i <= tokens.length)
i += 1
const close = tokens[i]
const endIndex = i
const open = tokens[startIndex]
let endIndex = i + 1
while (
!(
tokens[endIndex].type === 'blockquote_close' &&
tokens[endIndex].level === open.level
) &&
endIndex < tokens.length
)
endIndex++
if (endIndex === tokens.length) continue
const close = tokens[endIndex]
const firstContent = tokens
.slice(startIndex, endIndex + 1)
.find((token) => token.type === 'inline')

Loading…
Cancel
Save