Skip to content

Commit 2d58354

Browse files
committed
feat: support for TOML front matter (#141)
1 parent 241f91e commit 2d58354

File tree

4 files changed

+26
-20
lines changed

4 files changed

+26
-20
lines changed

Diff for: lib/prepare.js

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

109
fs.ensureDirSync(tempPath)
1110

@@ -160,23 +159,23 @@ async function resolveOptions (sourceDir) {
160159

161160
// extract yaml frontmatter
162161
const content = await fs.readFile(path.resolve(sourceDir, file), 'utf-8')
163-
const frontmatter = yaml.loadFront(content)
162+
const frontmatter = parseFrontmatter(content)
164163
// infer title
165164
const title = inferTitle(frontmatter)
166165
if (title) {
167166
data.title = title
168167
}
169168
const headers = extractHeaders(
170-
frontmatter.__content,
169+
frontmatter.content,
171170
['h2', 'h3'],
172171
options.markdown
173172
)
174173
if (headers.length) {
175174
data.headers = headers
176175
}
177-
delete frontmatter.__content
178-
if (Object.keys(frontmatter).length) {
179-
data.frontmatter = frontmatter
176+
delete frontmatter.content
177+
if (Object.keys(frontmatter.data).length) {
178+
data.frontmatter = frontmatter.data
180179
}
181180
return data
182181
}))

Diff for: 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')

Diff for: 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

Diff for: package.json

+3-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",
@@ -78,6 +79,7 @@
7879
"semver": "^5.5.0",
7980
"stylus": "^0.54.5",
8081
"stylus-loader": "^3.0.2",
82+
"toml": "^2.3.3",
8183
"url-loader": "^1.0.1",
8284
"vue": "^2.5.16",
8385
"vue-loader": "^15.0.0-rc.1",
@@ -90,8 +92,7 @@
9092
"webpack-merge": "^4.1.2",
9193
"webpack-serve": "^0.3.1",
9294
"webpackbar": "^2.6.1",
93-
"workbox-build": "^3.1.0",
94-
"yaml-front-matter": "^4.0.0"
95+
"workbox-build": "^3.1.0"
9596
},
9697
"devDependencies": {
9798
"conventional-changelog": "^1.1.23",

0 commit comments

Comments
 (0)