|
|
|
@@ -189,41 +189,8 @@ const MarkdownContent: React.FC<{ content: string }> = ({ content }) => {
|
|
|
|
|
return '\x00MEDIA' + (mediaBlocks.length - 1) + '\x00';
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 2.7 Extract Markdown tables: | h1 | h2 |\n|---|---| ... |
|
|
|
|
|
// 把整段表格替换为占位符,表格内容(已转义)放入 tableBlocks 数组
|
|
|
|
|
const tableBlocks: string[] = [];
|
|
|
|
|
text = text.replace(
|
|
|
|
|
/((?:^\|.+\|\r?\n)+^\|[\s\-:|]+\|\r?\n(?:^\|.+\|\r?\n?)+)/gm,
|
|
|
|
|
(_m: string, block: string) => {
|
|
|
|
|
const lines = block.trim().split(/\r?\n/).filter((l: string) => l.trim());
|
|
|
|
|
if (lines.length < 2) return block;
|
|
|
|
|
const headerCells = lines[0].split('|').slice(1, -1).map((c: string) => c.trim());
|
|
|
|
|
const alignCells = lines[1].split('|').slice(1, -1).map((c: string) => {
|
|
|
|
|
const t = c.trim();
|
|
|
|
|
const left = t.startsWith(':');
|
|
|
|
|
const right = t.endsWith(':');
|
|
|
|
|
if (left && right) return 'center';
|
|
|
|
|
if (right) return 'right';
|
|
|
|
|
if (left) return 'left';
|
|
|
|
|
return 'left';
|
|
|
|
|
});
|
|
|
|
|
const bodyRows = lines.slice(2).map((row: string) =>
|
|
|
|
|
row.split('|').slice(1, -1).map((c: string) => c.trim())
|
|
|
|
|
);
|
|
|
|
|
const thead = headerCells.map((c: string, i: number) =>
|
|
|
|
|
`<th style="text-align:${alignCells[i] || 'left'}">${c}</th>`
|
|
|
|
|
).join('');
|
|
|
|
|
const tbody = bodyRows.map((r: string[]) =>
|
|
|
|
|
'<tr>' + r.map((c: string, i: number) =>
|
|
|
|
|
`<td style="text-align:${alignCells[i] || 'left'}">${c}</td>`
|
|
|
|
|
).join('') + '</tr>'
|
|
|
|
|
).join('');
|
|
|
|
|
const html = `<table class="md-table"><thead><tr>${thead}</tr></thead><tbody>${tbody}</tbody></table>`;
|
|
|
|
|
const idx = tableBlocks.length;
|
|
|
|
|
tableBlocks.push(html);
|
|
|
|
|
return '\x00TABLE' + idx + '\x00';
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
// 2.6 Normalize excessive blank lines in prose (code blocks already extracted)
|
|
|
|
|
text = text.replace(/\n{3,}/g, '\n\n').trim();
|
|
|
|
|
|
|
|
|
|
// 2.8 LaTeX 数学公式:$$...$$(块级)和 $...$(行内)
|
|
|
|
|
// 提取为占位符,渲染后用 KaTeX 处理
|
|
|
|
@@ -273,15 +240,58 @@ const MarkdownContent: React.FC<{ content: string }> = ({ content }) => {
|
|
|
|
|
text = text.replace(/\*\*(.+?)\*\*/g, '<strong>$1</strong>');
|
|
|
|
|
text = text.replace(/(?<!\w)\*(?!\*)(.+?)(?<!\*)\*(?!\w)/g, '<em>$1</em>');
|
|
|
|
|
|
|
|
|
|
// 7.5 Extract Markdown tables after inline formatting so cells support bold/italic/links.
|
|
|
|
|
// 表格提取放在加粗/链接之后,避免单元格内 **xxx** 原样显示;同时压缩单元格内多余 <br>。
|
|
|
|
|
const tableBlocks: string[] = [];
|
|
|
|
|
text = text.replace(
|
|
|
|
|
/((?:^\|.+\|\r?\n)+^\|[\s\-:|]+\|\r?\n(?:^\|.+\|\r?\n?)+)/gm,
|
|
|
|
|
(_m: string, block: string) => {
|
|
|
|
|
const lines = block.trim().split(/\r?\n/).filter((l: string) => l.trim());
|
|
|
|
|
if (lines.length < 2) return block;
|
|
|
|
|
const headerCells = lines[0].split('|').slice(1, -1).map((c: string) => c.trim());
|
|
|
|
|
const alignCells = lines[1].split('|').slice(1, -1).map((c: string) => {
|
|
|
|
|
const t = c.trim();
|
|
|
|
|
const left = t.startsWith(':');
|
|
|
|
|
const right = t.endsWith(':');
|
|
|
|
|
if (left && right) return 'center';
|
|
|
|
|
if (right) return 'right';
|
|
|
|
|
if (left) return 'left';
|
|
|
|
|
return 'left';
|
|
|
|
|
});
|
|
|
|
|
const normalizeCell = (cell: string) => {
|
|
|
|
|
return cell
|
|
|
|
|
.replace(/<br\s*\/?>\s*/gi, '<br/>')
|
|
|
|
|
.replace(/\n/g, '<br/>')
|
|
|
|
|
.replace(/(<br\/>){2,}/g, '<br/>')
|
|
|
|
|
.replace(/^(<br\/>)+|(<br\/>)+$/g, '');
|
|
|
|
|
};
|
|
|
|
|
const bodyRows = lines.slice(2).map((row: string) =>
|
|
|
|
|
row.split('|').slice(1, -1).map((c: string) => normalizeCell(c.trim()))
|
|
|
|
|
);
|
|
|
|
|
const thead = headerCells.map((c: string, i: number) =>
|
|
|
|
|
`<th style="text-align:${alignCells[i] || 'left'}">${normalizeCell(c)}</th>`
|
|
|
|
|
).join('');
|
|
|
|
|
const tbody = bodyRows.map((r: string[]) =>
|
|
|
|
|
'<tr>' + r.map((c: string, i: number) =>
|
|
|
|
|
`<td style="text-align:${alignCells[i] || 'left'}">${c}</td>`
|
|
|
|
|
).join('') + '</tr>'
|
|
|
|
|
).join('');
|
|
|
|
|
const html = `<table class="md-table"><thead><tr>${thead}</tr></thead><tbody>${tbody}</tbody></table>`;
|
|
|
|
|
const idx = tableBlocks.length;
|
|
|
|
|
tableBlocks.push(html);
|
|
|
|
|
return '\x00TABLE' + idx + '\x00';
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// 8. Lists
|
|
|
|
|
text = text.replace(/^\s*[-*] (.+)$/gm, '<li class="md-li">$1</li>');
|
|
|
|
|
text = text.replace(/^\s*\d+\. (.+)$/gm, '<li class="md-li">$1</li>');
|
|
|
|
|
// Wrap consecutive <li> in <ul>
|
|
|
|
|
text = text.replace(/((?:<li class="md-li">[\s\S]*?<\/li>\n?)+)/g, '<ul class="md-ul">$1</ul>');
|
|
|
|
|
|
|
|
|
|
// 9. Convert double newlines to paragraph breaks, single to <br/>
|
|
|
|
|
// 9. Convert double newlines to paragraph breaks, single newlines to spaces
|
|
|
|
|
text = text.replace(/\n\n/g, '</p><p class="md-p">');
|
|
|
|
|
text = text.replace(/\n/g, '<br/>');
|
|
|
|
|
text = text.replace(/\n/g, ' ');
|
|
|
|
|
|
|
|
|
|
// 10. Restore code blocks and inline codes
|
|
|
|
|
text = text.replace(/\x00CODEBLOCK(\d+)\x00/g, (_m: string, idx: string) => codeBlocks[parseInt(idx)] || '');
|
|
|
|
|