From 3a51b4c0d79fafac83d671a76e498543b3449066 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?wifi=E6=AD=AAf?= <1402772884@qq.com> Date: Tue, 4 Feb 2025 00:29:44 +0800 Subject: [PATCH 1/2] =?UTF-8?q?chore(mini-markdown-ast-parser):=20?= =?UTF-8?q?=E4=BF=AE=E6=94=B9ts=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/core/parse/compose/blocks/html.ts | 2 +- .../mini-markdown-ast-parser/src/core/parse/compose/index.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/mini-markdown-ast-parser/src/core/parse/compose/blocks/html.ts b/packages/mini-markdown-ast-parser/src/core/parse/compose/blocks/html.ts index 24aaec6..3211e0d 100644 --- a/packages/mini-markdown-ast-parser/src/core/parse/compose/blocks/html.ts +++ b/packages/mini-markdown-ast-parser/src/core/parse/compose/blocks/html.ts @@ -91,7 +91,7 @@ export const parseHtml = ({ } currentStatus.inHtmlBlock = true; - currentStatus.htmlBlockTag = tagName; // 记录最外层的标签名 + currentStatus.htmlBlockTag = tagName || null; // 记录最外层的标签名 // 添加 data-line 属性 const dataLineAttr = ` data-line="${index + 1}"`; diff --git a/packages/mini-markdown-ast-parser/src/core/parse/compose/index.ts b/packages/mini-markdown-ast-parser/src/core/parse/compose/index.ts index 4a2b781..1c7b7f9 100644 --- a/packages/mini-markdown-ast-parser/src/core/parse/compose/index.ts +++ b/packages/mini-markdown-ast-parser/src/core/parse/compose/index.ts @@ -48,7 +48,7 @@ export type ParseFnParams = { currentTable: Tokens | null; htmlContent: string; inHtmlBlock: boolean; - htmlBlockTag: string; + htmlBlockTag: string | null; }; resetCurrentStatus: () => void; }; -- Gitee From fa0670cfd3016f518b2d2033763183f186cb2400 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?wifi=E6=AD=AAf?= <1402772884@qq.com> Date: Tue, 4 Feb 2025 00:52:59 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix(mini-markdown-ast-parser):=20=E8=BF=98?= =?UTF-8?q?=E6=98=AFhtml=E8=A7=A3=E6=9E=90=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/core/parse/compose/blocks/html.ts | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/packages/mini-markdown-ast-parser/src/core/parse/compose/blocks/html.ts b/packages/mini-markdown-ast-parser/src/core/parse/compose/blocks/html.ts index 3211e0d..115d032 100644 --- a/packages/mini-markdown-ast-parser/src/core/parse/compose/blocks/html.ts +++ b/packages/mini-markdown-ast-parser/src/core/parse/compose/blocks/html.ts @@ -31,6 +31,8 @@ export const parseHtml = ({ const htmlBlockStartRegex = /^\s*<([a-zA-Z][a-zA-Z0-9]*)(?![^>]*\/>)[^>]*>/; const htmlBlockEndRegex = /<\/([a-zA-Z][a-zA-Z0-9]*)>\s*$/; const selfClosingTagRegex = /^\s*<([a-zA-Z][a-zA-Z0-9]*)[^>]*\/>\s*$/; + // 添加新的单标签正则 + const voidElementRegex = /^\s*<([a-zA-Z][a-zA-Z0-9]*)[^>]*>/; // 如果在HTML块内,优先处理内容 if (currentStatus.inHtmlBlock) { @@ -63,14 +65,22 @@ export const parseHtml = ({ } // 检查是否是HTML块的开始 - if (!currentStatus.inHtmlBlock && htmlBlockStartRegex.test(trimmedLine)) { - const match = trimmedLine.match(htmlBlockStartRegex); - const tagName = match?.[1].toLowerCase(); + if ( + !currentStatus.inHtmlBlock && + (htmlBlockStartRegex.test(trimmedLine) || selfClosingTagRegex.test(trimmedLine)) + ) { + // 优先检查是否是单标签 + const voidMatch = trimmedLine.match(voidElementRegex); + const tagName = voidMatch?.[1].toLowerCase(); // 如果是单标签,直接处理并返回 if (tagName && voidElements.has(tagName)) { const dataLineAttr = ` data-line="${index + 1}"`; - const content = line.replace(/>/, `${dataLineAttr}>`); + // 处理自闭合和非自闭合两种情况 + const content = selfClosingTagRegex.test(trimmedLine) + ? line.replace("/>", `${dataLineAttr} />`) + : line.replace(/>/, `${dataLineAttr}>`); + root.children.push({ type: "html", value: content.trim(), -- Gitee