- beautifului.dev 组件直连:Sidebar Nav、Tool Chips、Approval Card(抽屉化)、Streaming/Thinking、Code Block、Selection Actions、Prompt Bar、Search
42 lines
1.2 KiB
JavaScript
42 lines
1.2 KiB
JavaScript
// 验证 Markdown.tsx 的 parseBlocks 是否产出空内容
|
|
const INLINE = /^(\s*)([-*+]\s+|\d+[.)]\s+|>\s?|(#{1,3}\s+))?(.*)$/
|
|
|
|
function parseBlocks(text) {
|
|
const blocks = []
|
|
const lines = text.split('\n')
|
|
let current = null
|
|
const flush = () => {
|
|
if (current) blocks.push(current)
|
|
current = null
|
|
}
|
|
for (const line of lines) {
|
|
const m = line.match(INLINE)
|
|
if (!m) continue
|
|
const [, indent, marker, content] = m
|
|
const isListItem = Boolean(marker && /^([-*+]\s+|\d+[.)]\s+)/.test(marker))
|
|
const kind = marker
|
|
? marker.startsWith('#')
|
|
? 'h1'
|
|
: isListItem
|
|
? /^\d/.test(marker) ? 'ol' : 'ul'
|
|
: marker.startsWith('>') ? 'quote' : null
|
|
: 'p'
|
|
if (kind === 'p') {
|
|
if (!current || current.kind !== 'p') {
|
|
flush()
|
|
current = { kind: 'p', lines: [] }
|
|
}
|
|
current.lines.push(content)
|
|
continue
|
|
}
|
|
flush()
|
|
}
|
|
return blocks
|
|
}
|
|
|
|
for (const t of ['pong', 'Hello world', '# 标题', '- 列表项', '> 引用', '1. 有序']) {
|
|
const blocks = parseBlocks(t)
|
|
const rendered = blocks.map((b) => b.lines.join('\n'))
|
|
console.log(JSON.stringify(t), '->', JSON.stringify(rendered))
|
|
}
|