(content: string)
| 43 | } |
| 44 | |
| 45 | function parseFrontmatter(content: string): { |
| 46 | content: string |
| 47 | ignoredKeys: string[] |
| 48 | invalid: boolean |
| 49 | tags: string[] |
| 50 | } { |
| 51 | const match = content.match( |
| 52 | /^---\r?\n([\s\S]*?)\r?\n---(?:\r?\n|$)([\s\S]*)$/, |
| 53 | ) |
| 54 | if (!match) { |
| 55 | return { content, ignoredKeys: [], invalid: false, tags: [] } |
| 56 | } |
| 57 | |
| 58 | let parsed: unknown |
| 59 | try { |
| 60 | parsed = yaml.load(match[1]) |
| 61 | } |
| 62 | catch { |
| 63 | return { content, ignoredKeys: [], invalid: true, tags: [] } |
| 64 | } |
| 65 | |
| 66 | if (!parsed || typeof parsed !== 'object') { |
| 67 | return { |
| 68 | content: match[2] ?? '', |
| 69 | ignoredKeys: [], |
| 70 | invalid: false, |
| 71 | tags: [], |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | const frontmatter = parsed as Record<string, unknown> |
| 76 | const ignoredKeys = Object.keys(frontmatter).filter(key => key !== 'tags') |
| 77 | const tags = frontmatter.tags |
| 78 | if (typeof tags === 'string') { |
| 79 | return { |
| 80 | content: match[2] ?? '', |
| 81 | ignoredKeys, |
| 82 | invalid: false, |
| 83 | tags: tags |
| 84 | .split(/[,\s]+/) |
| 85 | .map(normalizeImportTag) |
| 86 | .filter((tag): tag is string => !!tag), |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if (Array.isArray(tags)) { |
| 91 | return { |
| 92 | content: match[2] ?? '', |
| 93 | ignoredKeys, |
| 94 | invalid: false, |
| 95 | tags: tags.map(normalizeImportTag).filter((tag): tag is string => !!tag), |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | return { content: match[2] ?? '', ignoredKeys, invalid: false, tags: [] } |
| 100 | } |
| 101 | |
| 102 | function isImportableTag(tag: string): boolean { |
no outgoing calls
no test coverage detected