|
| 1 | +type NoteCodeSnippet = { |
| 2 | + language?: unknown; |
| 3 | + description?: unknown; |
| 4 | + code?: unknown; |
| 5 | +}; |
| 6 | + |
| 7 | +type NormalizedCodeSnippet = { |
| 8 | + language: string; |
| 9 | + description: string; |
| 10 | + code: string; |
| 11 | +}; |
| 12 | + |
| 13 | +export type NoteMarkdownExportInput = { |
| 14 | + id?: unknown; |
| 15 | + title?: unknown; |
| 16 | + summary?: unknown; |
| 17 | + key_conclusions?: unknown; |
| 18 | + code_snippets?: unknown; |
| 19 | + source_type?: unknown; |
| 20 | + tags?: unknown; |
| 21 | +}; |
| 22 | + |
| 23 | +type NoteMarkdownExportOptions = { |
| 24 | + exportedAt?: Date | string; |
| 25 | + labels?: Partial<NoteMarkdownExportLabels>; |
| 26 | +}; |
| 27 | + |
| 28 | +export type NoteMarkdownExport = { |
| 29 | + filename: string; |
| 30 | + content: string; |
| 31 | +}; |
| 32 | + |
| 33 | +type NoteMarkdownExportLabels = { |
| 34 | + summary: string; |
| 35 | + keyConclusions: string; |
| 36 | + codeSnippets: string; |
| 37 | +}; |
| 38 | + |
| 39 | +const DEFAULT_LABELS: NoteMarkdownExportLabels = { |
| 40 | + summary: "Summary", |
| 41 | + keyConclusions: "Key Conclusions", |
| 42 | + codeSnippets: "Code Snippets", |
| 43 | +}; |
| 44 | + |
| 45 | +function cleanString(value: unknown) { |
| 46 | + return typeof value === "string" ? value.trim() : ""; |
| 47 | +} |
| 48 | + |
| 49 | +function cleanStringArray(value: unknown) { |
| 50 | + if (!Array.isArray(value)) return []; |
| 51 | + return value |
| 52 | + .map((item) => cleanString(item)) |
| 53 | + .filter((item) => item.length > 0); |
| 54 | +} |
| 55 | + |
| 56 | +function yamlScalar(value: string) { |
| 57 | + if (/^[A-Za-z0-9_-]+(?: [A-Za-z0-9_-]+)*$/.test(value)) { |
| 58 | + return value; |
| 59 | + } |
| 60 | + |
| 61 | + return `"${value |
| 62 | + .replace(/\\/g, "\\\\") |
| 63 | + .replace(/\n/g, "\\n") |
| 64 | + .replace(/\r/g, "\\r") |
| 65 | + .replace(/"/g, '\\"')}"`; |
| 66 | +} |
| 67 | + |
| 68 | +function pad2(value: number) { |
| 69 | + return String(value).padStart(2, "0"); |
| 70 | +} |
| 71 | + |
| 72 | +function toDate(value: Date | string | undefined) { |
| 73 | + if (value instanceof Date) return value; |
| 74 | + if (typeof value === "string") { |
| 75 | + const parsed = new Date(value); |
| 76 | + if (!Number.isNaN(parsed.getTime())) return parsed; |
| 77 | + } |
| 78 | + return new Date(); |
| 79 | +} |
| 80 | + |
| 81 | +function formatLocalExportedAt(value: Date | string | undefined) { |
| 82 | + const date = toDate(value); |
| 83 | + const offsetMinutes = -date.getTimezoneOffset(); |
| 84 | + const offsetSign = offsetMinutes >= 0 ? "+" : "-"; |
| 85 | + const absoluteOffset = Math.abs(offsetMinutes); |
| 86 | + const offsetHours = Math.floor(absoluteOffset / 60); |
| 87 | + const offsetRemainderMinutes = absoluteOffset % 60; |
| 88 | + |
| 89 | + return [ |
| 90 | + `${date.getFullYear()}-${pad2(date.getMonth() + 1)}-${pad2(date.getDate())}`, |
| 91 | + `${pad2(date.getHours())}:${pad2(date.getMinutes())}:${pad2(date.getSeconds())}`, |
| 92 | + `GMT${offsetSign}${pad2(offsetHours)}:${pad2(offsetRemainderMinutes)}`, |
| 93 | + ].join(" "); |
| 94 | +} |
| 95 | + |
| 96 | +function slugify(value: string) { |
| 97 | + return value |
| 98 | + .toLowerCase() |
| 99 | + .replace(/[^a-z0-9]+/g, "-") |
| 100 | + .replace(/^-+|-+$/g, "") |
| 101 | + .replace(/-{2,}/g, "-"); |
| 102 | +} |
| 103 | + |
| 104 | +function maxBacktickRun(value: string) { |
| 105 | + return Math.max(0, ...Array.from(value.matchAll(/`+/g), (match) => match[0].length)); |
| 106 | +} |
| 107 | + |
| 108 | +function codeFenceFor(code: string) { |
| 109 | + return "`".repeat(Math.max(3, maxBacktickRun(code) + 1)); |
| 110 | +} |
| 111 | + |
| 112 | +function normalizeLanguage(value: unknown) { |
| 113 | + const language = cleanString(value).split(/\s+/)[0]; |
| 114 | + return language || "text"; |
| 115 | +} |
| 116 | + |
| 117 | +function normalizeCodeSnippets(value: unknown): NormalizedCodeSnippet[] { |
| 118 | + if (!Array.isArray(value)) return []; |
| 119 | + return value |
| 120 | + .filter((item): item is NoteCodeSnippet => Boolean(item && typeof item === "object")) |
| 121 | + .map((item) => ({ |
| 122 | + language: normalizeLanguage(item.language), |
| 123 | + description: cleanString(item.description), |
| 124 | + code: typeof item.code === "string" ? item.code : "", |
| 125 | + })) |
| 126 | + .filter((item) => item.code.length > 0); |
| 127 | +} |
| 128 | + |
| 129 | +function buildFrontmatter(input: { |
| 130 | + title: string; |
| 131 | + tags: string[]; |
| 132 | + sourceType: string; |
| 133 | + exportedAt: string; |
| 134 | +}) { |
| 135 | + const lines = ["---", `title: ${yamlScalar(input.title)}`]; |
| 136 | + |
| 137 | + if (input.tags.length > 0) { |
| 138 | + lines.push("tags:"); |
| 139 | + for (const tag of input.tags) { |
| 140 | + lines.push(` - ${yamlScalar(tag)}`); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + if (input.sourceType) { |
| 145 | + lines.push(`source_type: ${yamlScalar(input.sourceType)}`); |
| 146 | + } |
| 147 | + |
| 148 | + lines.push(`exported_at: ${yamlScalar(input.exportedAt)}`, "---"); |
| 149 | + return lines.join("\n"); |
| 150 | +} |
| 151 | + |
| 152 | +export function createNoteMarkdownExport( |
| 153 | + note: NoteMarkdownExportInput, |
| 154 | + options: NoteMarkdownExportOptions = {}, |
| 155 | +): NoteMarkdownExport { |
| 156 | + const id = typeof note.id === "number" || typeof note.id === "string" ? String(note.id) : "note"; |
| 157 | + const title = cleanString(note.title) || `Note ${id}`; |
| 158 | + const summary = cleanString(note.summary); |
| 159 | + const keyConclusions = cleanStringArray(note.key_conclusions); |
| 160 | + const codeSnippets = normalizeCodeSnippets(note.code_snippets); |
| 161 | + const tags = cleanStringArray(note.tags); |
| 162 | + const sourceType = cleanString(note.source_type); |
| 163 | + const exportedAt = formatLocalExportedAt(options.exportedAt); |
| 164 | + const labels = { ...DEFAULT_LABELS, ...options.labels }; |
| 165 | + const slug = slugify(title); |
| 166 | + const filename = slug ? `${id}-${slug}.md` : `note-${id}.md`; |
| 167 | + |
| 168 | + const sections = [ |
| 169 | + buildFrontmatter({ title, tags, sourceType, exportedAt }), |
| 170 | + `# ${title}`, |
| 171 | + ]; |
| 172 | + |
| 173 | + if (summary) { |
| 174 | + sections.push(`## ${labels.summary}\n\n${summary}`); |
| 175 | + } |
| 176 | + |
| 177 | + if (keyConclusions.length > 0) { |
| 178 | + sections.push(`## ${labels.keyConclusions}\n\n${keyConclusions.map((item) => `- ${item}`).join("\n")}`); |
| 179 | + } |
| 180 | + |
| 181 | + if (codeSnippets.length > 0) { |
| 182 | + const snippets = codeSnippets.map((snippet) => { |
| 183 | + const fence = codeFenceFor(snippet.code); |
| 184 | + const heading = snippet.description ? `### ${snippet.description}\n\n` : ""; |
| 185 | + return `${heading}${fence}${snippet.language}\n${snippet.code}\n${fence}`; |
| 186 | + }); |
| 187 | + sections.push(`## ${labels.codeSnippets}\n\n${snippets.join("\n\n")}`); |
| 188 | + } |
| 189 | + |
| 190 | + return { |
| 191 | + filename, |
| 192 | + content: `${sections.join("\n\n")}\n`, |
| 193 | + }; |
| 194 | +} |
| 195 | + |
| 196 | +export function downloadMarkdownFile(exported: NoteMarkdownExport) { |
| 197 | + const blob = new Blob([exported.content], { type: "text/markdown;charset=utf-8" }); |
| 198 | + const url = URL.createObjectURL(blob); |
| 199 | + const link = document.createElement("a"); |
| 200 | + link.href = url; |
| 201 | + link.download = exported.filename; |
| 202 | + document.body.appendChild(link); |
| 203 | + link.click(); |
| 204 | + link.remove(); |
| 205 | + URL.revokeObjectURL(url); |
| 206 | +} |
0 commit comments