Skip to content

Commit

Permalink
feat(front-matter): add front matter[WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
QingWei-Li committed Feb 19, 2017
1 parent 754f92c commit dbb9278
Show file tree
Hide file tree
Showing 4 changed files with 556 additions and 23 deletions.
43 changes: 20 additions & 23 deletions build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var build = function (opts) {
rollup
.rollup({
entry: 'src/' + opts.entry,
plugins: [buble()].concat(opts.plugins || [])
plugins: [buble(), commonjs(), nodeResolve()].concat(opts.plugins || [])
})
.then(function (bundle) {
var dest = 'lib/' + (opts.output || opts.entry)
Expand All @@ -28,38 +28,35 @@ var build = function (opts) {

build({
entry: 'core/index.js',
output: 'docsify.js',
plugins: [commonjs(), nodeResolve()]
output: 'docsify.js'
})

build({
entry: 'plugins/search/index.js',
output: 'plugins/search.js',
moduleName: 'D.Search'
})
var plugins = [
{ name: 'search', entry: 'search/index.js', moduleName: 'Search' },
{ name: 'ga', entry: 'ga.js', moduleName: 'GA' }
// { name: 'front-matter', entry: 'front-matter/index.js', moduleName: 'FrontMatter' }
]

build({
entry: 'plugins/ga.js',
output: 'plugins/ga.js',
moduleName: 'D.GA'
plugins.forEach(item => {
build({
entry: 'plugins/' + item.entry,
output: 'plugins/' + item.name + '.js',
moduleName: 'D.' + item.moduleName
})
})

if (isProd) {
build({
entry: 'core/index.js',
output: 'docsify.min.js',
plugins: [commonjs(), nodeResolve(), uglify()]
})
build({
entry: 'plugins/search/index.js',
output: 'plugins/search.min.js',
moduleName: 'D.Search',
plugins: [uglify()]
})
build({
entry: 'plugins/ga.js',
output: 'plugins/ga.min.js',
moduleName: 'D.GA',
plugins: [uglify()]
plugins.forEach(item => {
build({
entry: 'plugins/' + item.entry,
output: 'plugins/' + item.name + '.min.js',
moduleName: 'D.' + item.moduleName,
plugins: [uglify()]
})
})
}
13 changes: 13 additions & 0 deletions src/plugins/front-matter/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import parser from './parser'

const install = function (hook, vm) {
hook.beforeEach(content => {
const { attributes, body } = parser(content)

Docsify.util.merge(vm.config, attributes.config)

return body
})
}

window.$docsify.plugins = [].concat(install, window.$docsify.plugins)
54 changes: 54 additions & 0 deletions src/plugins/front-matter/parser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Fork https://github.com/egoist/docute/blob/master/src/utils/front-matter.js
*/
/* eslint-disable */
import parser from './yaml'

var optionalByteOrderMark = '\\ufeff?'
var pattern = '^(' +
optionalByteOrderMark +
'(= yaml =|---)' +
'$([\\s\\S]*?)' +
'(?:\\2|\\.\\.\\.)' +
'$' +
'' +
'(?:\\n)?)'
// NOTE: If this pattern uses the 'g' flag the `regex` variable definition will
// need to be moved down into the functions that use it.
var regex = new RegExp(pattern, 'm')

function extractor (string) {
string = string || ''

var lines = string.split(/(\r?\n)/)
if (lines[0] && /= yaml =|---/.test(lines[0])) {
return parse(string)
} else {
return { attributes: {}, body: string }
}
}

function parse (string) {
var match = regex.exec(string)

if (!match) {
return {
attributes: {},
body: string
}
}

var yaml = match[match.length - 1].replace(/^\s+|\s+$/g, '')
var attributes = parser(yaml) || {}
var body = string.replace(match[0], '')

return { attributes: attributes, body: body, frontmatter: yaml }
}

function test (string) {
string = string || ''

return regex.test(string)
}

export default extractor
Loading

0 comments on commit dbb9278

Please sign in to comment.