Skip to content

Commit df0dd10

Browse files
committed
[CM2] WIP continues for rendering2
- Add absolutePath field into fileSystemTree to vastly simplify copying assets - Add a copy method to renderer for copying assets/files - Implement copyAssets view - Delete the legacy localAsset (attachment) view. Copy them as part of their parent entry's view - Remove pagination from homepage. Pagination takes places in collections. Collection indexes may be used as homepage (in the future) - Implement post view as part of collection - Implement tags and category views as part of collection. And paginate them - Assume a postsPerPage field in categories and collections. Cascade: settings.ppp > collection.ppp > category.ppp - Render categories, tags and posts as part of collection view - Add date field to asset just like in attachment - Fix attachment outputPath (it was assuming it's always in a category) - Homepage passes a 'homepage' (instead of 'page') context to its attachments, so it doesn't count in attachment outputPath - Delete filterPosts helper. Filtering posts sounds like a template's job - Init settings in CM2 test so there's some settings.out to work with - Call move getSettings() call into function runtimes, so settings.out is not undefined when testing contentModel2 - Update CM2 test fixtures to include absolutePath in FS nodes
1 parent b3b175a commit df0dd10

24 files changed

+293
-201
lines changed

src/compiler/contentModel2/models/asset.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const { join } = require('path')
2-
const settings = require('../../../settings').getSettings()
2+
const Settings = require('../../../settings')
33

