From 8f8a6feb053b3f521a2c90e343dffa7f98bb63b3 Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Sun, 4 Feb 2024 00:44:51 +0530 Subject: [PATCH 1/2] fix(md): dont break on nesting blockquotes inside gfm alerts closes #3512 --- src/node/markdown/markdown.ts | 11 ++++++++++- src/node/markdown/plugins/githubAlerts.ts | 17 ++++++++++++----- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/node/markdown/markdown.ts b/src/node/markdown/markdown.ts index 61acb9db..9554ad7f 100644 --- a/src/node/markdown/markdown.ts +++ b/src/node/markdown/markdown.ts @@ -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) diff --git a/src/node/markdown/plugins/githubAlerts.ts b/src/node/markdown/plugins/githubAlerts.ts index fd084f59..48047b19 100644 --- a/src/node/markdown/plugins/githubAlerts.ts +++ b/src/node/markdown/plugins/githubAlerts.ts @@ -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') From 67a9964c4e6ffdb0e644624d075b8dd20f477686 Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Sun, 4 Feb 2024 00:55:36 +0530 Subject: [PATCH 2/2] chore: fix typo --- src/node/markdown/plugins/githubAlerts.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/node/markdown/plugins/githubAlerts.ts b/src/node/markdown/plugins/githubAlerts.ts index 48047b19..817002ed 100644 --- a/src/node/markdown/plugins/githubAlerts.ts +++ b/src/node/markdown/plugins/githubAlerts.ts @@ -26,11 +26,9 @@ export const gitHubAlertsPlugin = ( const open = tokens[startIndex] let endIndex = i + 1 while ( - !( - tokens[endIndex].type === 'blockquote_close' && - tokens[endIndex].level === open.level - ) && - endIndex < tokens.length + endIndex < tokens.length && + (tokens[endIndex].type !== 'blockquote_close' || + tokens[endIndex].level !== open.level) ) endIndex++ if (endIndex === tokens.length) continue