Skip to content

Commit 739acb1

Browse files
committed
[CM2] Introduce inline collection contentTypes 👇
Collection index can have these properties: - contentType - categoryContentType - entryContentType - categoryAlias - categoriesAlias - entryAlias - entriesAlias - defaultCategoryName 'defaultCategoryName' is to allow custom defaultCategory names per collection. 'ContentType' fields are used for template lookup during rendering. 'Alias' fields are to assist contentType-specific templates' readability. for example, in a custom collection template, instead of: {{#each categories}} you can say: {{#each technologies}} given you set categoriesAlias: technologies in the collection index. Similar logic applies to all aliases and their template counterparts. You can talk domain-driven in your templates now 💫 The idea is to brace myself to standalone contentType definitions the CMS frontend will eventually require. For now something resembling them can be inlined to collection frontMatter. Besides, compiler probably won't ever care about much more than what is implemented here. Also: - Fix default category index overriding collection index by giving it an actual slug, outputPath etc. - Remove the unneeded Settings dependency from attachment, category, post and tag - Lookup for a 'default.hbs' during rendering as a last resort, instead of 'index.hbs' - Get rid of the default 'text' contentType What's left: A theme2 I guess...
1 parent 877c7c7 commit 739acb1

File tree

10 files changed

+76
-45
lines changed

10 files changed

+76
-45
lines changed

‎src/compiler/contentModel2/models/attachment.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
const { join } = require('path')
2-
const Settings = require('../../../settings')
32

