Skip to content

Commit ee03aa6

Browse files
committed
[CM2] Add basic props & pass ctx to attachments
1 parent 59c1028 commit ee03aa6

8 files changed

Lines changed: 143 additions & 63 deletions

File tree

src/compiler/contentModel2/models/_baseEntry.js

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,54 @@
1+
const _ = require('lodash')
12
const frontMatter = require('front-matter')
23
const slug = require('slug')
3-
const {
4-
templateExtensions,
5-
isTemplateFile,
6-
removeExtension
7-
} = require('../helpers')
4+
const { templateExtensions, removeExtension } = require('../helpers')
85

96
const models = {
107
attachment: require('./attachment')
118
}
129

13-
function _baseEntry(node, indexFileNameOptions) {
14-
const dotlessTemplateExtensions = templateExtensions.map(e => e.substring(1))
15-
const entryFile = node.children ?
16-
node.children.find(child => {
17-
return isTemplateFile(child) && child.name.match(
18-
new RegExp(`^(${indexFileNameOptions.join('|')})\.(${dotlessTemplateExtensions.join('|')})$`, 'i')
19-
)
20-
}) :
21-
node
10+
const isIndexFile = (node, nameOptions) => {
11+
const names = nameOptions.join('|')
12+
const extensions = templateExtensions.join('|')
13+
const namePattern = new RegExp(`^(${names})(${extensions})$`, 'i')
14+
return !node.children && node.name.match(namePattern)
15+
}
16+
17+
function parseFolderedEntry(node, indexFileNameOptions) {
18+
const tree = {
19+
indexFile: null,
20+
attachments: []
21+
}
22+
node.children.forEach(childNode => {
23+
if (isIndexFile(childNode, indexFileNameOptions)) {
24+
tree.indexFile = childNode
25+
return
26+
}
27+
tree.attachments.push(models.attachment.bind(null, childNode))
28+
})
2229

23-
const attachments = (node.children || [])
24-
.filter(child => child !== entryFile)
25-
.map(child => models.attachment(child))
30+
return {
31+
...tree,
32+
name: node.name
33+
}
34+
}
2635

36+
function _baseEntry(node, indexFileNameOptions) {
37+
const folderedEntry = node.children ?
38+
parseFolderedEntry(node, indexFileNameOptions) :
39+
undefined
40+
const entryFile = folderedEntry?.indexFile || node
2741
const { attributes, body } = frontMatter(entryFile.content)
42+
const entryName = removeExtension(
43+
folderedEntry?.name || entryFile.name
44+
)
45+
const attachments = folderedEntry?.attachments || []
2846

2947
return {
30-
...node,
48+
..._.omit(node, 'children'),
3149
...attributes,
32-
title: attributes.title || removeExtension(node.children ? node.name : entryFile.name),
33-
slug: attributes.slug || slug(removeExtension(node.children ? node.name : entryFile.name), '-'),
50+
title: attributes.title || entryName,
51+
slug: attributes.slug || slug(entryName),
3452
content: body || '',
3553
attachments
3654
}
Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
1-
function attachment(node) {
2-
return node
1+
const settings = require('../../../settings').getSettings()
2+
3+
function attachment(node, context) {
4+
const permalink = (
5+
settings.permalinkPrefix +
6+
[
7+
context.collection?.slug,
8+
context.category?.isDefaultCategory ? '' : context.category.slug,
9+
context.post?.slug,
10+
context.page?.slug,
11+
node.name
12+
].filter(Boolean).join('/')
13+
)
14+
15+
return {
16+
...node,
17+
context,
18+
permalink,
19+
date: new Date(node.stats.birthtime || Date.now())
20+
}
321
}
422

523
module.exports = attachment

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ function category(node, context) {
6262
)
6363
}
6464
return tree.attachments.push(
65-
models.attachment(childNode)
65+
models.attachment(childNode, {
66+
...context,
67+
category: categoryContext
68+
})
6669
)
6770
}
6871
if (childNode.children.some(c => isTemplateFile(c) && c.name.match(/^(index|post)\..+$/))) {
@@ -74,7 +77,10 @@ function category(node, context) {
7477
)
7578
}
7679
return tree.push(
77-
models.attachment(childNode)
80+
models.attachment(childNode, {
81+
...context,
82+
category: categoryContext
83+
})
7884
)
7985
})
8086

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,6 @@ const models = {
1010
}
1111

