Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support for TOML front matter (#141) #164

Merged
merged 2 commits into from Apr 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions lib/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ const fs = require('fs-extra')
const globby = require('globby')
const yamlParser = require('js-yaml')
const tomlParser = require('toml')
const yaml = require('yaml-front-matter')
const createMarkdown = require('./markdown')
const tempPath = path.resolve(__dirname, 'app/.temp')
const { inferTitle, extractHeaders } = require('./util')
const { inferTitle, extractHeaders, parseFrontmatter } = require('./util')

fs.ensureDirSync(tempPath)

Expand Down Expand Up @@ -174,23 +173,23 @@ async function resolveOptions (sourceDir) {

// extract yaml frontmatter
const content = await fs.readFile(path.resolve(sourceDir, file), 'utf-8')
const frontmatter = yaml.loadFront(content)
const frontmatter = parseFrontmatter(content)
// infer title
const title = inferTitle(frontmatter)
if (title) {
data.title = title
}
const headers = extractHeaders(
frontmatter.__content,
frontmatter.content,
['h2', 'h3'],
options.markdown
)
if (headers.length) {
data.headers = headers
}
delete frontmatter.__content
if (Object.keys(frontmatter).length) {
data.frontmatter = frontmatter
delete frontmatter.content
if (Object.keys(frontmatter.data).length) {
data.frontmatter = frontmatter.data
}
return data
}))
Expand Down
19 changes: 13 additions & 6 deletions lib/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,28 @@ exports.applyUserWebpackConfig = function (userConfig, config, isServer) {
}

exports.inferTitle = function (frontmatter) {
if (frontmatter.home) {
if (frontmatter.data.home) {
return 'Home'
}
if (frontmatter.title) {
return frontmatter.title
if (frontmatter.data.title) {
return frontmatter.data.title
}
const match = frontmatter.__content.trim().match(/^#+\s+(.*)/)
const match = frontmatter.content.trim().match(/^#+\s+(.*)/)
if (match) {
return match[1]
}
}

exports.parseFrontmatter = content => {
const yaml = require('yaml-front-matter')
return yaml.loadFront(content)
const matter = require('gray-matter')
const toml = require('toml')

return matter(content, {
engines: {
toml: toml.parse.bind(toml),
excerpt: false
}
})
}

const LRU = require('lru-cache')
Expand Down
9 changes: 4 additions & 5 deletions lib/webpack/markdownLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const path = require('path')
const hash = require('hash-sum')
const { EventEmitter } = require('events')
const { getOptions } = require('loader-utils')
const yaml = require('yaml-front-matter')
const { inferTitle, extractHeaders } = require('../util')
const { inferTitle, extractHeaders, parseFrontmatter } = require('../util')
const LRU = require('lru-cache')

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

const frontmatter = yaml.loadFront(src)
const content = frontmatter.__content
const frontmatter = parseFrontmatter(src)
const content = frontmatter.content

if (!isProd && !isServer) {
const inferredTitle = inferTitle(frontmatter)
const headers = extractHeaders(content, ['h2', 'h3'], markdown)
delete frontmatter.__content
delete frontmatter.content

// diff frontmatter and title, since they are not going to be part of the
// returned component, changes in frontmatter do not trigger proper updates
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"file-loader": "^1.1.11",
"fs-extra": "^5.0.0",
"globby": "^8.0.1",
"gray-matter": "^4.0.1",
"js-yaml": "^3.11.0",
"koa-connect": "^2.0.1",
"koa-mount": "^3.0.0",
Expand Down Expand Up @@ -91,8 +92,7 @@
"webpack-merge": "^4.1.2",
"webpack-serve": "^0.3.1",
"webpackbar": "^2.6.1",
"workbox-build": "^3.1.0",
"yaml-front-matter": "^4.0.0"
"workbox-build": "^3.1.0"
},
"devDependencies": {
"conventional-changelog": "^1.1.23",
Expand Down