|
@@ -259,12 +259,13 @@ const MarkdownParser = ({ content }: MarkdownParserProps) => {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
- // 处理无序列表(支持嵌套)
|
|
|
|
- const ulMatch = line.match(/^\s*[-*+]\s+/);
|
|
|
|
- if (ulMatch) {
|
|
|
|
|
|
+ // 处理无序列表和有序列表(统一显示为无序列表)
|
|
|
|
+ const listMatch = line.match(/^\s*([-*+]|\d+\.)\s+/);
|
|
|
|
+ if (listMatch) {
|
|
const depth = getIndentDepth(line); // 当前列表项的缩进深度
|
|
const depth = getIndentDepth(line); // 当前列表项的缩进深度
|
|
- const text = line.replace(/^\s*[-*+]\s+/, ''); // 提取文本内容
|
|
|
|
- const itemKey = getStableKey(`ul-item-${text}`, lineIndex);
|
|
|
|
|
|
+ // 提取文本内容(移除列表标记)
|
|
|
|
+ const text = line.replace(/^\s*([-*+]|\d+\.)\s+/, '');
|
|
|
|
+ const itemKey = getStableKey(`list-item-${text}`, lineIndex);
|
|
|
|
|
|
// 创建新列表项
|
|
// 创建新列表项
|
|
const newItem: ListItem = {
|
|
const newItem: ListItem = {
|
|
@@ -286,7 +287,7 @@ const MarkdownParser = ({ content }: MarkdownParserProps) => {
|
|
lastParentItem.children.push(poppedLevel);
|
|
lastParentItem.children.push(poppedLevel);
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
- // 如果栈为空,直接渲染弹出的层级(这种情况不应该发生)
|
|
|
|
|
|
+ // 如果栈为空,直接渲染弹出的层级
|
|
const listKey = getStableKey(`unexpected-list-${poppedLevel.type}-${poppedLevel.depth}`);
|
|
const listKey = getStableKey(`unexpected-list-${poppedLevel.type}-${poppedLevel.depth}`);
|
|
elements.push(
|
|
elements.push(
|
|
<View key={listKey} className={`markdown-list markdown-${poppedLevel.type} depth-${poppedLevel.depth}`}>
|
|
<View key={listKey} className={`markdown-list markdown-${poppedLevel.type} depth-${poppedLevel.depth}`}>
|
|
@@ -296,10 +297,10 @@ const MarkdownParser = ({ content }: MarkdownParserProps) => {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- // 2. 如果栈为空或当前深度大于栈顶深度,创建新层级
|
|
|
|
|
|
+ // 2. 如果栈为空或当前深度大于栈顶深度,创建新层级(统一使用ul类型)
|
|
if (listStack.length === 0 || depth > listStack[listStack.length - 1].depth) {
|
|
if (listStack.length === 0 || depth > listStack[listStack.length - 1].depth) {
|
|
listStack.push({
|
|
listStack.push({
|
|
- type: 'ul',
|
|
|
|
|
|
+ type: 'ul', // 所有列表都显示为无序列表
|
|
items: [newItem],
|
|
items: [newItem],
|
|
depth
|
|
depth
|
|
});
|
|
});
|
|
@@ -310,51 +311,6 @@ const MarkdownParser = ({ content }: MarkdownParserProps) => {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
- // 处理有序列表(支持嵌套)
|
|
|
|
- const olMatch = line.match(/^\s*\d+\.\s+/);
|
|
|
|
- if (olMatch) {
|
|
|
|
- const depth = getIndentDepth(line);
|
|
|
|
- const text = line.replace(/^\s*\d+\.\s+/, '');
|
|
|
|
- const itemKey = getStableKey(`ol-item-${text}`, lineIndex);
|
|
|
|
-
|
|
|
|
- const newItem: ListItem = {
|
|
|
|
- content: <RichText nodes={parseInlineToNodes(text)} />,
|
|
|
|
- key: itemKey
|
|
|
|
- };
|
|
|
|
-
|
|
|
|
- while (listStack.length > depth) {
|
|
|
|
- const poppedLevel = listStack.pop()!;
|
|
|
|
- if (listStack.length > 0) {
|
|
|
|
- const parentLevel = listStack[listStack.length - 1];
|
|
|
|
- if (parentLevel.items.length > 0) {
|
|
|
|
- const lastParentItem = parentLevel.items[parentLevel.items.length - 1];
|
|
|
|
- if (!lastParentItem.children) {
|
|
|
|
- lastParentItem.children = [];
|
|
|
|
- }
|
|
|
|
- lastParentItem.children.push(poppedLevel);
|
|
|
|
- }
|
|
|
|
- } else {
|
|
|
|
- const listKey = getStableKey(`unexpected-list-${poppedLevel.type}-${poppedLevel.depth}`);
|
|
|
|
- elements.push(
|
|
|
|
- <View key={listKey} className={`markdown-list markdown-${poppedLevel.type} depth-${poppedLevel.depth}`}>
|
|
|
|
- {renderListItems(poppedLevel.items, 0)}
|
|
|
|
- </View>
|
|
|
|
- );
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (listStack.length === 0 || depth > listStack[listStack.length - 1].depth) {
|
|
|
|
- listStack.push({
|
|
|
|
- type: 'ol',
|
|
|
|
- items: [newItem],
|
|
|
|
- depth
|
|
|
|
- });
|
|
|
|
- } else {
|
|
|
|
- listStack[listStack.length - 1].items.push(newItem);
|
|
|
|
- }
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
// 处理引用
|
|
// 处理引用
|
|
const quoteMatch = line.match(/^\s*>\s+/);
|
|
const quoteMatch = line.match(/^\s*>\s+/);
|
|
if (quoteMatch) {
|
|
if (quoteMatch) {
|
|
@@ -403,4 +359,4 @@ const MarkdownParser = ({ content }: MarkdownParserProps) => {
|
|
);
|
|
);
|
|
};
|
|
};
|
|
|
|
|
|
-export default MarkdownParser;
|
|
|
|
|
|
+export default MarkdownParser;
|