Skip to content

Commit efda1fb

Browse files
committed
[CM2] Introduce draft nodes
Posts, categories, subCategories and collections can be made draft with: --- draft: true --- Drafting an entry means it'll only show up in the start mode. Links to a draft entry (via field relations, e.g. field: +/path/to/draft/entry) are removed from the source entries. Also in the changes: - Add missing setting keys to defaultSettings. Importantly, 'mode' - Update defaultSettings of all models to include 'mode' - Copy-paste a 'draftCheck' function across all models - Coin the term 'node'. A node is anything in the hierarchy.
1 parent 376ba0a commit efda1fb

File tree

7 files changed

+82
-32
lines changed

7 files changed

+82
-32
lines changed

src/compiler/contentModel2/index.js

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,33 @@ const linkEntries = (contentModel) => {
6767
Object.keys(post).forEach(key => {
6868
const value = post[key]
6969
if (Array.isArray(value)) {
70-
value.forEach((valueItem, valueIndex) => {
70+
for (let i = 0; i < value.length; i++) {
71+
let valueItem = value[i]
7172
if (!LINKED_FIELD_SYNTAX.test(valueItem)) {
72-
return
73+
break
7374
}
7475
const link = parseLink(valueItem)
7576
const entry = findLinkedEntry(contentModel, link)
76-
post[key][valueIndex] = Object.assign({}, entry)
77-
linkBack(post, entry, key)
78-
})
77+
if (entry) {
78+
post[key][i] = Object.assign({}, entry)
79+
linkBack(post, entry, key)
80+
} else {
81+
post[key].splice(i, 1)
82+
i--
83+
}
84+
}
7985
} else {
8086
if (!LINKED_FIELD_SYNTAX.test(value)) {
8187
return
8288
}
8389
const link = parseLink(value)
8490
const entry = findLinkedEntry(contentModel, link)
85-
post[key] = Object.assign({}, entry)
86-
linkBack(post, entry, key)
91+
if (entry) {
92+
post[key] = Object.assign({}, entry)
93+
linkBack(post, entry, key)
94+
} else {
95+
post[key] = undefined
96+
}
8797
}
8898
})
8999
})
@@ -101,7 +111,11 @@ const defaultContentModelSettings = {
101111
site: {
102112
title: '',
103113
description: ''
104-
}
114+
},
115+
mode: 'start',
116+
search: 'off',
117+
rss: 'off',
118+
syntaxHighlighting: 'off'
105119
}
106120
class ContentModel {
107121
constructor(contentModelSettings = defaultContentModelSettings, contentTypes = []) {
@@ -112,6 +126,10 @@ class ContentModel {
112126
this.contentTypes = contentTypes
113127
}
114128

129+
draftCheck(node) {
130+
return this.settings.mode === 'start' || !node.draft
131+
}
132+
115133
create(fileSystemTree) {
116134
const indexFileNameOptions = ['root']
117135

@@ -138,19 +156,23 @@ class ContentModel {
138156
.filter(ct => ct.model === 'collection')
139157
.map(ct => ct.collectionAlias),
140158
...(indexProps.attributes?.collectionAliases || [])
141-
]
159+
],
160+
mode: this.settings.mode
142161
}, this.contentTypes),
143162

144163
subpage: models.subpage({
145-
pagesDirectory: this.settings.pagesDirectory
164+
pagesDirectory: this.settings.pagesDirectory,
165+
mode: this.settings.mode
146166
}),
147167

148168
homepage: models.homepage({
149-
homepageDirectory: this.settings.homepageDirectory
169+
homepageDirectory: this.settings.homepageDirectory,
170+
mode: this.settings.mode
150171
}),
151172

152173
asset: models.asset({
153-
assetsDirectory: this.settings.assetsDirectory
174+
assetsDirectory: this.settings.assetsDirectory,
175+
mode: this.settings.mode
154176
})
155177
}
156178

@@ -192,9 +214,11 @@ class ContentModel {
192214
}
193215

194216
if (this.models.collection.match(node)) {
195-
return this.contentModel.collections.push(
196-
this.models.collection.create(node, context)
197-
)
217+
const collection = this.models.collection.create(node, context)
218+
if (this.draftCheck(collection)) {
219+
this.contentModel.collections.push(collection)
220+
}
221+
return
198222
}
199223

