Skip to content

Commit d9d5eb5

Browse files
committed
Tag posts have full tag objects in their tags field
Now, when tags are listed in a tag index, their tags can become links to other tag indices. Previously, contentModel.tags were created when post.tags was a simple array of string, and then the linking between tags and posts took place. This made posts inside contentModel.tags have tags as strings, rather than the full tag objects. Now, weirdly, linking actually introduces the tag objects for the first time. Then the tags enhancer collects those objects and creates the array of tags in contentModel, attaching the posts in the tag object
1 parent 1cf1b28 commit d9d5eb5

File tree

3 files changed

+23
-25
lines changed

3 files changed

+23
-25
lines changed

src/compiler/contentModel/enhancers/links.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const _ = require('lodash')
2-
const { pipe } = require('../../../helpers')
2+
const Settings = require('../../../settings')
3+
const { pipe, getSlug, makePermalink } = require('../../../helpers')
34

45
const getScoreByOverlap = (arrayOfStrings1 = [], arrayOfStrings2 = []) => {
56
const overlap = _.intersection(
@@ -145,11 +146,19 @@ const linkMentionedEntries = (contentModel) => {
145146
}
146147

147148
const _linkPostTags = (post, tags) => {
149+
const { permalinkPrefix } = Settings.getSettings()
148150
return {
149151
...post,
150152
tags: post.tags.map(tag => {
151-
const { posts, ...rest } = tags.find(t => t.tag === tag)
152-
return rest
153+
const permalink = makePermalink({
154+
prefix: permalinkPrefix,
155+
parts: ['tags', tag]
156+
})
157+
return {
158+
tag,
159+
slug: getSlug(tag),
160+
permalink
161+
}
153162
})
154163
}
155164
}

src/compiler/contentModel/enhancers/tags.js

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const { join } = require('path')
2-
const Settings = require('../../../settings')
3-
const { getSlug, makePermalink } = require('../../../helpers')
42

3+
/* Extract tags from posts as an array of tags with posts */
54
const getTags = (posts) => {
65
const tags = posts.map((post) => {
76
return post.tags.map((tag) => {
@@ -12,32 +11,22 @@ const getTags = (posts) => {
1211
const flatTags = [].concat(...tags)
1312

1413
const tagsIndex = flatTags.reduce((acc, tagWithPost) => {
15-
const tag = tagWithPost.tag
14+
const { tag, post } = tagWithPost
1615
return {
1716
...acc,
18-
[tag]: [
19-
...(acc[tag] || []),
20-
tagWithPost.post
21-
]
17+
[tag.tag]: {
18+
...tag,
19+
posts: [
20+
...(acc[tag.tag] ? acc[tag.tag].posts : []),
21+
post
22+
]
23+
}
2224
}
2325
}, {})
2426

25-
const { permalinkPrefix } = Settings.getSettings()
26-
2727
return Object
2828
.keys(tagsIndex)
29-
.map(key => {
30-
const permalink = makePermalink({
31-
prefix: permalinkPrefix,
32-
parts: ['tags', key]
33-
})
34-
return {
35-
tag: key,
36-
slug: getSlug(key),
37-
permalink,
38-
posts: tagsIndex[key]
39-
}
40-
})
29+
.map(key => tagsIndex[key])
4130
.sort((a, b) => b.posts.length - a.posts.length)
4231
}
4332

src/compiler/contentModel/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ const create = async (fileSystemTree) => {
1111
decorate.bind(null, 'contentModel'),
1212
withDates,
1313
withSortedPosts,
14+
withLinkedPosts,
1415
withTags,
15-
withLinkedPosts
1616
])
1717
return contentModel
1818
}

0 commit comments

Comments
 (0)