Skip to content

Commit 03e93e4

Browse files
committed
[CM2] Parse markdown content of models
Copy-paste the parse function from _baseEntry to category and collection. Maybe a base model class with this parse functionality?
1 parent 48eb114 commit 03e93e4

File tree

4 files changed

+71
-8
lines changed

4 files changed

+71
-8
lines changed

src/compiler/contentModel2/helpers.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const marked = require('marked')
2+
13
const templateExtensions = [
24
'.hbs',
35
'.handlebars',
@@ -25,9 +27,30 @@ const parseTags = (tags = []) => {
2527
tags
2628
}
2729

30+
const Markdown = {
31+
parse(text) {
32+
return Markdown.unescapeHandlebarsExpressions(
33+
marked.parse(
34+
text.replace(/^[\u200B\u200C\u200D\u200E\u200F\uFEFF]/, '')
35+
)
36+
)
37+
},
38+
39+
unescapeHandlebarsExpressions(html) {
40+
// partial greater sign
41+
html = html.replace(/{{>/g, '{{>')
42+
// helpers
43+
html = html.replace(/{{(.+)(?:"|')(.+)(?:"|')(.*)}}/g, '{{\$1"\$2"\$3}}')
44+
// partials
45+
html = html.replace(/{{>(.+)(?:"|')(.+)(?:"|').*}}/g, '{{>\$1"\$2"\$3}}')
46+
return html
47+
},
48+
}
49+
2850
module.exports = {
2951
templateExtensions,
3052
isTemplateFile,
3153
removeExtension,
32-
parseTags
54+
parseTags,
55+
Markdown
3356
}

src/compiler/contentModel2/models/_baseEntry.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
const _ = require('lodash')
22
const frontMatter = require('front-matter')
33
const slug = require('slug')
4-
const { templateExtensions, removeExtension } = require('../helpers')
4+
const { templateExtensions, removeExtension, Markdown } = require('../helpers')
55

66
const models = {
77
attachment: require('./attachment')
88
}
99

1010
const isIndexFile = (node, nameOptions) => {
11+
if (node.children) {
12+
return false
13+
}
1114
const names = nameOptions.join('|')
1215
const extensions = templateExtensions.join('|')
1316
const namePattern = new RegExp(`^(${names})(${extensions})$`, 'i')
14-
return !node.children && node.name.match(namePattern)
17+
return node.name.match(namePattern)
1518
}
1619

1720
function parseFolderedEntry(node, indexFileNameOptions) {
@@ -33,6 +36,13 @@ function parseFolderedEntry(node, indexFileNameOptions) {
3336
}
3437
}
3538

39+
function parseContent(node, content) {
40+
if (node.extension.match(/(html|htm|hbs|handlebars)/i)) {
41+
return content
42+
}
43+
return Markdown.parse(content)
44+
}
45+
3646
function _baseEntry(node, indexFileNameOptions) {
3747
const folderedEntry = node.children ?
3848
parseFolderedEntry(node, indexFileNameOptions) :
@@ -43,13 +53,16 @@ function _baseEntry(node, indexFileNameOptions) {
4353
folderedEntry?.name || entryFile.name
4454
)
4555
const attachments = folderedEntry?.attachments || []
56+
const contentRaw = body || ''
57+
const content = parseContent(entryFile, contentRaw)
4658

4759
return {
4860
..._.omit(node, 'children'),
4961
...attributes,
5062
title: attributes.title || entryName,
5163
slug: attributes.slug || slug(entryName),
52-
content: body || '',
64+
contentRaw,
65+
content,
5366
attachments
5467
}
5568
}

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@ const { join } = require('path')
22
const frontMatter = require('front-matter')
33
const makeSlug = require('slug')
44
const Settings = require('../../../../settings')
5-
const { isTemplateFile } = require('../../helpers')
5+
const { isTemplateFile, Markdown } = require('../../helpers')
66
const models = {
77
post: require('./post'),
88
attachment: require('../attachment')
99
}
1010

11+
function parseContent(node, content) {
12+
if (node.extension.match(/(html|htm|hbs|handlebars)/i)) {
13+
return content
14+
}
15+
return Markdown.parse(content)
16+
}
17+
1118
function category(node, context) {
1219
const settings = Settings.getSettings()
1320

@@ -26,6 +33,7 @@ function category(node, context) {
2633
context,
2734
childContentType: context.collection.childContentType,
2835
content: '',
36+
contentRaw: '',
2937
title: settings.defaultCategoryName,
3038
slug,
3139
permalink,
@@ -102,11 +110,17 @@ function category(node, context) {
102110

103111
tree.posts.sort((a, b) => b.date - a.date)
104112

113+
const contentRaw = indexProps.content || ''
114+
const content = indexFile ?
115+
parseContent(indexFile, contentRaw) :
116+
''
117+
105118
return {
106119
...categoryContext,
107120
...tree,
108121
context: context,
109-
content: indexProps.content || '',
122+
contentRaw,
123+
content,
110124
outputPath
111125
}
112126
}

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@ const _ = require('lodash')
33
const frontMatter = require('front-matter')
44
const makeSlug = require('slug')
55
const Settings = require('../../../../settings')
6-
const { isTemplateFile } = require('../../helpers')
6+
const { isTemplateFile, Markdown } = require('../../helpers')
77
const models = {
88
attachment: require('../attachment'),
99
category: require('./category'),
1010
post: require('./post')
1111
}
1212

13+
function parseContent(node, content) {
14+
if (node.extension.match(/(html|htm|hbs|handlebars)/i)) {
15+
return content
16+
}
17+
return Markdown.parse(content)
18+
}
19+
1320
function collection(node) {
1421
const settings = Settings.getSettings()
1522

@@ -108,10 +115,16 @@ function collection(node) {
108115
attachLinks(post, i, posts)
109116
})
110117

118+
const contentRaw = indexProps.content || ''
119+
const content = indexFile ?
120+
parseContent(indexFile, contentRaw) :
121+
''
122+
111123
return {
112124
...context,
113125
...tree,
114-
content: indexProps.content || '',
126+
contentRaw,
127+
content,
115128
outputPath
116129
}
117130
}

0 commit comments

Comments
 (0)