Skip to content

Commit c223c1b

Browse files
committed
[CM2] Link categories and collections in entry fields
A breadth-first search is done to match the shallowest entry first. So, if these entries exist: - demos/geocentric-universe - demos/css/geocentric-universe - demos/css/css-only/geocentric-universe The following front-matter: --- favoriteDemo: +demos/geocentric-universe bestDemo: +demos/css/geocentric-universe crazyDemo: +demos/css/css-only/geocentric-universe --- would result in exactly what it looks like it should. In the earlier code, subcategories were not being traversed and also a bug (links.categorySlug) meant that it always looked at collection.posts, and never actually considered the categorySlug. With this update it's also fixed. And categories and collections can now also be linked through front-matter fields.
1 parent efda1fb commit c223c1b

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/compiler/contentModel2/index.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,36 @@ const parseLink = (value) => {
2424
}
2525

2626
const findLinkedEntry = (contentModel, link) => {
27-
const collection = contentModel.collections.find(c => c.slug.match(new RegExp(link.collectionSlug, 'i')))
28-
const container = link.categorySlugs[0] ? // TODO: Handle subcategories
29-
collection.categories.find(c => c.slug.match(new RegExp(link.categorySlug, 'i'))) || collection :
30-
collection
31-
return container.posts.find(p => p.slug.match(new RegExp(link.entrySlug, 'i')))
27+
const collectionSlugRe = new RegExp(link.collectionSlug, 'i')
28+
const collection = contentModel.collections.find(c => c.slug.match(collectionSlugRe))
29+
let container = collection
30+
31+
for (const categorySlug of link.categorySlugs) {
32+
const categorySlugRe = new RegExp(categorySlug, 'i')
33+
const category = container.categories?.find(c => c.slug.match(categorySlugRe))
34+
if (!category) {
35+
break
36+
}
37+
container = category
38+
}
39+
40+
const entrySlugRe = new RegExp(link.entrySlug, 'i')
41+
const queue = [container]
42+
while (queue.length > 0) {
43+
const node = queue.shift()
44+
if (node.slug?.match(entrySlugRe)) {
45+
return node
46+
}
47+
const match = node.levelPosts?.find(p => p.slug.match(entrySlugRe))
48+
if (match) {
49+
return match
50+
}
51+
if (node.categories) {
52+
queue.push(...node.categories)
53+
}
54+
}
55+
56+
return undefined
3257
}
3358

3459
const linkBack = (post, entry, key) => {

src/compiler/contentModel2/models/collection/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,15 @@ module.exports = function Collection(settings = defaultSettings, contentTypes =
105105
if (childModels.category.draftCheck(uncategorizedPost)) {
106106
defaultCategory.levelPosts.push(uncategorizedPost)
107107
defaultCategory.posts.push(uncategorizedPost)
108+
tree.levelPosts.push(uncategorizedPost)
108109
tree.posts.push(uncategorizedPost)
109110
}
110111
}
111112

112113
const tree = {
113114
categories: [],
114115
posts: [],
116+
levelPosts: [],
115117
attachments: [],
116118
facets: []
117119
}

0 commit comments

Comments
 (0)