|
1 | 1 | import { Block } from '../types'; |
2 | 2 |
|
| 3 | +/** |
| 4 | + * Parsed YAML frontmatter as key-value pairs. |
| 5 | + */ |
| 6 | +export interface Frontmatter { |
| 7 | + [key: string]: string | string[]; |
| 8 | +} |
| 9 | + |
| 10 | +/** |
| 11 | + * Extract YAML frontmatter from markdown if present. |
| 12 | + * Returns both the parsed frontmatter and the remaining markdown. |
| 13 | + */ |
| 14 | +export function extractFrontmatter(markdown: string): { frontmatter: Frontmatter | null; content: string } { |
| 15 | + const trimmed = markdown.trimStart(); |
| 16 | + if (!trimmed.startsWith('---')) { |
| 17 | + return { frontmatter: null, content: markdown }; |
| 18 | + } |
| 19 | + |
| 20 | + // Find the closing --- |
| 21 | + const endIndex = trimmed.indexOf('\n---', 3); |
| 22 | + if (endIndex === -1) { |
| 23 | + return { frontmatter: null, content: markdown }; |
| 24 | + } |
| 25 | + |
| 26 | + // Extract frontmatter content (between the --- delimiters) |
| 27 | + const frontmatterRaw = trimmed.slice(4, endIndex).trim(); |
| 28 | + const afterFrontmatter = trimmed.slice(endIndex + 4).trimStart(); |
| 29 | + |
| 30 | + // Parse simple YAML (key: value pairs) |
| 31 | + const frontmatter: Frontmatter = {}; |
| 32 | + let currentKey: string | null = null; |
| 33 | + let currentArray: string[] | null = null; |
| 34 | + |
| 35 | + for (const line of frontmatterRaw.split('\n')) { |
| 36 | + const trimmedLine = line.trim(); |
| 37 | + |
| 38 | + // Array item (- value) |
| 39 | + if (trimmedLine.startsWith('- ') && currentKey) { |
| 40 | + const value = trimmedLine.slice(2).trim(); |
| 41 | + if (!currentArray) { |
| 42 | + currentArray = []; |
| 43 | + frontmatter[currentKey] = currentArray; |
| 44 | + } |
| 45 | + currentArray.push(value); |
| 46 | + continue; |
| 47 | + } |
| 48 | + |
| 49 | + // Key: value pair |
| 50 | + const colonIndex = trimmedLine.indexOf(':'); |
| 51 | + if (colonIndex > 0) { |
| 52 | + currentKey = trimmedLine.slice(0, colonIndex).trim(); |
| 53 | + const value = trimmedLine.slice(colonIndex + 1).trim(); |
| 54 | + currentArray = null; |
| 55 | + |
| 56 | + if (value) { |
| 57 | + frontmatter[currentKey] = value; |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return { frontmatter, content: afterFrontmatter }; |
| 63 | +} |
| 64 | + |
3 | 65 | /** |
4 | 66 | * A simplified markdown parser that splits content into linear blocks. |
5 | 67 | * For a production app, we would use a robust AST walker (remark), |
6 | 68 | * but for this demo, we want predictable text-anchoring. |
7 | 69 | */ |
8 | 70 | export const parseMarkdownToBlocks = (markdown: string): Block[] => { |
9 | | - const lines = markdown.split('\n'); |
| 71 | + const { content: cleanMarkdown } = extractFrontmatter(markdown); |
| 72 | + const lines = cleanMarkdown.split('\n'); |
10 | 73 | const blocks: Block[] = []; |
11 | 74 | let currentId = 0; |
12 | 75 |
|
|
0 commit comments