(document: QuartoDocument)
| 60 | * @returns The serialized Quarto format string |
| 61 | */ |
| 62 | export function serializeQuartoFormat(document: QuartoDocument): string { |
| 63 | const lines: string[] = [] |
| 64 | |
| 65 | // Add YAML frontmatter |
| 66 | if (document.frontmatter && Object.keys(document.frontmatter).length > 0) { |
| 67 | lines.push('---') |
| 68 | for (const [key, value] of Object.entries(document.frontmatter)) { |
| 69 | if (typeof value === 'string') { |
| 70 | // Quote strings that contain special characters |
| 71 | if (value.includes(':') || value.includes('#') || value.includes("'") || value.includes('"')) { |
| 72 | lines.push(`${key}: "${value.replace(/"/g, '\\"')}"`) |
| 73 | } else { |
| 74 | lines.push(`${key}: ${value}`) |
| 75 | } |
| 76 | } else if (typeof value === 'boolean') { |
| 77 | lines.push(`${key}: ${value}`) |
| 78 | } else if (value !== undefined) { |
| 79 | lines.push(`${key}: ${JSON.stringify(value)}`) |
| 80 | } |
| 81 | } |
| 82 | lines.push('---') |
| 83 | lines.push('') |
| 84 | } |
| 85 | |
| 86 | // Add cells |
| 87 | let previousCellType: 'code' | 'markdown' | null = null |
| 88 | for (const cell of document.cells) { |
| 89 | if (cell.cellType === 'markdown') { |
| 90 | // Add delimiter between consecutive markdown cells to preserve boundaries |
| 91 | if (previousCellType === 'markdown') { |
| 92 | lines.push('<!-- cell -->') |
| 93 | lines.push('') |
| 94 | } |
| 95 | lines.push(cell.content) |
| 96 | lines.push('') |
| 97 | } else { |
| 98 | // Code cell |
| 99 | const language = cell.language || 'python' |
| 100 | lines.push(`\`\`\`{${language}}`) |
| 101 | |
| 102 | // Add cell options |
| 103 | if (cell.options) { |
| 104 | const optionLines = serializeQuartoCellOptions(cell.options) |
| 105 | lines.push(...optionLines) |
| 106 | } |
| 107 | |
| 108 | lines.push(cell.content) |
| 109 | lines.push('```') |
| 110 | lines.push('') |
| 111 | } |
| 112 | previousCellType = cell.cellType |
| 113 | } |
| 114 | |
| 115 | // Remove trailing empty lines |
| 116 | while (lines.length > 0 && lines[lines.length - 1] === '') { |
| 117 | lines.pop() |
| 118 | } |
| 119 |
no test coverage detected