(content: string)
| 49 | * ``` |
| 50 | */ |
| 51 | export function parseQuartoFormat(content: string): QuartoDocument { |
| 52 | const cells: QuartoCell[] = [] |
| 53 | |
| 54 | // Parse YAML frontmatter |
| 55 | let frontmatter: QuartoFrontmatter | undefined |
| 56 | let mainContent = content |
| 57 | |
| 58 | // Match YAML frontmatter between --- markers (allows empty content) |
| 59 | const frontmatterMatch = /^---\r?\n([\s\S]*?)---\r?\n?/.exec(content) |
| 60 | if (frontmatterMatch) { |
| 61 | const parsed = parseYamlFrontmatter(frontmatterMatch[1]) |
| 62 | // Always set frontmatter if there's a frontmatter block, even if empty |
| 63 | frontmatter = parsed |
| 64 | mainContent = content.slice(frontmatterMatch[0].length) |
| 65 | } |
| 66 | |
| 67 | // Split content into chunks based on code fences |
| 68 | // Match ```{language} ... ``` patterns |
| 69 | // Allow hyphens in language identifiers (e.g., python-repl) |
| 70 | const codeChunkRegex = /```\{([\w-]+)\}\r?\n([\s\S]*?)```/g |
| 71 | |
| 72 | let lastIndex = 0 |
| 73 | let match: RegExpExecArray | null = codeChunkRegex.exec(mainContent) |
| 74 | |
| 75 | while (match !== null) { |
| 76 | // Add markdown before this code chunk |
| 77 | const markdownBefore = mainContent.slice(lastIndex, match.index).trim() |
| 78 | if (markdownBefore) { |
| 79 | // Split at cell delimiters to preserve cell boundaries |
| 80 | addMarkdownCells(cells, markdownBefore) |
| 81 | } |
| 82 | |
| 83 | // Parse the code chunk |
| 84 | const language = match[1] |
| 85 | let codeContent = match[2] |
| 86 | |
| 87 | // Parse cell options (lines starting with #|) |
| 88 | const options = parseQuartoCellOptions(codeContent) |
| 89 | if (options) { |
| 90 | // Remove option lines from content |
| 91 | const lines = codeContent.split('\n') |
| 92 | const contentLines = lines.filter(line => !line.trimStart().startsWith('#|')) |
| 93 | codeContent = contentLines.join('\n').trim() |
| 94 | } else { |
| 95 | codeContent = codeContent.trim() |
| 96 | } |
| 97 | |
| 98 | cells.push({ |
| 99 | cellType: 'code', |
| 100 | content: codeContent, |
| 101 | language, |
| 102 | ...(options ? { options } : {}), |
| 103 | }) |
| 104 | |
| 105 | lastIndex = match.index + match[0].length |
| 106 | match = codeChunkRegex.exec(mainContent) |
| 107 | } |
| 108 |
no test coverage detected