Skip to content

Commit a333c1c

Browse files
committed
Introduce exclusive one-to-one linking
Just like how a post can link to an author as a singular link, now the author can link back to a single post. Schema's linked-field definitions now have to use array notation to allow linking back to many entries. If it is in the form of: soulmate: +Person/foo then the field will be populated with a single entry. This is a preparation step for making linking more abstract. Make the entry schema responsible for the linked-field's type. Previously all backlinks were collected in 'bucket' fields and schema had no power to change that. Now, it has.
1 parent 0927bb3 commit a333c1c

File tree

6 files changed

+44
-9
lines changed

6 files changed

+44
-9
lines changed

src/compiler/contentModel/index.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,19 @@ const linkBack = (post, entry, key) => {
6363
if (entry.schema) {
6464
Object.keys(entry.schema).forEach(schemaKey => {
6565
const schemaValue = entry.schema[schemaKey]
66+
const isSchemaValueArray = Array.isArray(schemaValue)
6667
const re = new RegExp(`^\\+(${post.contentType}|):${key}$`)
67-
const match = Array.isArray(schemaValue) ?
68+
const match = isSchemaValueArray ?
6869
schemaValue.find(v => re.test(v)) :
6970
re.test(schemaValue)
7071
if (match) {
71-
// console.log('linking', post.title, 'to', schemaKey, 'field of', entry.title)
72-
entry[schemaKey] = entry[schemaKey] || []
73-
entry[schemaKey].push(post)
72+
if (isSchemaValueArray) {
73+
// console.log('linking', post.title, 'to', schemaKey, 'field of', entry.title)
74+
entry[schemaKey] = entry[schemaKey] || []
75+
entry[schemaKey].push(post)
76+
} else {
77+
entry[schemaKey] = post
78+
}
7479
}
7580
})
7681
return
@@ -372,4 +377,4 @@ class ContentModel extends ContentModelEntryNode {
372377
}
373378
}
374379

375-
module.exports = ContentModel
380+
module.exports = ContentModel
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
---
22
date: 2025-11-07
33
slug: tim
4+
twin: +authors/enes
45
---
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
---
22
name: Person
3-
articles: +Article:author
4-
demos: +Demo:maker
3+
articles: [+Article:author]
4+
demos: [+Demo:maker]
55
events: [+Event:organizers, +Event:participants]
6-
books: +Book:author
6+
books: [+Book:author]
7+
twin: +Person:twin
78
---

src/e2e-tests/magazine/fixtures/fs/theme/keep/templates/pages/post/Person.hbs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@
6565
{{/if}}
6666
</div>
6767

68+
<div>
69+
{{#if twin}}
70+
Twin: <a data-twin-link href="{{twin.permalink}}">{{twin.title}}</a>
71+
{{/if}}
72+
</div>
73+
6874
<div>
6975
{{#if links.relations}}
7076
Relations:

src/e2e-tests/magazine/fixtures/model.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,11 @@ const FIXTURE_CONTENT_MODEL = {
317317
},
318318
{ title: 'Vue good', slug: 'vue-good', permalink: '/articles/vue-good.html' }
319319
],
320+
twin: {
321+
title: 'Sir Tim Berners Lee',
322+
slug: 'tim',
323+
permalink: '/authors/tim.html'
324+
},
320325
content: "Hey, it's me.",
321326
context: {
322327
collection: {
@@ -423,6 +428,11 @@ const FIXTURE_CONTENT_MODEL = {
423428
permalink: '/articles/pure-js-very-good.html'
424429
}
425430
],
431+
twin: {
432+
title: 'Mustafa Enes',
433+
slug: 'enes',
434+
permalink: '/authors/enes.html'
435+
},
426436
content: '',
427437
context: {
428438
collection: {

src/e2e-tests/magazine/post.test.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,18 @@ test('E2E Magazine - Post Pages', async t => {
207207
`${post.permalink} displays all events with correct links`
208208
)
209209
}
210+
211+
if (post.twin) {
212+
const twinLink = $('[data-twin-link]')
213+
const linkText = $(twinLink).text()
214+
const linkHref = $(twinLink).attr('href')
215+
const twinLinked = linkText === post.twin.title && linkHref === post.twin.permalink
216+
217+
t.ok(
218+
twinLinked,
219+
`${post.permalink} displays twin with correct link`
220+
)
221+
}
210222
} catch (err) {
211223
t.fail(
212224
`${post.permalink} Person content links: ${err.message}`
@@ -373,4 +385,4 @@ test('E2E Magazine - Post Pages', async t => {
373385
}
374386
})
375387

376-
})
388+
})

0 commit comments

Comments
 (0)