44
function asset(node) {
5+
const settings = Settings.getSettings()
6+
57
const permalink = (
68
settings.permalinkPrefix +
79
[settings.assetsDirectory, node.name].join('/')
@@ -16,7 +18,8 @@ function asset(node) {
1618
return {
1719
...node,
1820
permalink,
19-
outputPath
21+
outputPath,
22+
date: new Date(node.stats.birthtime || Date.now())
2023
}
2124
}
2225

src/compiler/contentModel2/models/attachment.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const { join } = require('path')
2-
const settings = require('../../../settings').getSettings()
2+
const Settings = require('../../../settings')
33

44
function attachment(node, context) {
5+
const settings = Settings.getSettings()
6+
57
const permalink = (
68
settings.permalinkPrefix +
79
[
@@ -16,7 +18,7 @@ function attachment(node, context) {
1618
const outputPath = join(...[
1719
settings.out,
1820
context.collection?.slug,
19-
context.category?.isDefaultCategory ? '' : context.category.slug,
21+
context.category?.isDefaultCategory ? '' : context.category?.slug,
2022
context.post?.slug,
2123
context.page?.slug,
2224
node.name

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
const { join } = require('path')
22
const frontMatter = require('front-matter')
33
const makeSlug = require('slug')
4-
const settings = require('../../../../settings').getSettings()
4+
const Settings = require('../../../../settings')
55
const { isTemplateFile } = require('../../helpers')
66
const models = {
77
post: require('./post'),
88
attachment: require('../attachment')
99
}
1010

1111
function category(node, context) {
12+
const settings = Settings.getSettings()
13+
1214
if (node.isDefaultCategory) {
1315
const slug = makeSlug(settings.defaultCategoryName)
1416
const permalink = (

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const { join } = require('path')
22
const _ = require('lodash')
33
const frontMatter = require('front-matter')
44
const makeSlug = require('slug')
5-
const settings = require('../../../../settings').getSettings()
5+
const Settings = require('../../../../settings')
66
const { isTemplateFile } = require('../../helpers')
77
const models = {
88
attachment: require('../attachment'),
@@ -11,6 +11,8 @@ const models = {
1111
}
1212

1313
function collection(node) {
14+
const settings = Settings.getSettings()
15+
1416
function collectPostTags(post) {
1517
post.tags.forEach(postTag => {
1618
let collectionTag = tree.tags.find(t => t.name === postTag.name)

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
const { join } = require('path')
2-
const settings = require('../../../../settings').getSettings()
2+
const Settings = require('../../../../settings')
33
const { parseTags } = require('../../helpers')
44
const models = {
55
_baseEntry: require('../_baseEntry'),
66
tag: require('./tag')
77
}
88

99
function post(node, context) {
10+
const settings = Settings.getSettings()
11+
1012
const baseEntryProps = models._baseEntry(node, ['index', 'post'])
1113

1214
const permalink = (

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
const { join } = require('path')
22
const makeSlug = require('slug')
3-
const settings = require('../../../../settings').getSettings()
3+
const Settings = require('../../../../settings')
44

55
function tag(name, context) {
6+
const settings = Settings.getSettings()
67
const slug = makeSlug(name)
78
const permalink = (
89
settings.permalinkPrefix +

src/compiler/contentModel2/models/homepage.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
const { join } = require('path')
2-
const settings = require('../../../settings').getSettings()
2+
const Settings = require('../../../settings')
33
const models = {
44
_baseEntry: require('./_baseEntry')
55
}
66

77
function homepage(node) {
8+
const settings = Settings.getSettings()
9+
810
const baseEntryProps = models._baseEntry(node, ['index'])
911

1012
const permalink = settings.permalinkPrefix
@@ -20,7 +22,7 @@ function homepage(node) {
2022
return {
2123
...baseEntryProps,
2224
attachments: baseEntryProps.attachments.map(a => a({
23-
page: pageContext
25+
homepage: pageContext
2426
})),
2527
permalink,
2628
outputPath

src/compiler/contentModel2/models/subpage.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
const { join } = require('path')
2-
const settings = require('../../../settings').getSettings()
2+
const Settings = require('../../../settings')
33
const models = {
44
_baseEntry: require('./_baseEntry')
55
}
66

77
function subpage(node) {
8+
const settings = Settings.getSettings()
9+
810
const baseEntryProps = models._baseEntry(node, ['index', 'page'])
911

1012
const permalink = (

src/compiler/fileSystem.js

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const _explore = async (currentPath, depth = 0) => {
4747
const baseProperties = {
4848
name: fileName,
4949
path: relative(rootPath, accumulatedPath),
50+
absolutePath: accumulatedPath,
5051
stats: { birthtime },
5152
depth,
5253
}

src/compiler/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ const compile2 = async () => {
1010

1111
const fileSystemTree = await FileSystem.exploreTree()
1212
const contentModel = ContentModel2.create(fileSystemTree)
13+
14+
return {}
15+
1316
await Rendering2.render(contentModel)
1417
Debug.timeEnd('compiler')
1518
return {

src/compiler/rendering2/helpers/filterPosts.js

-25
This file was deleted.

src/compiler/rendering2/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = {
1111
async render(contentModel) {
1212
await Renderer.init()
1313
return Promise.all([
14-
Views.copyAssets(contentModel),
14+
Views.copyAssets(Renderer, contentModel),
1515
Views.renderHomepage(Renderer, contentModel),
1616
Views.renderSubpages(Renderer, contentModel),
1717
Views.renderCollections(Renderer, contentModel)

src/compiler/rendering2/renderer.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const Handlebars = require('handlebars')
2-
const { stat, readdir, writeFile } = require('fs/promises')
2+
const { stat, readdir, writeFile, cp } = require('fs/promises')
33
const { dirname, extname, join } = require('path')
44
const { debugLog } = require('../../debug')
55
const { decorate } = require('../../decorations')
@@ -89,8 +89,22 @@ const compile = ({ data, content, options = {} }) => {
8989
return template(data)
9090
}
9191

92+
const copy = async ({ src, dest, recursive }) => {
93+
try {
94+
return await cp(src, dest, { recursive })
95+
} catch (e) {
96+
if (e.code === 'ENOENT') {
97+
debugLog('failed copying file that no longer exists', e)
98+
} else {
99+
debugLog('failed copying file', e)
100+
}
101+
return Promise.resolve()
102+
}
103+
}
104+
92105
module.exports = {
93106
init,
94107
render,
95-
compile
108+
compile,
109+
copy
96110
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const copyAssets = async (Renderer, contentModel) => {
2+
return Promise.all(
3+
contentModel.assets.map(node => {
4+
return Renderer.copy({
5+
src: node.absolutePath,
6+
dest: node.outputPath,
7+
recursive: !!node.children
8+
})
9+
})
10+
)
11+
}
12+
13+
module.exports = copyAssets

src/compiler/rendering2/views/collection/attachment.js

-94
This file was deleted.

0 commit comments

Comments
 (0)