(content, fallback)
| 13 | * truncated to max 160 characters for SEO meta descriptions. |
| 14 | */ |
| 15 | export function extractDescription(content, fallback) { |
| 16 | const paragraphLines = []; |
| 17 | let started = false; |
| 18 | for (const line of content.split('\n')) { |
| 19 | const trimmed = line.trim(); |
| 20 | if (!started) { |
| 21 | if (trimmed && !trimmed.startsWith('#') && !trimmed.startsWith('---')) { |
| 22 | started = true; |
| 23 | paragraphLines.push(trimmed); |
| 24 | } |
| 25 | } else { |
| 26 | if (!trimmed || trimmed.startsWith('#')) break; |
| 27 | paragraphLines.push(trimmed); |
| 28 | } |
| 29 | } |
| 30 | const firstParagraph = paragraphLines.join(' ').replace(/\s+/g, ' ').trim(); |
| 31 | |
| 32 | if (firstParagraph.length > 10 && firstParagraph.length <= 160) { |
| 33 | return firstParagraph; |
| 34 | } |
| 35 | if (firstParagraph.length > 160) { |
| 36 | const truncated = firstParagraph.substring(0, 157); |
| 37 | const lastSpace = truncated.lastIndexOf(' '); |
| 38 | return (lastSpace > 100 ? truncated.substring(0, lastSpace) : truncated) + '...'; |
| 39 | } |
| 40 | return fallback; |
| 41 | } |
| 42 | |
| 43 | /** Escape special characters for safe inclusion in YAML frontmatter values. */ |
| 44 | export function escapeYaml(value) { |
no test coverage detected