Commit 4bf56acb authored by 水玉婷's avatar 水玉婷
Browse files

feat:修复md文档解析ul以及引用块bug

parent c7c89b11
...@@ -95,11 +95,15 @@ export const parseMarkdown = (text: string): string => { ...@@ -95,11 +95,15 @@ export const parseMarkdown = (text: string): string => {
} }
// 处理引用块(必须在HTML转义之前,避免>被转义为>) // 处理引用块(必须在HTML转义之前,避免>被转义为>)
// 改进的多行引用块处理,支持连续引用行 // 改进的多行引用块处理,支持连续引用行(包括开头可能有空格的引用行)
text = text.replace(/^>\s*(.*(?:\n>\s*.*)*)$/gim, (_match, content) => { text = text.replace(/^\s*>\s*(.*(?:\n\s*>\s*.*)*)$/gim, (_match, content) => {
// 将多行引用内容合并,并用<br>分隔 // 将多行引用内容合并,并用<br>分隔
const lines = content.split(/\n>\s*/); const lines = content.split(/\n\s*>\s*/);
const processedContent = lines.map((line: string) => line.trim()).join('<br/>'); // 过滤掉空行(只有空格的引用行)
const filteredLines = lines.filter((line: string) => line.trim() !== '');
// 移除每行开头可能残留的>符号
const cleanedLines = filteredLines.map((line: string) => line.replace(/^\s*>\s*/, '').trim());
const processedContent = cleanedLines.join('<br/>');
return `<blockquote>${processedContent}</blockquote>`; return `<blockquote>${processedContent}</blockquote>`;
}); });
...@@ -112,7 +116,63 @@ export const parseMarkdown = (text: string): string => { ...@@ -112,7 +116,63 @@ export const parseMarkdown = (text: string): string => {
</div>`; </div>`;
}); });
// 处理基本的Markdown格式(在表格解析之前处理,确保表格单元格中的格式也能被处理) // 处理列表的辅助函数
const processLists = (text: string): string => {
const lines = text.split('\n');
let result = '';
let currentListType: 'ol' | 'ul' | null = null;
let listItems: string[] = [];
const flushList = () => {
if (listItems.length > 0 && currentListType) {
result += `<${currentListType}>${listItems.join('')}</${currentListType}>`;
listItems = [];
currentListType = null;
}
};
for (const line of lines) {
const trimmedLine = line.trim();
// 检查有序列表项
const orderedMatch = trimmedLine.match(/^(\d+)\.\s+(.*)$/);
if (orderedMatch) {
if (currentListType !== 'ol') {
flushList();
currentListType = 'ol';
}
const originalNumber = parseInt(orderedMatch[1]);
const content = parseMarkdown(orderedMatch[2]);
// 使用原始序号作为value属性
listItems.push(`<li value="${originalNumber}">${content}</li>`);
continue;
}
// 检查无序列表项
const unorderedMatch = trimmedLine.match(/^([-*+])\s+(.*)$/);
if (unorderedMatch) {
if (currentListType !== 'ul') {
flushList();
currentListType = 'ul';
}
const content = parseMarkdown(unorderedMatch[2]);
listItems.push(`<li>${content}</li>`);
continue;
}
// 非列表行
flushList();
result += line + '\n';
}
flushList(); // 处理最后可能存在的列表
return result;
};
// 使用新的列表处理逻辑(必须在Markdown格式处理之前)
text = processLists(text);
// 处理基本的Markdown格式(在列表解析之后处理,确保列表项中的格式也能被处理)
// 使用改进的Markdown格式处理函数,避免处理HTML标签内的内容 // 使用改进的Markdown格式处理函数,避免处理HTML标签内的内容
text = processMarkdownFormat(text); text = processMarkdownFormat(text);
...@@ -182,62 +242,6 @@ export const parseMarkdown = (text: string): string => { ...@@ -182,62 +242,6 @@ export const parseMarkdown = (text: string): string => {
return `<a href="${url}" ${target}>${text}</a>`; return `<a href="${url}" ${target}>${text}</a>`;
}); });
// 处理列表的辅助函数
const processLists = (text: string): string => {
const lines = text.split('\n');
let result = '';
let currentListType: 'ol' | 'ul' | null = null;
let listItems: string[] = [];
const flushList = () => {
if (listItems.length > 0 && currentListType) {
result += `<${currentListType}>${listItems.join('')}</${currentListType}>`;
listItems = [];
currentListType = null;
}
};
for (const line of lines) {
const trimmedLine = line.trim();
// 检查有序列表项
const orderedMatch = trimmedLine.match(/^(\d+)\.\s+(.*)$/);
if (orderedMatch) {
if (currentListType !== 'ol') {
flushList();
currentListType = 'ol';
}
const originalNumber = parseInt(orderedMatch[1]);
const content = parseMarkdown(orderedMatch[2]);
// 使用原始序号作为value属性
listItems.push(`<li value="${originalNumber}">${content}</li>`);
continue;
}
// 检查无序列表项
const unorderedMatch = trimmedLine.match(/^([-*+])\s+(.*)$/);
if (unorderedMatch) {
if (currentListType !== 'ul') {
flushList();
currentListType = 'ul';
}
const content = parseMarkdown(unorderedMatch[2]);
listItems.push(`<li>${content}</li>`);
continue;
}
// 非列表行
flushList();
result += line + '\n';
}
flushList(); // 处理最后可能存在的列表
return result;
};
// 使用新的列表处理逻辑
text = processLists(text);
// 处理水平分割线 // 处理水平分割线
text = text.replace(/^\s*---\s*$/gim, '<hr />'); text = text.replace(/^\s*---\s*$/gim, '<hr />');
text = text.replace(/^\s*\*\*\*\s*$/gim, '<hr />'); text = text.replace(/^\s*\*\*\*\s*$/gim, '<hr />');
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment