Skip to content

Commit 08f6963

Browse files
committed
[CM2] Link back to entries via named props
Content type of an entry can specify through which fields can serve as connection points to entries that link to them. e.g. Article's author links to writers/enes, as usual AND NOW Person's articles can be a bucket of those Articles. SO If the content type of an entry specifies fields that accepts links, those fields will be used during backlinking, instead of pushing to its relations object. For this to work, the content type should use this syntax for a 'bucket' field: (a person content type) articles: +Article:author parents: [+Person:sons, +Person:daughters] photos: +:photographer in other words, the value of a field should specify the linking entry's field name and optionally its content type. And multiple of these can be provided as an array. This is a first draft and it needs more thoughts. Currently, the previous behaviour of pushing into target entry's relations object is preserved for when the target entry doesn't have a content type or when it doesn't specify anything related to linking or otherwise no buckets are found in target's content type.
1 parent c330407 commit 08f6963

File tree

4 files changed

+26
-7
lines changed

4 files changed

+26
-7
lines changed

src/compiler/contentModel2/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ const findLinkedEntry = (contentModel, link) => {
3232
}
3333

3434
const linkBack = (post, entry, key) => {
35+
if (entry.schema) {
36+
Object.keys(entry.schema).forEach(schemaKey => {
37+
const schemaValue = entry.schema[schemaKey]
38+
const re = new RegExp(`^\\+(${post.contentType}|):${key}$`)
39+
const match = Array.isArray(schemaValue) ?
40+
schemaValue.find(v => re.test(v)) :
41+
re.test(schemaValue)
42+
if (match) {
43+
// console.log('linking', post.title, 'to', schemaKey, 'field of', entry.title)
44+
entry[schemaKey] = entry[schemaKey] || []
45+
entry[schemaKey].push(post)
46+
}
47+
})
48+
return
49+
}
3550
entry.links = entry.links || {}
3651
entry.links.relations = entry.links.relations || []
3752
const relation = entry.links.relations.find(r => r.key === key)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const defaultSettings = {
4242
categoryAlias: undefined,
4343
entryAlias: undefined
4444
}
45-
module.exports = function Category(settings = defaultSettings, level = 1) {
45+
module.exports = function Category(settings = defaultSettings, contentTypes, level = 1) {
4646
const indexFileNameOptions = [settings.categoryAlias, 'category'].filter(Boolean)
4747

4848
const isCategoryIndexFile = (node) => {
@@ -55,7 +55,7 @@ module.exports = function Category(settings = defaultSettings, level = 1) {
5555
attachment: models.attachment(),
5656
post: models.post({
5757
entryAlias: settings.entryAlias
58-
})
58+
}, contentTypes)
5959
}
6060

6161
return {
@@ -173,7 +173,7 @@ module.exports = function Category(settings = defaultSettings, level = 1) {
173173
const SubCategoryModel = Category({
174174
entryAlias: categoryContext.entryAlias || settings.entryAlias,
175175
categoryAlias: categoryContext.categoryAlias || settings.categoryAlias
176-
}, level + 1)
176+
}, contentTypes, level + 1)
177177
const subCategory = SubCategoryModel.create(childNode, childContext)
178178
tree.categories.push(subCategory)
179179
tree.posts.push(...subCategory.posts)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,10 @@ module.exports = function Collection(settings = defaultSettings, contentTypes =
149149
category: models.category({
150150
categoryAlias: indexProps.attributes?.categoryAlias || contentType?.categoryAlias,
151151
entryAlias: indexProps.attributes?.entryAlias || contentType?.entryAlias
152-
}),
152+
}, contentTypes),
153153
post: models.post({
154154
entryAlias: indexProps.attributes?.entryAlias || contentType?.entryAlias
155-
})
155+
}, contentTypes)
156156
}
157157

158158
node.children.forEach(childNode => {

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const models = {
1010
const defaultSettings = {
1111
entryAlias: undefined
1212
}
13-
module.exports = function Post(settings = defaultSettings) {
13+
module.exports = function Post(settings = defaultSettings, contentTypes = []) {
1414
const indexFileNameOptions = [settings.entryAlias, 'post', 'index'].filter(Boolean)
1515

1616
const isPostIndexFile = (node) => {
@@ -44,11 +44,14 @@ module.exports = function Post(settings = defaultSettings) {
4444
outputPath,
4545
}
4646

47+
const contentType = context.peek().entryContentType
48+
4749
return {
4850
...baseEntryProps,
4951
...postContext,
5052
context,
51-
contentType: context.peek().entryContentType,
53+
contentType,
54+
schema: contentTypes.find(ct => ct.name === contentType),
5255
date: new Date(baseEntryProps.date || baseEntryProps.stats.birthtime || Date.now()),
5356
attachments: baseEntryProps.attachments.map(
5457
attachment => attachment(context.push({
@@ -83,6 +86,7 @@ module.exports = function Post(settings = defaultSettings) {
8386
return renderer.render({
8487
templates: [
8588
`pages/${post.template}`,
89+
`pages/post/${entryAlias}`,
8690
`pages/post/${post.contentType}`,
8791
`pages/post/default`
8892
],

0 commit comments

Comments
 (0)