200224
if (this.models.asset.matchAssetsDirectory(node)) {

src/compiler/contentModel2/models/asset.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ const { join, resolve } = require('path')
22
const { makePermalink } = require('../helpers')
33

44
const defaultSettings = {
5-
assetsDirectory: 'assets'
5+
assetsDirectory: 'assets',
6+
mode: 'start'
67
}
78
module.exports = function Asset(settings = defaultSettings) {
89
const assetsDirectoryNameOptions = [settings.assetsDirectory, 'assets']

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ function linkPosts(post, postIndex, posts) {
4343

4444
const defaultSettings = {
4545
categoryAlias: undefined,
46-
entryAlias: undefined
46+
entryAlias: undefined,
47+
mode: 'start'
4748
}
4849
module.exports = function Category(settings = defaultSettings, contentTypes, level = 1) {
4950
const indexFileNameOptions = [settings.categoryAlias, 'category'].filter(Boolean)
@@ -54,6 +55,10 @@ module.exports = function Category(settings = defaultSettings, contentTypes, lev
5455
)
5556
}
5657

58+
const draftCheck = (node) => {
59+
return settings.mode === 'start' || !node.draft
60+
}
61+
5762
const childModels = {
5863
attachment: models.attachment(),
5964
post: models.post({
@@ -62,6 +67,8 @@ module.exports = function Category(settings = defaultSettings, contentTypes, lev
6267
}
6368

6469
return {
70+
draftCheck,
71+
6572
match: (node) => node.children?.find(childNode => {
6673
const containsPosts = childModels.post.match(childNode)
6774
if (level > 3) {
@@ -166,8 +173,10 @@ module.exports = function Category(settings = defaultSettings, contentTypes, lev
166173
}
167174
if (childModels.post.match(childNode)) {
168175
const post = childModels.post.create(childNode, childContext)
169-
tree.levelPosts.push(post)
170-
tree.posts.push(post)
176+
if (draftCheck(post)) {
177+
tree.levelPosts.push(post)
178+
tree.posts.push(post)
179+
}
171180
return
172181
}
173182
if (Category().match(childNode)) {
@@ -176,8 +185,10 @@ module.exports = function Category(settings = defaultSettings, contentTypes, lev
176185
categoryAlias: categoryContext.categoryAlias || settings.categoryAlias
177186
}, contentTypes, level + 1)
178187
const subCategory = SubCategoryModel.create(childNode, childContext)
179-
tree.categories.push(subCategory)
180-
tree.posts.push(...subCategory.posts)
188+
if (draftCheck(subCategory)) {
189+
tree.categories.push(subCategory)
190+
tree.posts.push(...subCategory.posts)
191+
}
181192
return
182193
}
183194
if (childModels.attachment.match(childNode)) {

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

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ function locatePinnedEntries(entries) {
4848

4949
const defaultSettings = {
5050
defaultCategoryName: '',
51-
collectionAliases: []
51+
collectionAliases: [],
52+
mode: 'start'
5253
}
5354
module.exports = function Collection(settings = defaultSettings, contentTypes = []) {
5455
const indexFileNameOptions = [
@@ -66,6 +67,10 @@ module.exports = function Collection(settings = defaultSettings, contentTypes =
6667
return node.name.match(new RegExp(`^${parentNode.name}\\.json$`, 'i'))
6768
}
6869

70+
const draftCheck = (node) => {
71+
return settings.mode === 'start' || !node.draft
72+
}
73+
6974
return {
7075
match: node => {
7176
const hasCollectionIndex = node.children?.find(isCollectionIndexFile)
@@ -97,9 +102,11 @@ module.exports = function Collection(settings = defaultSettings, contentTypes =
97102
const uncategorizedPost = childNode ?
98103
childModels.post.create(childNode, postContext) :
99104
childModels.post.createFromData(postData, postContext)
100-
defaultCategory.levelPosts.push(uncategorizedPost)
101-
defaultCategory.posts.push(uncategorizedPost)
102-
tree.posts.push(uncategorizedPost)
105+
if (childModels.category.draftCheck(uncategorizedPost)) {
106+
defaultCategory.levelPosts.push(uncategorizedPost)
107+
defaultCategory.posts.push(uncategorizedPost)
108+
tree.posts.push(uncategorizedPost)
109+
}
103110
}
104111

105112
const tree = {
@@ -154,10 +161,12 @@ module.exports = function Collection(settings = defaultSettings, contentTypes =
154161
attachment: models.attachment(),
155162
category: models.category({
156163
categoryAlias: indexProps.attributes?.categoryAlias || contentType?.categoryAlias,
157-
entryAlias: indexProps.attributes?.entryAlias || contentType?.entryAlias
164+
entryAlias: indexProps.attributes?.entryAlias || contentType?.entryAlias,
165+
mode: settings.mode
158166
}, contentTypes),
159167
post: models.post({
160-
entryAlias: indexProps.attributes?.entryAlias || contentType?.entryAlias
168+
entryAlias: indexProps.attributes?.entryAlias || contentType?.entryAlias,
169+
mode: settings.mode
161170
}, contentTypes)
162171
}
163172

@@ -185,8 +194,10 @@ module.exports = function Collection(settings = defaultSettings, contentTypes =
185194
key: 'collection'
186195
})
187196
)
188-
tree.categories.push(newCategory)
189-
tree.posts.push(...newCategory.posts)
197+
if (draftCheck(newCategory)) {
198+
tree.categories.push(newCategory)
199+
tree.posts.push(...newCategory.posts)
200+
}
190201
return
191202
}
192203
if (childModels.attachment.match(childNode)) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const models = {
88
}
99

1010
const defaultSettings = {
11-
entryAlias: undefined
11+
entryAlias: undefined,
12+
mode: 'start'
1213
}
1314
module.exports = function Post(settings = defaultSettings, contentTypes = []) {
1415
const indexFileNameOptions = [settings.entryAlias, 'post', 'index'].filter(Boolean)

src/compiler/contentModel2/models/homepage.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ const models = {
66
}
77

88
const defaultSettings = {
9-
homepageDirectory: 'homepage'
9+
homepageDirectory: 'homepage',
10+
mode: 'start'
1011
}
1112
module.exports = function Homepage(settings = defaultSettings) {
1213
const indexFileNameOptions = ['homepage', 'home', 'index']

src/compiler/contentModel2/models/subpage.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ const models = {
66
}
77

88
const defaultSettings = {
9-
pagesDirectory: 'pages'
9+
pagesDirectory: 'pages',
10+
mode: 'start'
1011
}
1112
module.exports = function Subpage(settings = defaultSettings) {
1213
const indexFileNameOptions = ['page', 'index']

0 commit comments

Comments
 (0)