|
1 | 1 | const getPosts = require("./posts.js"); |
2 | 2 | const getTagDefinitions = require("./tagDefinitions.js"); |
3 | 3 |
|
| 4 | +function createFallbackDefinition(rawTag) { |
| 5 | + return { |
| 6 | + tag: rawTag, |
| 7 | + title: `Tag: ${rawTag}`, |
| 8 | + commentary: null, |
| 9 | + parent: null, |
| 10 | + parentTag: null, |
| 11 | + parentPermalink: null, |
| 12 | + displayTitle: rawTag, |
| 13 | + heading: `Tag: ${rawTag}`, |
| 14 | + permalink: `/tags/${rawTag}/`, |
| 15 | + slug: `tags/${rawTag}`, |
| 16 | + aliases: [rawTag] |
| 17 | + }; |
| 18 | +} |
| 19 | + |
| 20 | +function uniquePosts(posts) { |
| 21 | + const seen = new Set(); |
| 22 | + const result = []; |
| 23 | + |
| 24 | + for (const post of posts) { |
| 25 | + if (seen.has(post.permalink)) { |
| 26 | + continue; |
| 27 | + } |
| 28 | + |
| 29 | + seen.add(post.permalink); |
| 30 | + result.push(post); |
| 31 | + } |
| 32 | + |
| 33 | + return result; |
| 34 | +} |
| 35 | + |
4 | 36 | module.exports = function () { |
5 | 37 | const posts = getPosts(); |
6 | | - const { byAlias } = getTagDefinitions(); |
| 38 | + const { all, byAlias } = getTagDefinitions(); |
7 | 39 | const tagMap = new Map(); |
8 | 40 |
|
| 41 | + for (const definition of all) { |
| 42 | + tagMap.set(definition.permalink, { |
| 43 | + ...definition, |
| 44 | + directPosts: [], |
| 45 | + posts: [], |
| 46 | + rawTags: new Set(), |
| 47 | + childrenPermalinks: new Set() |
| 48 | + }); |
| 49 | + } |
| 50 | + |
9 | 51 | for (const post of posts) { |
10 | 52 | for (const rawTag of post.tags || []) { |
11 | | - const definition = byAlias[rawTag] || { |
12 | | - tag: rawTag, |
13 | | - title: `Tag: ${rawTag}`, |
14 | | - displayTitle: rawTag, |
15 | | - heading: `Tag: ${rawTag}`, |
16 | | - permalink: `/tags/${rawTag}/`, |
17 | | - slug: `tags/${rawTag}`, |
18 | | - aliases: [rawTag] |
19 | | - }; |
| 53 | + const definition = byAlias[rawTag] || createFallbackDefinition(rawTag); |
20 | 54 | const key = definition.permalink; |
21 | 55 |
|
22 | 56 | if (!tagMap.has(key)) { |
23 | 57 | tagMap.set(key, { |
24 | 58 | ...definition, |
| 59 | + directPosts: [], |
25 | 60 | posts: [], |
26 | 61 | rawTags: new Set() |
27 | 62 | }); |
28 | 63 | } |
29 | 64 |
|
30 | 65 | const tagEntry = tagMap.get(key); |
31 | 66 |
|
32 | | - if (!tagEntry.posts.some((entry) => entry.permalink === post.permalink)) { |
33 | | - tagEntry.posts.push(post); |
| 67 | + if (!tagEntry.directPosts.some((entry) => entry.permalink === post.permalink)) { |
| 68 | + tagEntry.directPosts.push(post); |
34 | 69 | } |
35 | 70 |
|
36 | 71 | tagEntry.rawTags.add(rawTag); |
37 | 72 | } |
38 | 73 | } |
39 | 74 |
|
| 75 | + for (const tagEntry of tagMap.values()) { |
| 76 | + if (tagEntry.parentPermalink && tagMap.has(tagEntry.parentPermalink)) { |
| 77 | + tagMap.get(tagEntry.parentPermalink).childrenPermalinks.add(tagEntry.permalink); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + const memo = new Map(); |
| 82 | + |
| 83 | + function collectPostsForTag(permalink, stack = []) { |
| 84 | + if (memo.has(permalink)) { |
| 85 | + return memo.get(permalink); |
| 86 | + } |
| 87 | + |
| 88 | + if (stack.includes(permalink)) { |
| 89 | + throw new Error(`Tag hierarchy cycle detected: ${stack.concat(permalink).join(" -> ")}`); |
| 90 | + } |
| 91 | + |
| 92 | + const tagEntry = tagMap.get(permalink); |
| 93 | + const nestedStack = stack.concat(permalink); |
| 94 | + let postsForTag = [...(tagEntry.directPosts || [])]; |
| 95 | + |
| 96 | + for (const childPermalink of tagEntry.childrenPermalinks || []) { |
| 97 | + postsForTag = postsForTag.concat(collectPostsForTag(childPermalink, nestedStack)); |
| 98 | + } |
| 99 | + |
| 100 | + const deduped = uniquePosts(postsForTag).sort((a, b) => new Date(b.date) - new Date(a.date)); |
| 101 | + memo.set(permalink, deduped); |
| 102 | + return deduped; |
| 103 | + } |
| 104 | + |
40 | 105 | return Array.from(tagMap.values()) |
41 | 106 | .map((tag) => ({ |
42 | 107 | ...tag, |
| 108 | + directCount: tag.directPosts.length, |
| 109 | + childTags: Array.from(tag.childrenPermalinks) |
| 110 | + .map((permalink) => tagMap.get(permalink)) |
| 111 | + .filter(Boolean) |
| 112 | + .map((entry) => ({ |
| 113 | + tag: entry.tag, |
| 114 | + displayTitle: entry.displayTitle, |
| 115 | + permalink: entry.permalink |
| 116 | + })) |
| 117 | + .sort((a, b) => a.displayTitle.localeCompare(b.displayTitle)), |
43 | 118 | rawTags: Array.from(tag.rawTags).sort(), |
44 | | - posts: tag.posts.sort((a, b) => new Date(b.date) - new Date(a.date)), |
45 | | - count: tag.posts.length |
| 119 | + posts: collectPostsForTag(tag.permalink), |
| 120 | + count: collectPostsForTag(tag.permalink).length |
46 | 121 | })) |
47 | 122 | .sort((a, b) => a.displayTitle.localeCompare(b.displayTitle)); |
48 | 123 | }; |
0 commit comments