Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 5x 5x 63x 63x 85x 85x 85x 85x 85x 21x 21x 21x 11x 64x 2x 62x 73x | export type Block =
{ type: 'paragraph'; text: string } | { type: 'list'; isOrdered: boolean; items: string[] }
const BULLET = /^[-*] (.*)$/
const NUMBERED = /^\d+\. (.*)$/
// Splits a body into paragraphs and lists: "- item" lines make a bulleted list, "1. item" lines a
// numbered one. Each text is still raw; parseInline formats it.
// shortcut: no nesting, quotes or code blocks; MSG-1 in M2 brings the full Markdown subset.
export function parseBlocks(body: string): Block[] {
const blocks: Block[] = []
for (const line of body.split('\n')) {
const bullet = BULLET.exec(line)
const numbered = NUMBERED.exec(line)
const item = bullet ?? numbered
const last = blocks.at(-1)
if (item) {
const isOrdered = numbered !== null
const text = item[1] ?? ''
if (last?.type === 'list' && last.isOrdered === isOrdered) last.items.push(text)
else blocks.push({ type: 'list', isOrdered, items: [text] })
} else if (last?.type === 'paragraph') {
last.text += `\n${line}`
} else {
blocks.push({ type: 'paragraph', text: line })
}
}
return blocks.filter((block) => block.type === 'list' || block.text.trim() !== '')
}
|