Skip to content

Commit 70620ba

Browse files
ryanrivestyyx990803
authored andcommitted
feat: support for TOML front matter (#141) (#164)
1 parent f91d0a2 commit 70620ba

File tree

4 files changed

+25
-20
lines changed

4 files changed

+25
-20
lines changed

lib/prepare.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ const fs = require('fs-extra')
33
const globby = require('globby')
44
const yamlParser = require('js-yaml')
55
const tomlParser = require('toml')
6-
const yaml = require('yaml-front-matter')
76
const createMarkdown = require('./markdown')
87
const tempPath = path.resolve(__dirname, 'app/.temp')
9-
const { inferTitle, extractHeaders } = require('./util')
8+
const { inferTitle, extractHeaders, parseFrontmatter } = require('./util')
109

1110
fs.ensureDirSync(tempPath)
1211

@@ -174,23 +173,23 @@ async function resolveOptions (sourceDir) {
174173

175174
// extract yaml frontmatter
176175
const content = await fs.readFile(path.resolve(sourceDir, file), 'utf-8')
177-
const frontmatter = yaml.loadFront(content)
176+
const frontmatter = parseFrontmatter(content)
178177
// infer title
179178
const title = inferTitle(frontmatter)
180179
if (title) {
181180
data.title = title
182181
}
183182
const headers = extractHeaders(
184-
frontmatter.__content,
183+
frontmatter.content,
185184
['h2', 'h3'],
186185
options.markdown
187186
)
188187
if (headers.length) {
189188
data.headers = headers
190189
}
191-
delete frontmatter.__content
192-
if (Object.keys(frontmatter).length) {
193-
data.frontmatter = frontmatter
190+
delete frontmatter.content
191+
if (Object.keys(frontmatter.data).length) {
192+
data.frontmatter = frontmatter.data
194193
}
195194
return data
196195
}))

lib/util/index.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,28 @@ exports.applyUserWebpackConfig = function (userConfig, config, isServer) {
2626
}
2727

2828
exports.inferTitle = function (frontmatter) {
29-
if (frontmatter.home) {
29+
if (frontmatter.data.home) {
3030
return 'Home'
3131
}
32-
if (frontmatter.title) {
33-
return frontmatter.title
32+
if (frontmatter.data.title) {
33+
return frontmatter.data.title
3434
}
35-
const match = frontmatter.__content.trim().match(/^#+\s+(.*)/)
35+
const match = frontmatter.content.trim().match(/^#+\s+(.*)/)
3636
if (match) {
3737
return match[1]
3838
}
3939
}
4040

4141
exports.parseFrontmatter = content => {
42-
const yaml = require('yaml-front-matter')
43-
return yaml.loadFront(content)
42+
const matter = require('gray-matter')
43+
const toml = require('toml')
44+
45+
return matter(content, {
46+
engines: {
47+
toml: toml.parse.bind(toml),
48+
excerpt: false
49+
}
50+
})
4451
}
4552

4653
const LRU = require('lru-cache')

lib/webpack/markdownLoader.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ const path = require('path')
33
const hash = require('hash-sum')
44
const { EventEmitter } = require('events')
55
const { getOptions } = require('loader-utils')
6-
const yaml = require('yaml-front-matter')
7-
const { inferTitle, extractHeaders } = require('../util')
6+
const { inferTitle, extractHeaders, parseFrontmatter } = require('../util')
87
const LRU = require('lru-cache')
98

109
const cache = LRU({ max: 1000 })
@@ -25,13 +24,13 @@ module.exports = function (src) {
2524
return cached
2625
}
2726

28-
const frontmatter = yaml.loadFront(src)
29-
const content = frontmatter.__content
27+
const frontmatter = parseFrontmatter(src)
28+
const content = frontmatter.content
3029

3130
if (!isProd && !isServer) {
3231
const inferredTitle = inferTitle(frontmatter)
3332
const headers = extractHeaders(content, ['h2', 'h3'], markdown)
34-
delete frontmatter.__content
33+
delete frontmatter.content
3534

3635
// diff frontmatter and title, since they are not going to be part of the
3736
// returned component, changes in frontmatter do not trigger proper updates

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"file-loader": "^1.1.11",
5757
"fs-extra": "^5.0.0",
5858
"globby": "^8.0.1",
59+
"gray-matter": "^4.0.1",
5960
"js-yaml": "^3.11.0",
6061
"koa-connect": "^2.0.1",
6162
"koa-mount": "^3.0.0",
@@ -91,8 +92,7 @@
9192
"webpack-merge": "^4.1.2",
9293
"webpack-serve": "^0.3.1",
9394
"webpackbar": "^2.6.1",
94-
"workbox-build": "^3.1.0",
95-
"yaml-front-matter": "^4.0.0"
95+
"workbox-build": "^3.1.0"
9696
},
9797
"devDependencies": {
9898
"conventional-changelog": "^1.1.23",

0 commit comments

Comments
 (0)