Build a short plaintext preview from markdown.
(body: string)
| 1590 | |
| 1591 | /** Build a short plaintext preview from markdown. */ |
| 1592 | function buildExcerpt(body: string): string { |
| 1593 | const withoutFront = body.startsWith('---\n') ? body.replace(/^---\n[\s\S]*?\n---\n/, '') : body |
| 1594 | let text = stripCodeContent(withoutFront) |
| 1595 | if (text.includes('](')) { |
| 1596 | text = text |
| 1597 | .replace(/!\[[^\]]*\]\([^)]*\)/g, ' ') |
| 1598 | .replace(/\[([^\]]+)\]\([^)]*\)/g, '$1') |
| 1599 | } |
| 1600 | if (text.includes('![[')) { |
| 1601 | text = text.replace(/!\[\[([^\]|]+)(?:\|([^\]]+))?\]\]/g, (_, a, b) => b || a) |
| 1602 | } |
| 1603 | if (text.includes('[[')) { |
| 1604 | text = text.replace(/\[\[([^\]|]+)(?:\|([^\]]+))?\]\]/g, (_, a, b) => b || a) |
| 1605 | } |
| 1606 | if (text.includes('#')) text = text.replace(/^#{1,6}\s+/gm, '') |
| 1607 | if (/[*_~>]/.test(text)) text = text.replace(/[*_~>]+/g, '') |
| 1608 | text = text.replace(/\s+/g, ' ').trim() |
| 1609 | return text.slice(0, 220) |
| 1610 | } |
| 1611 | |
| 1612 | function escapeRegex(value: string): string { |
| 1613 | return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') |
no test coverage detected