Skip to content

Commit b3b175a

Browse files
committed
[CM2] WIP rendering2
- Bring back outputPath to models right next to permalinks - Copy rendering as rendering2 and make rough edits (WIP) - Render each entry with a set of preferred partials involving the entry's 'template' field, 'contentType' field and a fallback - Should copy attachments as part of their context entry - Should render category, tags and posts as part of rendering collection - Introduce a temporary flag 'compilerVersion' to be able to test the new setup (not tested yet)
1 parent ee03aa6 commit b3b175a

23 files changed

Lines changed: 648 additions & 18 deletions
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
1+
const { join } = require('path')
2+
const settings = require('../../../settings').getSettings()
3+
14
function asset(node) {
2-
return node
5+
const permalink = (
6+
settings.permalinkPrefix +
7+
[settings.assetsDirectory, node.name].join('/')
8+
)
9+
10+
const outputPath = join(
11+
settings.out,
12+
settings.assetsDirectory,
13+
node.name
14+
)
15+
16+
return {
17+
...node,
18+
permalink,
19+
outputPath
20+
}
321
}
422

523
module.exports = asset

src/compiler/contentModel2/models/attachment.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { join } = require('path')
12
const settings = require('../../../settings').getSettings()
23

34
function attachment(node, context) {
@@ -12,10 +13,20 @@ function attachment(node, context) {
1213
].filter(Boolean).join('/')
1314
)
1415