1212
function collection(node) {
13-
const tree = {
14-
categories: [],
15-
posts: [],
16-
tags: [],
17-
attachments: []
18-
}
19-
20-
const indexFile = node.children.find(child => {
21-
return isTemplateFile(child) && child.name.match(/^collection\..+$/)
22-
})
23-
24-
const indexProps = indexFile ? frontMatter(indexFile.content) : {}
25-
26-
const slug = indexProps.attributes?.slug || makeSlug(node.name)
27-
const context = {
28-
...indexProps.attributes,
29-
childContentType: indexProps.attributes?.childContentType || 'text',
30-
title: indexProps.attributes?.title || node.name,
31-
slug,
32-
permalink: settings.permalinkPrefix + slug
33-
}
34-
3513
function collectPostTags(post) {
3614
post.tags.forEach(postTag => {
3715
let collectionTag = tree.tags.find(t => t.name === postTag.name)
@@ -77,6 +55,28 @@ function collection(node) {
7755
tree.posts.push(uncategorizedPost)
7856
}
7957

58+
const tree = {
59+
categories: [],
60+
posts: [],
61+
tags: [],
62+
attachments: []
63+
}
64+
65+
const indexFile = node.children.find(child => {
66+
return isTemplateFile(child) && child.name.match(/^collection\..+$/)
67+
})
68+
69+
const indexProps = indexFile ? frontMatter(indexFile.content) : {}
70+
71+
const slug = indexProps.attributes?.slug || makeSlug(node.name)
72+
const context = {
73+
...indexProps.attributes,
74+
childContentType: indexProps.attributes?.childContentType || 'text',
75+
title: indexProps.attributes?.title || node.name,
76+
slug,
77+
permalink: settings.permalinkPrefix + slug
78+
}
79+
8080
node.children.forEach(childNode => {
8181
if (!childNode.children) {
8282
if (isTemplateFile(childNode)) {

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

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,36 @@ const models = {
77

88
function post(node, context) {
99
const baseEntryProps = models._baseEntry(node, ['index', 'post'])
10+
11+
const permalink = (
12+
settings.permalinkPrefix +
13+
[
14+
context.collection.slug,
15+
context.category.isDefaultCategory ? '' : context.category.slug,
16+
baseEntryProps.slug
17+
].filter(Boolean).join('/') +
18+
(node.children ? '' : '.html')
19+
)
20+
21+
const postContext = {
22+
title: baseEntryProps.title,
23+
slug: baseEntryProps.slug,
24+
permalink
25+
}
26+
1027
return {
1128
...baseEntryProps,
29+
...postContext,
1230
context,
1331
contentType: baseEntryProps.contentType || context.category.childContentType,
1432
tags: parseTags(baseEntryProps.tags).map(tagName => {
1533
return models.tag(tagName, context)
1634
}),
1735
date: new Date(baseEntryProps.date || baseEntryProps.stats.birthtime || Date.now()),
18-
permalink: (
19-
settings.permalinkPrefix +
20-
[
21-
context.collection.slug,
22-
context.category.isDefaultCategory ? '' : context.category.slug,
23-
baseEntryProps.slug
24-
].filter(Boolean).join('/') +
25-
(node.children ? '' : '.html')
26-
)
36+
attachments: baseEntryProps.attachments.map(a => a({
37+
...context,
38+
post: postContext
39+
}))
2740
}
2841
}
2942

src/compiler/contentModel2/models/homepage.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,21 @@ const models = {
55

66
function homepage(node) {
77
const baseEntryProps = models._baseEntry(node, ['index'])
8+
9+
const permalink = settings.permalinkPrefix
10+
11+
const pageContext = {
12+
title: baseEntryProps.title,
13+
slug: baseEntryProps.slug,
14+
permalink
15+
}
16+
817
return {
918
...baseEntryProps,
10-
permalink: settings.permalinkPrefix
19+
attachments: baseEntryProps.attachments.map(a => a({
20+
page: pageContext
21+
})),
22+
permalink
1123
}
1224
}
1325

src/compiler/contentModel2/models/subpage.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,25 @@ const models = {
55

66
function subpage(node) {
77
const baseEntryProps = models._baseEntry(node, ['index', 'page'])
8+
9+
const permalink = (
10+
settings.permalinkPrefix +
11+
baseEntryProps.slug +
12+
(node.children ? '' : '.html')
13+
)
14+
15+
const pageContext = {
16+
title: baseEntryProps.title,
17+
slug: baseEntryProps.slug,
18+
permalink
19+
}
20+
821
return {
922
...baseEntryProps,
10-
permalink: (
11-
settings.permalinkPrefix +
12-
baseEntryProps.slug +
13-
(node.children ? '' : '.html')
14-
)
23+
...pageContext,
24+
attachments: baseEntryProps.attachments.map(a => a({
25+
page: pageContext
26+
}))
1527
}
1628
}
1729

src/tests/compiler/contentModel2.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ const FSTree = os.platform() === 'win32' ?
88

99
test('compiler/contentModel2', t => {
1010
t.test('exits 0', async () => {
11-
console.log(
12-
contentModel2.create(FSTree).collections[0]
11+
console.dir(
12+
contentModel2.create(FSTree).collections[0],
13+
{ depth: null, colors: true }
1314
)
1415
})
1516
})

0 commit comments

Comments
 (0)