Skip to content

Commit 9577a2a

Browse files
committed
[CM2] Most basically introduce collection index
It's just a copy of how homepage works for the root. Cascade of content types (collection -> category -> entry) is not implemented.
1 parent 95d5e68 commit 9577a2a

File tree

5 files changed

+142
-1
lines changed

5 files changed

+142
-1
lines changed

src/compiler/contentModel2/collection/contentTypes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
module.exports = {
2+
COLLECTION_HOME: 'collectionHome',
3+
FOLDERED_COLLECTION_HOME_INDEX: 'folderedCollectionHomeIndex',
24
COLLECTION: 'collection',
35
CATEGORY: 'category',
46
CATEGORY_INDEX: 'categoryIndex',

src/compiler/contentModel2/collection/fsToContent.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ const { dirname } = require('path')
22
const _ = require('lodash')
33
const Dictionary = require('../../../dictionary')
44
const { last } = require('../../../helpers')
5-
const contentTypes = require('./contentTypes')
65
const { createLocalAsset } = require('../models/localAsset')
76

7+
const {
8+
createCollectionHome,
9+
createFolderedCollectionHome,
10+
createFolderedCollectionHomeIndex
11+
} = require('./models/collectionHome')
12+
813
const {
914
createCategory,
1015
createCategoryIndex
@@ -31,6 +36,14 @@ const isTemplateFile = (fsObject) => {
3136
return new RegExp(templateExtensions.join('|'), 'i').test(fsObject.extension)
3237
}
3338

39+
const isCollectionHomeFile = (fsObject) => {
40+
return isTemplateFile(fsObject) && fsObject.name.match(/^(index|home|collection)\..+$/)
41+
}
42+
43+
const isCollectionHomeDirectory = (fsObject) => {
44+
return fsObject.name.match(`^(index|home|collection)$`)
45+
}
46+
3447
const isFolderedPostIndexFile = (fsObject) => {
3548
return isTemplateFile(fsObject) && fsObject.name.match(/^post\..+$/)
3649
}
@@ -85,6 +98,39 @@ const upsertEntry = ({
8598
}
8699
}
87100

101+
const withCollectionHome = (contentModel, fsObject) => {
102+
return newEntry({
103+
contentModel,
104+
key: 'index',
105+
entryFn: () => createCollectionHome(fsObject).data,
106+
replace: true
107+
})
108+
}
109+
110+
const withFolderedCollectionHome = (contentModel, fsObject) => {
111+
return newEntry({
112+
contentModel,
113+
key: 'index',
114+
entryFn: () => {
115+
return createFolderedCollectionHome({
116+
...fsObject,
117+
children: fsObject.children.map(mapFolderedCollectionHomeTree)
118+
}).data
119+
},
120+
replace: true
121+
})
122+
}
123+
124+
const mapFolderedCollectionHomeTree = (fsObject) => {
125+
if (isCollectionHomeFile(fsObject)) {
126+
return createFolderedCollectionHomeIndex(fsObject)
127+
}
128+
return createLocalAsset({
129+
...fsObject,
130+
isFolder: !!fsObject.children
131+
})
132+
}
133+
88134
const withLocalAsset = (contentModel, fsObject) => {
89135
return newEntry({
90136
contentModel,
@@ -190,6 +236,9 @@ const mapCategoryTree = (fsObject) => {
190236

191237
const mapCollectionTree = (fsTree) => {
192238
return fsTree.reduce((contentModel, fsObject) => {
239+
if (isCollectionHomeFile(fsObject)) {
240+
return withCollectionHome(contentModel, fsObject)
241+
}
193242
if (isTemplateFile(fsObject)) {
194243
return withDefaultCategory(
195244
withDefaultCategoryPost(contentModel, fsObject),
@@ -199,6 +248,9 @@ const mapCollectionTree = (fsTree) => {
199248
if (!fsObject.children) {
200249
return withLocalAsset(contentModel, fsObject)
201250
}
251+
if (isCollectionHomeDirectory(fsObject)) {
252+
return withFolderedCollectionHome(contentModel, fsObject)
253+
}
202254
if (fsObject.children.some(isFolderedPostIndexFile)) {
203255
return withDefaultCategory(
204256
withFolderedPost(contentModel, fsObject),
@@ -207,6 +259,7 @@ const mapCollectionTree = (fsTree) => {
207259
}
208260
return withCategory(contentModel, fsObject)
209261
}, {
262+
index: createCollectionHome({}).data,
210263
categories: [],
211264
posts: [],
212265
localAssets: [],
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const _ = require('lodash')
2+
const Settings = require('../../../../settings')
3+
const { maybeRawHTMLType } = require('../../../../helpers')
4+
const contentTypes = require('../../contentTypes')
5+
const parseTemplate = require('../../parseTemplate')
6+
const { isLocalAsset } = require('../../models/localAsset')
7+
8+
const DEFAULT_TYPE = 'basic'
9+
10+
const isFolderedCollectionHomeIndex = (fsObject) => {
11+
return fsObject.type === contentTypes.FOLDERED_COLLECTION_HOME_INDEX
12+
}
13+
14+
const _createCollectionHome = (fsObject, { foldered }) => {
15+
const indexFile = foldered ?
16+
fsObject.children.find(isFolderedCollectionHomeIndex) :
17+
fsObject
18+
19+
const localAssets = foldered ?
20+
fsObject.children.filter(isLocalAsset) :
21+
[]
22+
23+
const permalink = Settings.getSettings().permalinkPrefix
24+
25+
const metadata = parseTemplate(indexFile, {
26+
permalink,
27+
localAssets
28+
})
29+
30+
return {
31+
..._.omit(fsObject, 'children'),
32+
type: contentTypes.COLLECTION_HOME,
33+
data: {
34+
type: metadata.type || maybeRawHTMLType(indexFile?.extension) || DEFAULT_TYPE,
35+
title: metadata.title || '',
36+
content: metadata.content || '',
37+
mentions: metadata.mentions || [],
38+
...metadata.attributes,
39+
foldered,
40+
localAssets,
41+
permalink
42+
}
43+
}
44+
}
45+
46+
const createFolderedCollectionHomeIndex = (fsObject) => {
47+
return {
48+
...fsObject,
49+
type: contentTypes.FOLDERED_COLLECTION_HOME_INDEX
50+
}
51+
}
52+
53+
const createCollectionHome = (fsObject) => {
54+
return _createCollectionHome(fsObject, { foldered: false })
55+
}
56+
57+
const createFolderedCollectionHome = (fsObject) => {
58+
return _createCollectionHome(fsObject, { foldered: true })
59+
}
60+
61+
module.exports = {
62+
createCollectionHome,
63+
createFolderedCollectionHome,
64+
isFolderedCollectionHomeIndex,
65+
createFolderedCollectionHomeIndex
66+
}

src/tests/fixtures/contentModel2FSTree.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77
},
88
"depth": 0,
99
"children": [
10+
{
11+
"name": "collection.md",
12+
"path": "Blog/collection.md",
13+
"stats": {
14+
"birthtime": "2025-03-01T03:38:49.442Z"
15+
},
16+
"depth": 1,
17+
"extension": ".md",
18+
"content": "---\ntitle: Blog\n---\nWelcome to my blog"
19+
},
1020
{
1121
"name": "Türkçe",
1222
"path": "Blog/Türkçe",

src/tests/fixtures/contentModel2FSTreeWin32.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77
},
88
"depth": 0,
99
"children": [
10+
{
11+
"name": "collection.md",
12+
"path": "Blog\\collection.md",
13+
"stats": {
14+
"birthtime": "2025-03-01T03:38:49.442Z"
15+
},
16+
"depth": 1,
17+
"extension": ".md",
18+
"content": "---\ntitle: Blog\n---\nWelcome to my blog"
19+
},
1020
{
1121
"name": "Türkçe",
1222
"path": "Blog\\Türkçe",

0 commit comments

Comments
 (0)