43
function attachment(node, context) {
5-
const settings = Settings.getSettings()
6-
74
const permalink = [
85
context.page?.permalink ||
96
context.post?.permalink ||

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

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const { join } = require('path')
22
const frontMatter = require('front-matter')
33
const makeSlug = require('slug')
4-
const Settings = require('../../../../settings')
54
const { isTemplateFile, Markdown } = require('../../helpers')
65
const models = {
76
post: require('./post'),
@@ -24,8 +23,6 @@ function parseContent(node, content) {
2423
}
2524

2625
function category(node, context) {
27-
const settings = Settings.getSettings()
28-
2926
function linkPosts(post, postIndex, posts) {
3027
post.links = {}
3128
if (postIndex > 0) {
@@ -42,20 +39,31 @@ function category(node, context) {
4239
}
4340
}
4441

42+
const entriesAlias = context.collection.entriesAlias
43+
4544
if (node.isDefaultCategory) {
46-
return {
45+
const title = context.collection.defaultCategoryName
46+
const slug = makeSlug(title)
47+
48+
const defaultCategory = {
4749
context,
48-
childContentType: context.collection.childContentType,
50+
contentType: context.collection.categoryContentType,
4951
content: '',
5052
contentRaw: '',
51-
slug: '',
52-
title: settings.defaultCategoryName,
53-
permalink: context.collection.permalink,
54-
outputPath: context.collection.outputPath,
53+
slug,
54+
title,
55+
permalink: [context.collection.permalink, slug].join('/'),
56+
outputPath: join(context.collection.outputPath, slug),
5557
isDefaultCategory: true,
5658
posts: [],
5759
attachments: []
5860
}
61+
62+
if (entriesAlias) {
63+
defaultCategory[entriesAlias] = defaultCategory.posts
64+
}
65+
66+
return defaultCategory
5967
}
6068

6169
const indexFile = node.children.find(isCategoryIndexFile)
@@ -67,7 +75,7 @@ function category(node, context) {
6775

6876
const categoryContext = {
6977
...indexProps.attributes,
70-
childContentType: indexProps.attributes?.childContentType || context.collection.childContentType,
78+
contentType: context.collection.categoryContentType,
7179
title: indexProps.attributes?.title || node.name,
7280
slug,
7381
permalink,
@@ -79,6 +87,10 @@ function category(node, context) {
7987
attachments: []
8088
}
8189

90+
if (entriesAlias) {
91+
tree[entriesAlias] = tree.posts
92+
}
93+
8294
node.children.forEach(childNode => {
8395
if (isCategoryIndexFile(childNode)) {
8496
return
@@ -110,7 +122,7 @@ function category(node, context) {
110122
return {
111123
...categoryContext,
112124
...tree,
113-
context: context,
125+
context,
114126
contentRaw,
115127
content
116128
}

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,30 @@ function collection(node) {
9797
const outputPath = join(settings.out, slug)
9898
const context = {
9999
...indexProps.attributes,
100-
childContentType: indexProps.attributes?.childContentType || 'text',
100+
contentType: indexProps.attributes?.contentType || 'default',
101+
categoryContentType: indexProps.attributes?.categoryContentType || 'default',
102+
entryContentType: indexProps.attributes?.entryContentType || 'default',
103+
categoryAlias: indexProps.attributes?.categoryAlias,
104+
categoriesAlias: indexProps.attributes?.categoriesAlias,
105+
entryAlias: indexProps.attributes?.entryAlias,
106+
entriesAlias: indexProps.attributes?.entriesAlias,
107+
defaultCategoryName: indexProps.attributes?.defaultCategoryName || settings.defaultCategoryName,
101108
title: indexProps.attributes?.title || node.name,
102109
slug,
103110
permalink,
104111
outputPath
105112
}
106113

114+
const categoriesAlias = indexProps.attributes?.categoriesAlias
115+
if (categoriesAlias) {
116+
tree[categoriesAlias] = tree.categories
117+
}
118+
119+
const entriesAlias = indexProps.attributes?.entriesAlias
120+
if (entriesAlias) {
121+
tree[entriesAlias] = tree.posts
122+
}
123+
107124
node.children.forEach(childNode => {
108125
if (isCollectionIndexFile(childNode)) {
109126
return
@@ -127,7 +144,7 @@ function collection(node) {
127144
collectPostTags(post)
128145
})
129146

130-
const contentRaw = indexProps.content || ''
147+
const contentRaw = indexProps.body || ''
131148
const content = indexFile ?
132149
parseContent(indexFile, contentRaw) :
133150
''

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
const { join } = require('path')
2-
const Settings = require('../../../../settings')
32
const { parseArray } = require('../../helpers')
43
const models = {
54
_baseEntry: require('../_baseEntry'),
65
tag: require('./tag')
76
}
87

98
function post(node, context) {
10-
const settings = Settings.getSettings()
11-
129
const baseEntryProps = models._baseEntry(node, ['index', 'post'])
1310

1411
const permalink = [
@@ -29,7 +26,7 @@ function post(node, context) {
2926
...baseEntryProps,
3027
...postContext,
3128
context,
32-
contentType: baseEntryProps.contentType || context.category.childContentType,
29+
contentType: context.collection.entryContentType,
3330
tags: parseArray(baseEntryProps.tags).map(tagName => {
3431
return models.tag(tagName, context)
3532
}),

‎src/compiler/contentModel2/models/collection/tag.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
const { join } = require('path')
22
const makeSlug = require('slug')
3-
const Settings = require('../../../../settings')
43

54
function tag(name, context) {
6-
const settings = Settings.getSettings()
75
const slug = makeSlug(name)
86
const permalink = [context.collection.permalink, 'tags', slug].join('/')
97
const outputPath = join(context.collection.outputPath, slug)

‎src/compiler/rendering2/views/collection/category.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,28 @@ const renderCategories = (Renderer, contentModel, collection) => {
1717
),
1818
outputDir: category.outputPath,
1919
render: async ({ outputPath, pageOfPosts, paginationData }) => {
20+
const data = {
21+
...contentModel,
22+
collection,
23+
category,
24+
pagination: paginationData,
25+
posts: pageOfPosts,
26+
settings,
27+
debug: Debug.getDebug()
28+
}
29+
const categoryAlias = category.context.collection.categoryAlias
30+
if (categoryAlias) {
31+
data[categoryAlias] = data.category
32+
}
2033
return Renderer.render({
2134
templates: [
2235
`pages/${category.template}`,
23-
`pages/category/${category.childContentType}`,
24-
`pages/category`
36+
`pages/category/${category.contentType}`,
37+
`pages/category/default`
2538
],
2639
outputPath,
2740
content: category.content,
28-
data: {
29-
...contentModel,
30-
collection,
31-
category,
32-
pagination: paginationData,
33-
posts: pageOfPosts,
34-
settings,
35-
debug: Debug.getDebug()
36-
}
41+
data
3742
})
3843
}
3944
})

‎src/compiler/rendering2/views/collection/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ const renderCollections = async (Renderer, contentModel) => {
2222
return Renderer.render({
2323
templates: [
2424
`pages/${collection.template}`,
25-
`pages/collection/${collection.childContentType}`,
26-
`pages/collection`
25+
`pages/collection/${collection.contentType}`,
26+
`pages/collection/default`
2727
],
2828
outputPath,
2929
content: collection.content,

‎src/compiler/rendering2/views/collection/post.js

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,29 @@ const Debug = require('../../../../debug')
44

55
const renderPosts = (Renderer, contentModel, collection) => {
66
const compilation = collection.posts.map(post => {
7+
const data = {
8+
...contentModel,
9+
collection,
10+
post,
11+
settings: Settings.getSettings(),
12+
debug: Debug.getDebug()
13+
}
14+
const entryAlias = post.context.collection.entryAlias
15+
if (entryAlias) {
16+
data[entryAlias] = data.post
17+
}
718
const renderPage = Renderer.render({
819
templates: [
920
`pages/${post.template}`,
1021
`pages/post/${post.contentType}`,
11-
`pages/post`
22+
`pages/post/default`
1223
],
1324
outputPath: join(...[
1425
post.outputPath,
1526
post.hasIndex ? 'index' : ''
1627
].filter(Boolean)) + '.html',
1728
content: post.content,
18-
data: {
19-
...contentModel,
20-
collection,
21-
post,
22-
settings: Settings.getSettings(),
23-
debug: Debug.getDebug()
24-
}
29+
data
2530
})
2631

2732
const copyAttachments = post.attachments.map(node => {

‎src/compiler/rendering2/views/homepage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const renderHomepage = async (Renderer, contentModel) => {
99
templates: [
1010
`pages/${homepage.template}`,
1111
`pages/homepage/${homepage.contentType}`,
12-
`pages/homepage`
12+
`pages/homepage/default`
1313
],
1414
outputPath: join(homepage.outputPath, 'index.html'),
1515
content: homepage.content,

‎src/compiler/rendering2/views/subpage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const renderSubpages = (Renderer, contentModel) => {
88
templates: [
99
`pages/${subpage.template}`,
1010
`pages/subpage/${subpage.contentType}`,
11-
`pages/subpage`
11+
`pages/subpage/default`
1212
],
1313
outputPath: join(...[
1414
subpage.outputPath,

0 commit comments

Comments
 (0)