16+
const outputPath = join(...[
17+
settings.out,
18+
context.collection?.slug,
19+
context.category?.isDefaultCategory ? '' : context.category.slug,
20+
context.post?.slug,
21+
context.page?.slug,
22+
node.name
23+
].filter(Boolean))
24+
1525
return {
1626
...node,
1727
context,
1828
permalink,
29+
outputPath,
1930
date: new Date(node.stats.birthtime || Date.now())
2031
}
2132
}

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { join } = require('path')
12
const frontMatter = require('front-matter')
23
const makeSlug = require('slug')
34
const settings = require('../../../../settings').getSettings()
@@ -10,16 +11,23 @@ const models = {
1011
function category(node, context) {
1112
if (node.isDefaultCategory) {
1213
const slug = makeSlug(settings.defaultCategoryName)
14+
const permalink = (
15+
settings.permalinkPrefix +
16+
context.collection.slug
17+
)
18+
const outputPath = join(
19+
settings.out,
20+
context.collection.slug,
21+
'index.html'
22+
)
1323
return {
1424
context,
1525
childContentType: context.collection.childContentType,
1626
content: '',
1727
title: settings.defaultCategoryName,
1828
slug,
19-
permalink: (
20-
settings.permalinkPrefix +
21-
[ context.collection.slug, slug ].join('/')
22-
),
29+
permalink,
30+
outputPath,
2331
isDefaultCategory: true,
2432
posts: [],
2533
attachments: []
@@ -32,15 +40,21 @@ function category(node, context) {
3240
const indexProps = indexFile ? frontMatter(indexFile) : {}
3341

3442
const slug = indexProps.attributes?.slug || makeSlug(node.name)
43+
const permalink = (
44+
settings.permalinkPrefix +
45+
[ context.collection.slug, slug ].join('/')
46+
)
47+
const outputPath = join(
48+
settings.out,
49+
context.collection.slug, slug, 'index.html'
50+
)
51+
3552
const categoryContext = {
3653
...indexProps.attributes,
3754
childContentType: indexProps.attributes?.childContentType || context.collection.childContentType,
3855
title: indexProps.attributes?.title || node.name,
3956
slug,
40-
permalink: (
41-
settings.permalinkPrefix +
42-
[ context.collection.slug, slug ].join('/')
43-
)
57+
permalink
4458
}
4559

4660
const tree = {
@@ -90,7 +104,8 @@ function category(node, context) {
90104
...categoryContext,
91105
...tree,
92106
context: context,
93-
content: indexProps.content || ''
107+
content: indexProps.content || '',
108+
outputPath
94109
}
95110
}
96111

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { join } = require('path')
12
const _ = require('lodash')
23
const frontMatter = require('front-matter')
34
const makeSlug = require('slug')
@@ -69,12 +70,14 @@ function collection(node) {
6970
const indexProps = indexFile ? frontMatter(indexFile.content) : {}
7071

7172
const slug = indexProps.attributes?.slug || makeSlug(node.name)
73+
const permalink = settings.permalinkPrefix + slug
74+
const outputPath = join(settings.out, slug, 'index.html')
7275
const context = {
7376
...indexProps.attributes,
7477
childContentType: indexProps.attributes?.childContentType || 'text',
7578
title: indexProps.attributes?.title || node.name,
7679
slug,
77-
permalink: settings.permalinkPrefix + slug
80+
permalink
7881
}
7982

8083
node.children.forEach(childNode => {
@@ -106,7 +109,8 @@ function collection(node) {
106109
return {
107110
...context,
108111
...tree,
109-
content: indexProps.content || ''
112+
content: indexProps.content || '',
113+
outputPath
110114
}
111115
}
112116

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { join } = require('path')
12
const settings = require('../../../../settings').getSettings()
23
const { parseTags } = require('../../helpers')
34
const models = {
@@ -18,6 +19,14 @@ function post(node, context) {
1819
(node.children ? '' : '.html')
1920
)
2021

22+
const outputPath = join(...[
23+
settings.out,
24+
context.collection.slug,
25+
(context.category.isDefaultCategory ? '' : context.category.slug),
26+
baseEntryProps.slug,
27+
(node.children ? 'index' : '')
28+
].filter(Boolean)) + '.html'
29+
2130
const postContext = {
2231
title: baseEntryProps.title,
2332
slug: baseEntryProps.slug,
@@ -33,6 +42,7 @@ function post(node, context) {
3342
return models.tag(tagName, context)
3443
}),
3544
date: new Date(baseEntryProps.date || baseEntryProps.stats.birthtime || Date.now()),
45+
outputPath,
3646
attachments: baseEntryProps.attachments.map(a => a({
3747
...context,
3848
post: postContext

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1+
const { join } = require('path')
12
const makeSlug = require('slug')
23
const settings = require('../../../../settings').getSettings()
34

45
function tag(name, context) {
6+
const slug = makeSlug(name)
7+
const permalink = (
8+
settings.permalinkPrefix +
9+
context.collection.slug + '/tags/' + slug
10+
)
11+
const outputPath = join(
12+
settings.out,
13+
context.collection.slug,
14+
slug,
15+
'index.html'
16+
)
517
return {
618
name,
7-
permalink: (
8-
settings.permalinkPrefix +
9-
context.collection.slug + '/tags/' + makeSlug(name)
10-
)
19+
slug,
20+
permalink
1121
}
1222
}
1323

src/compiler/contentModel2/models/homepage.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { join } = require('path')
12
const settings = require('../../../settings').getSettings()
23
const models = {
34
_baseEntry: require('./_baseEntry')
@@ -8,6 +9,8 @@ function homepage(node) {
89

910
const permalink = settings.permalinkPrefix
1011

12+
const outputPath = join(settings.out, 'index.html')
13+
1114
const pageContext = {
1215
title: baseEntryProps.title,
1316
slug: baseEntryProps.slug,
@@ -19,7 +22,8 @@ function homepage(node) {
1922
attachments: baseEntryProps.attachments.map(a => a({
2023
page: pageContext
2124
})),
22-
permalink
25+
permalink,
26+
outputPath
2327
}
2428
}
2529

src/compiler/contentModel2/models/subpage.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { join } = require('path')
12
const settings = require('../../../settings').getSettings()
23
const models = {
34
_baseEntry: require('./_baseEntry')
@@ -12,6 +13,12 @@ function subpage(node) {
1213
(node.children ? '' : '.html')
1314
)
1415

16+
const outputPath = join(...[
17+
settings.out,
18+
baseEntryProps.slug,
19+
(node.children ? 'index' : '')
20+
].filter(Boolean)) + '.html'
21+
1522
const pageContext = {
1623
title: baseEntryProps.title,
1724
slug: baseEntryProps.slug,
@@ -21,6 +28,7 @@ function subpage(node) {
2128
return {
2229
...baseEntryProps,
2330
...pageContext,
31+
outputPath,
2432
attachments: baseEntryProps.attachments.map(a => a({
2533
page: pageContext
2634
}))

src/compiler/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
const Debug = require('../debug')
2+
const Settings = require('../settings').getSettings()
23
const FileSystem = require('./fileSystem')
34
const ContentModel = require('./contentModel')
45
const Rendering = require('./rendering')
56

7+
const compile2 = async () => {
8+
const ContentModel2 = require('./contentModel2')
9+
const Rendering2 = require('./rendering2')
10+
11+
const fileSystemTree = await FileSystem.exploreTree()
12+
const contentModel = ContentModel2.create(fileSystemTree)
13+
await Rendering2.render(contentModel)
14+
Debug.timeEnd('compiler')
15+
return {
16+
fileSystemTree,
17+
contentModel
18+
}
19+
}
20+
621
const compile = async () => {
722
Debug.timeStart('compiler')
23+
if (Settings.compilerVersion === 2) {
24+
return compile2()
25+
}
826
const fileSystemTree = await FileSystem.exploreTree()
927
const contentModel = await ContentModel.create(fileSystemTree)
1028
await Rendering.render(contentModel)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const filterPosts = (page, posts) => {
2+
const filters = {
3+
// TODO: i18n
4+
exclude: page['exclude post types'],
5+
include: page['include post types']
6+
}
7+
8+
const excludedPostTypes = filters.exclude ?
9+
filters.exclude.split(',').map(t => t.trim()) :
10+
[]
11+
12+
const includedPostTypes = filters.include ?
13+
filters.include.split(',').map(t => t.trim()) :
14+
[]
15+
16+
return posts
17+
.filter(({ type }) => !excludedPostTypes.includes(type))
18+
.filter(({ type }) => (
19+
!includedPostTypes.length || includedPostTypes.includes(type))
20+
)
21+
}
22+
23+
module.exports = {
24+
filterPosts
25+
}

0 commit comments

Comments
 (0)