|
| 1 | +import type { ContentListFile } from 'comark-content' |
| 2 | +import type { GitHubCommit } from './github' |
| 3 | +import { hashManifestItem } from './json' |
| 4 | + |
| 5 | +/** How a push changed the content source, already filtered to `contentDir`. */ |
| 6 | +export interface ContentChanges { |
| 7 | + /** Manifest keys (`content/<stem><ext>`) of files added or modified. */ |
| 8 | + upserted: string[] |
| 9 | + /** Manifest keys of files removed — only the previous manifest can resolve their paths. */ |
| 10 | + removed: string[] |
| 11 | + /** A `.navigation.*` file changed, so the tree changed regardless of which pages did. */ |
| 12 | + navTouched: boolean |
| 13 | +} |
| 14 | + |
| 15 | +/** Files the content source can actually serve — matches the parsers installed in `content.ts`. */ |
| 16 | +const CONTENT_EXTENSIONS = ['.md', '.yml', '.yaml', '.json'] |
| 17 | + |
| 18 | +/** The manifest's hardcoded source name (see `contentSource()` in `content.ts`). */ |
| 19 | +const SOURCE_NAME = 'content' |
| 20 | + |
| 21 | +/** |
| 22 | + * A push's changed content files, named by their manifest key (`content/<stem><ext>`) — the |
| 23 | + * reverse of `meta.key`, so a diff against `manifest.items` doesn't need to re-derive file → URL |
| 24 | + * mappings that comark already owns. |
| 25 | + */ |
| 26 | +export function changesForPush(contentDir: string, commits: GitHubCommit[]): ContentChanges { |
| 27 | + const upserted = new Set<string>() |
| 28 | + const removed = new Set<string>() |
| 29 | + let navTouched = false |
| 30 | + |
| 31 | + const consider = (file: string, into: Set<string>) => { |
| 32 | + const key = manifestKeyFor(file, contentDir) |
| 33 | + if (!key) return |
| 34 | + |
| 35 | + if (isNavConfigFile(file)) navTouched = true |
| 36 | + else into.add(key) |
| 37 | + } |
| 38 | + |
| 39 | + for (const commit of commits) { |
| 40 | + for (const file of commit.added ?? []) consider(file, upserted) |
| 41 | + for (const file of commit.modified ?? []) consider(file, upserted) |
| 42 | + for (const file of commit.removed ?? []) consider(file, removed) |
| 43 | + } |
| 44 | + |
| 45 | + // A path removed and re-added in the same push is an upsert, not a removal. |
| 46 | + for (const key of upserted) removed.delete(key) |
| 47 | + |
| 48 | + return { upserted: [...upserted], removed: [...removed], navTouched } |
| 49 | +} |
| 50 | + |
| 51 | +/** Repo-relative path → its key in the manifest, or `null` when it can't be a content file. */ |
| 52 | +function manifestKeyFor(file: string, contentDir: string): string | null { |
| 53 | + const dir = contentDir.replace(/^\/+|\/+$/g, '') |
| 54 | + const prefix = dir ? `${dir}/` : '' |
| 55 | + |
| 56 | + if (prefix && !file.startsWith(prefix)) return null |
| 57 | + if (!CONTENT_EXTENSIONS.some((ext) => file.toLowerCase().endsWith(ext))) return null |
| 58 | + |
| 59 | + return `${SOURCE_NAME}/${file.slice(prefix.length)}` |
| 60 | +} |
| 61 | + |
| 62 | +/** Directory configuration (`.navigation.yml`), which contributes to the tree rather than a page. */ |
| 63 | +function isNavConfigFile(file: string): boolean { |
| 64 | + return /\.navigation\.(?:ya?ml|json)$/i.test(file) |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * The payload URL a client-side navigation fetches for `path` |
| 69 | + */ |
| 70 | +export function payloadUrlForPage(path: string, buildId?: string): string { |
| 71 | + const base = path === '/' ? '/_payload.json' : `${path.replace(/\/$/, '')}/_payload.json` |
| 72 | + return buildId ? `${base}?_b=${buildId}` : base |
| 73 | +} |
| 74 | + |
| 75 | +/** `content/<stem><ext>` (a manifest key) → page path, the reverse of what the path-keyed manifest gives. */ |
| 76 | +export function indexByFileKey(items: Record<string, ContentListFile>): Map<string, string> { |
| 77 | + const index = new Map<string, string>() |
| 78 | + for (const item of Object.values(items)) index.set(item.meta.key, item.path) |
| 79 | + return index |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Which pages a push changed, and whether the tree itself moved. |
| 84 | + */ |
| 85 | +export function diffContent( |
| 86 | + changes: ContentChanges, |
| 87 | + before: Record<string, ContentListFile>, |
| 88 | + after: Record<string, ContentListFile> |
| 89 | +): { pagePaths: string[]; navChanged: boolean } { |
| 90 | + const pagePaths = new Set<string>() |
| 91 | + |
| 92 | + const afterByKey = indexByFileKey(after) |
| 93 | + const beforeByKey = indexByFileKey(before) |
| 94 | + |
| 95 | + for (const key of changes.upserted) { |
| 96 | + const path = afterByKey.get(key) |
| 97 | + if (path) pagePaths.add(path) |
| 98 | + } |
| 99 | + for (const key of changes.removed) { |
| 100 | + const path = beforeByKey.get(key) |
| 101 | + if (path) pagePaths.add(path) |
| 102 | + } |
| 103 | + |
| 104 | + const beforeKeys = Object.keys(before) |
| 105 | + const afterKeys = Object.keys(after) |
| 106 | + const navChanged = |
| 107 | + beforeKeys.length !== afterKeys.length || |
| 108 | + afterKeys.some((key) => !before[key]) || |
| 109 | + // Listing fields (title, description, icon, `navigation`…) are what the tree renders from. |
| 110 | + afterKeys.some((key) => before[key] && !sameListing(before[key]!, after[key]!)) |
| 111 | + |
| 112 | + return { pagePaths: [...pagePaths], navChanged } |
| 113 | +} |
| 114 | + |
| 115 | +function sameListing(a: ContentListFile, b: ContentListFile): boolean { |
| 116 | + return a.path === b.path && hashManifestItem(a) === hashManifestItem(b) |
| 117 | +} |
0 commit comments