xiaoshuo / ui-src /src /md.tsx
root
AI 协作室、采集室门锁、AI 执行自查与平台检查、热榜原文人机共读、审查 AI 设置
0399aa9
Raw History Blame Contribute Delete
3.97 kB
import React from 'react'
// 轻量 Markdown 排版:给人看的「AI 守则」「热榜原文」等,同一份文字 AI 也直接读。
function inline(s: string, key = ''): React.ReactNode[] {
const out: React.ReactNode[] = []
const re = /(\*\*([^*]+)\*\*|`([^`]+)`|\[([^\]]+)\]\(([^)\s]+)\))/g
let last = 0, m: RegExpExecArray | null, i = 0
while ((m = re.exec(s))) {
if (m.index > last) out.push(s.slice(last, m.index))
if (m[2]) out.push(<b key={key + i++}>{m[2]}</b>)
else if (m[3]) out.push(<code key={key + i++}>{m[3]}</code>)
else if (m[4]) out.push(<a key={key + i++} href={m[5]} target="_blank" rel="noreferrer">{m[4]}</a>)
last = m.index + m[0].length
}
if (last < s.length) out.push(s.slice(last))
return out
}
type Li = { text: string; kids: Li[] }
function parseList(lines: string[]): { ordered: boolean; items: Li[] } {
const ordered = /^\s*\d+[.、]\s/.test(lines[0])
const items: Li[] = []
for (const l of lines) {
const ind = l.match(/^\s*/)![0].length
const text = l.replace(/^\s*(?:[-*]|\d+[.、])\s+/, '')
if (ind >= 2 && items.length) items[items.length - 1].kids.push({ text, kids: [] })
else items.push({ text, kids: [] })
}
return { ordered, items }
}
export function Md({ text, className }: { text: string; className?: string }) {
const lines = String(text || '').replace(/\r/g, '').split('\n')
const blocks: React.ReactNode[] = []
let i = 0, k = 0
const isList = (l: string) => /^\s*(?:[-*]|\d+[.、])\s+/.test(l)
while (i < lines.length) {
const l = lines[i]
if (!l.trim()) { i++; continue }
const h = l.match(/^(#{1,4})\s+(.*)$/)
if (h) { const T = (`h${Math.min(4, h[1].length + 1)}`) as any; blocks.push(<T key={k++}>{inline(h[2])}</T>); i++; continue }
if (/^-{3,}\s*$/.test(l)) { blocks.push(<hr key={k++} />); i++; continue }
if (l.startsWith('```')) {
const buf: string[] = []; i++
while (i < lines.length && !lines[i].startsWith('```')) buf.push(lines[i++])
i++; blocks.push(<pre key={k++}>{buf.join('\n')}</pre>); continue
}
if (l.startsWith('>')) {
const buf: string[] = []
while (i < lines.length && lines[i].startsWith('>')) buf.push(lines[i++].replace(/^>\s?/, ''))
blocks.push(<blockquote key={k++}>{inline(buf.join(' '))}</blockquote>); continue
}
if (l.trim().startsWith('|')) {
const rows: string[][] = []
while (i < lines.length && lines[i].trim().startsWith('|')) {
const cells = lines[i].trim().replace(/^\||\|$/g, '').split('|').map(c => c.trim())
if (!cells.every(c => /^:?-{2,}:?$/.test(c))) rows.push(cells)
i++
}
const [head, ...body] = rows
blocks.push(
<div className="md-table" key={k++}><table>
<thead><tr>{head.map((c, j) => <th key={j}>{inline(c)}</th>)}</tr></thead>
<tbody>{body.map((r, a) => <tr key={a}>{r.map((c, j) => <td key={j}>{inline(c)}</td>)}</tr>)}</tbody>
</table></div>)
continue
}
if (isList(l)) {
const buf: string[] = []
while (i < lines.length && (isList(lines[i]) || (/^\s{2,}\S/.test(lines[i]) && buf.length))) buf.push(lines[i++])
const { ordered, items } = parseList(buf)
const L = ordered ? 'ol' : 'ul'
const start = ordered ? Number(buf[0].match(/\d+/)![0]) : undefined
blocks.push(React.createElement(L, { key: k++, start }, items.map((it, j) => (
<li key={j}>{inline(it.text)}{it.kids.length > 0 && <ul>{it.kids.map((c, q) => <li key={q}>{inline(c.text)}</li>)}</ul>}</li>
))))
continue
}
const buf: string[] = []
while (i < lines.length && lines[i].trim() && !/^(#{1,4}\s|>|\||```|-{3,}\s*$)/.test(lines[i]) && !isList(lines[i])) buf.push(lines[i++])
blocks.push(<p key={k++}>{buf.map((b, j) => <React.Fragment key={j}>{j > 0 && <br />}{inline(b, `${j}-`)}</React.Fragment>)}</p>)
}
return <div className={`md ${className || ''}`}>{blocks}</div>
}