Skip to content

Commit 31db389

Browse files
committed
Introduce ContentModelResourceNode & ContentModelEntryNode
Use ContentModelNode as the base for both new classes. ContentModelResourceNode is a simpler parent class for assets and attachments. ContentModelEntryClass takes care of parsing index front-matter. With this, update asset, attachment, homepage, subpage and post models to use the class that fits them the best. And finally delete _baseEntry.
1 parent 5d67765 commit 31db389

File tree

9 files changed

+231
-287
lines changed

9 files changed

+231
-287
lines changed

src/compiler/contentModel/models/_baseEntry.js

Lines changed: 0 additions & 67 deletions
This file was deleted.

src/compiler/contentModel/models/asset.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
const { join } = require('path')
22
const { makePermalink } = require('../../../lib/contentModelHelpers')
3-
const ContentModelNode = require('../../../lib/ContentModelNode')
3+
const ContentModelResourceNode = require('../../../lib/ContentModelResourceNode')
44

55
const defaultSettings = {
66
assetsDirectory: 'assets'
77
}
8-
class Asset extends ContentModelNode {
8+
class Asset extends ContentModelResourceNode {
99
constructor(fsNode, context, settings = defaultSettings) {
1010
super(fsNode, context, settings)
1111
}

src/compiler/contentModel/models/attachment.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const ContentModelNode = require('../../../lib/ContentModelNode')
1+
const ContentModelResourceNode = require('../../../lib/ContentModelResourceNode')
22

3-
class Attachment extends ContentModelNode {
3+
class Attachment extends ContentModelResourceNode {
44
constructor(fsNode, context) {
55
super(fsNode, context)
66
}

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

Lines changed: 41 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
const { join } = require('path')
2-
const makeSlug = require('slug')
3-
const ContentModelNode = require('../../../../lib/ContentModelNode')
4-
const { templateExtensions, makePermalink, Markdown } = require('../../../../lib/contentModelHelpers')
5-
const { parseTextEntry } = require('../../../../lib/parseTextEntry')
2+
const ContentModelEntryNode = require('../../../../lib/ContentModelEntryNode')
3+
const { templateExtensions } = require('../../../../lib/contentModelHelpers')
64

75
const models = {
86
facet: require('./facet'),
@@ -12,75 +10,28 @@ const models = {
1210
const defaultSettings = {
1311
entryAlias: undefined
1412
}
15-
class Post extends ContentModelNode {
16-
constructor(fsNode, context, contentTypes, settings = defaultSettings) {
17-
super(fsNode, context, settings)
18-
19-
const isFlatData = !fsNode.stats?.birthtime
20-
const entryProperties = parseTextEntry(this.fsNode, this.subtree.indexFile, isFlatData)
21-
22-
// re-call these because slug is only now ready to use :(
23-
this.permalink = this.getPermalink(entryProperties.slug, entryProperties.hasIndex)
24-
this.outputPath = this.getOutputPath(entryProperties.slug, entryProperties.hasIndex)
25-
26-
const postContext = {
27-
title: entryProperties.title,
28-
slug: entryProperties.slug,
29-
permalink: this.permalink,
30-
outputPath: this.outputPath
13+
class Post extends ContentModelEntryNode {
14+
static serialize(post) {
15+
return {
16+
...post,
17+
attachments: post.subtree.attachments
3118
}
32-
33-
const contentType = this.context.peek().entryContentType
34-
35-
Object.assign(this, {
36-
...entryProperties,
37-
...postContext,
38-
context: this.context,
39-
contentType,
40-
schema: contentTypes.find(ct => ct.name === contentType),
41-
attachments: this.subtree.attachments.map(attachmentNode => {
42-
return new models.Attachment(
43-
attachmentNode,
44-
this.context.push({
45-
...postContext,
46-
key: 'post'
47-
})
48-
)
49-
})
50-
})
5119
}
5220

53-
getPermalink(slug, hasIndex) {
54-
return makePermalink(
55-
this.context.peek().permalink,
56-
this.slug || slug || ''
57-
) + ((this.hasIndex || hasIndex) ? '' : '.html')
58-
}
21+
constructor(fsNode, context, contentTypes, settings = defaultSettings) {
22+
super(fsNode, context, settings)
5923

60-
getOutputPath(slug, hasIndex) {
61-
return join(
62-
this.context.peek().outputPath,
63-
this.slug || slug || ''
64-
)
24+
this.contentType = this.context.peek().entryContentType
25+
this.schema = contentTypes.find(ct => ct.name === this.contentType)
6526
}
6627

67-
parseSubtree() {
68-
const tree = {
69-
indexFile: this.fsNode,
70-
attachments: []
71-
}
72-
73-
if (!this.fsNode.children || !this.fsNode.children.length) {
74-
return tree
75-
}
76-
77-
const indexFileNameOptions = [this.settings.entryAlias, 'post', 'index'].filter(Boolean)
78-
79-
const matchers = {
28+
getSubtreeMatchers() {
29+
return {
8030
indexFile: (fsNode) => {
8131
if (fsNode.children) {
8232
return false
8333
}
34+
const indexFileNameOptions = [this.settings.entryAlias, 'post', 'index'].filter(Boolean)
8435
const names = indexFileNameOptions.join('|')
8536
const extensions = templateExtensions.join('|')
8637
const namePattern = new RegExp(`^(${names})(${extensions})$`, 'i')
@@ -89,14 +40,34 @@ class Post extends ContentModelNode {
8940

9041
attachment: (fsNode) => true
9142
}
43+
}
44+
45+
parseSubtree() {
46+
const tree = {
47+
indexFile: this.fsNode,
48+
attachments: []
49+
}
50+
51+
if (!this.fsNode.children || !this.fsNode.children.length) {
52+
return tree
53+
}
54+
55+
const context = this.context.push({
56+
title: this.title,
57+
slug: this.slug,
58+
permalink: this.permalink,
59+
outputPath: this.outputPath,
60+
key: 'post'
61+
})
9262

9363
this.fsNode.children.forEach(childNode => {
94-
if (matchers.indexFile(childNode, indexFileNameOptions)) {
95-
tree.indexFile = childNode
64+
if (this.matchers.indexFile(childNode)) {
9665
return
9766
}
98-
if (matchers.attachment(childNode)) {
99-
tree.attachments.push(childNode)
67+
if (this.matchers.attachment(childNode)) {
68+
tree.attachments.push(
69+
new models.Attachment(childNode, context)
70+
)
10071
}
10172
})
10273

@@ -107,7 +78,7 @@ class Post extends ContentModelNode {
10778
// TODO: feels like collection should handle this
10879
models.facet().linkWithEntryFields(this, collectionFacets)
10980

110-
this.attachments.forEach(attachment => {
81+
this.subtree.attachments.forEach(attachment => {
11182
attachment.afterEffects(contentModel)
11283
})
11384
}
@@ -116,7 +87,7 @@ class Post extends ContentModelNode {
11687
const renderPost = () => {
11788
const data = {
11889
...contentModel,
119-
post: this,
90+
post: Post.serialize(this),
12091
settings,
12192
debug
12293
}
@@ -142,7 +113,7 @@ class Post extends ContentModelNode {
142113

143114
const renderAttachments = () => {
144115
return Promise.all(
145-
this.attachments.map(attachment => {
116+
this.subtree.attachments.map(attachment => {
146117
return attachment.render(renderer)
147118
})
148119
)

src/compiler/contentModel/models/homepage.js

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const { join } = require('path')
2-
const ContentModelNode = require('../../../lib/ContentModelNode')
2+
const ContentModelEntryNode = require('../../../lib/ContentModelEntryNode')
33
const { templateExtensions } = require('../../../lib/contentModelHelpers')
4-
const { parseTextEntry } = require('../../../lib/parseTextEntry')
54

65
const models = {
76
Attachment: require('./attachment'),
@@ -11,29 +10,16 @@ const models = {
1110
const defaultSettings = {
1211
homepageDirectory: 'homepage'
1312
}
14-
class Homepage extends ContentModelNode {
15-
constructor(fsNode, context, settings = defaultSettings) {
16-
super(fsNode, context, settings)
17-
18-
const entryProperties = parseTextEntry(this.fsNode, this.subtree.indexFile)
19-
20-
const pageContext = {
21-
title: entryProperties.title,
22-
slug: entryProperties.slug,
23-
permalink: this.permalink,
24-
outputPath: this.outputPath
13+
class Homepage extends ContentModelEntryNode {
14+
static serialize(homepage) {
15+
return {
16+
...homepage,
17+
attachments: homepage.subtree.attachments
2518
}
19+
}
2620

27-
Object.assign(this, {
28-
...entryProperties,
29-
...pageContext,
30-
context: this.context,
31-
attachments: this.subtree.attachments.map(attachmentNode => {
32-
return new models.Attachment(attachmentNode, {
33-
homepage: pageContext
34-
})
35-
})
36-
})
21+
constructor(fsNode, context, settings = defaultSettings) {
22+
super(fsNode, context, settings)
3723
}
3824

3925
getPermalink() {
@@ -44,20 +30,10 @@ class Homepage extends ContentModelNode {
4430
return this.context.peek().outputPath
4531
}
4632

47-
parseSubtree() {
48-
const tree = {
49-
indexFile: this.fsNode,
50-
attachments: []
51-
}
52-
53-
if (!this.fsNode.children || !this.fsNode.children.length) {
54-
return tree
55-
}
56-
57-
const indexFileNameOptions = ['index']
58-
59-
const matchers = {
33+
getSubtreeMatchers() {
34+
return {
6035
indexFile: (fsNode) => {
36+
const indexFileNameOptions = ['index']
6137
if (fsNode.children) {
6238
return false
6339
}
@@ -69,22 +45,43 @@ class Homepage extends ContentModelNode {
6945

7046
attachment: (fsNode) => true
7147
}
48+
}
49+
50+
parseSubtree() {
51+
const tree = {
52+
indexFile: this.fsNode,
53+
attachments: []
54+
}
55+
56+
if (!this.fsNode.children || !this.fsNode.children.length) {
57+
return tree
58+
}
59+
60+
const context = {
61+
homepage: {
62+
title: this.title,
63+
slug: this.slug,
64+
permalink: this.permalink,
65+
outputPath: this.outputPath
66+
}
67+
}
7268

7369
this.fsNode.children.forEach(childNode => {
74-
if (matchers.indexFile(childNode, indexFileNameOptions)) {
75-
tree.indexFile = childNode
70+
if (this.matchers.indexFile(childNode)) {
7671
return
7772
}
78-
if (matchers.attachment(childNode)) {
79-
tree.attachments.push(childNode)
73+
if (this.matchers.attachment(childNode)) {
74+
tree.attachments.push(
75+
new models.Attachment(childNode, context)
76+
)
8077
}
8178
})
8279

8380
return tree
8481
}
8582

8683
afterEffects(contentModel) {
87-
this.attachments.forEach(attachment => {
84+
this.subtree.attachments.forEach(attachment => {
8885
attachment.afterEffects(contentModel)
8986
})
9087
}
@@ -110,7 +107,7 @@ class Homepage extends ContentModelNode {
110107

111108
const renderAttachments = () => {
112109
return Promise.all(
113-
this.attachments.map(attachment => {
110+
this.subtree.attachments.map(attachment => {
114111
return attachment.render(renderer)
115112
})
116113
)

0 commit comments

Comments
 (0)