Skip to content

Commit dbb9278

Browse files
committed
feat(front-matter): add front matter[WIP]
1 parent 754f92c commit dbb9278

File tree

4 files changed

+556
-23
lines changed

4 files changed

+556
-23
lines changed

build/build.js

+20-23
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var build = function (opts) {
99
rollup
1010
.rollup({
1111
entry: 'src/' + opts.entry,
12-
plugins: [buble()].concat(opts.plugins || [])
12+
plugins: [buble(), commonjs(), nodeResolve()].concat(opts.plugins || [])
1313
})
1414
.then(function (bundle) {
1515
var dest = 'lib/' + (opts.output || opts.entry)
@@ -28,38 +28,35 @@ var build = function (opts) {
2828

2929
build({
3030
entry: 'core/index.js',
31-
output: 'docsify.js',
32-
plugins: [commonjs(), nodeResolve()]
31+
output: 'docsify.js'
3332
})
3433

35-
build({
36-
entry: 'plugins/search/index.js',
37-
output: 'plugins/search.js',
38-
moduleName: 'D.Search'
39-
})
34+
var plugins = [
35+
{ name: 'search', entry: 'search/index.js', moduleName: 'Search' },
36+
{ name: 'ga', entry: 'ga.js', moduleName: 'GA' }
37+
// { name: 'front-matter', entry: 'front-matter/index.js', moduleName: 'FrontMatter' }
38+
]
4039

41-
build({
42-
entry: 'plugins/ga.js',
43-
output: 'plugins/ga.js',
44-
moduleName: 'D.GA'
40+
plugins.forEach(item => {
41+
build({
42+
entry: 'plugins/' + item.entry,
43+
output: 'plugins/' + item.name + '.js',
44+
moduleName: 'D.' + item.moduleName
45+
})
4546
})
4647

4748
if (isProd) {
4849
build({
4950
entry: 'core/index.js',
5051
output: 'docsify.min.js',
51-
plugins: [commonjs(), nodeResolve(), uglify()]
52-
})
53-
build({
54-
entry: 'plugins/search/index.js',
55-
output: 'plugins/search.min.js',
56-
moduleName: 'D.Search',
5752
plugins: [uglify()]
5853
})
59-
build({
60-
entry: 'plugins/ga.js',
61-
output: 'plugins/ga.min.js',
62-
moduleName: 'D.GA',
63-
plugins: [uglify()]
54+
plugins.forEach(item => {
55+
build({
56+
entry: 'plugins/' + item.entry,
57+
output: 'plugins/' + item.name + '.min.js',
58+
moduleName: 'D.' + item.moduleName,
59+
plugins: [uglify()]
60+
})
6461
})
6562
}

src/plugins/front-matter/index.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import parser from './parser'
2+
3+
const install = function (hook, vm) {
4+
hook.beforeEach(content => {
5+
const { attributes, body } = parser(content)
6+
7+
Docsify.util.merge(vm.config, attributes.config)
8+
9+
return body
10+
})
11+
}
12+
13+
window.$docsify.plugins = [].concat(install, window.$docsify.plugins)

src/plugins/front-matter/parser.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* Fork https://github.com/egoist/docute/blob/master/src/utils/front-matter.js
3+
*/
4+
/* eslint-disable */
5+
import parser from './yaml'
6+
7+
var optionalByteOrderMark = '\\ufeff?'
8+
var pattern = '^(' +
9+
optionalByteOrderMark +
10+
'(= yaml =|---)' +
11+
'$([\\s\\S]*?)' +
12+
'(?:\\2|\\.\\.\\.)' +
13+
'$' +
14+
'' +
15+
'(?:\\n)?)'
16+
// NOTE: If this pattern uses the 'g' flag the `regex` variable definition will
17+
// need to be moved down into the functions that use it.
18+
var regex = new RegExp(pattern, 'm')
19+
20+
function extractor (string) {
21+
string = string || ''
22+
23+
var lines = string.split(/(\r?\n)/)
24+
if (lines[0] && /= yaml =|---/.test(lines[0])) {
25+
return parse(string)
26+
} else {
27+
return { attributes: {}, body: string }
28+
}
29+
}
30+
31+
function parse (string) {
32+
var match = regex.exec(string)
33+
34+
if (!match) {
35+
return {
36+
attributes: {},
37+
body: string
38+
}
39+
}
40+
41+
var yaml = match[match.length - 1].replace(/^\s+|\s+$/g, '')
42+
var attributes = parser(yaml) || {}
43+
var body = string.replace(match[0], '')
44+
45+
return { attributes: attributes, body: body, frontmatter: yaml }
46+
}
47+
48+
function test (string) {
49+
string = string || ''
50+
51+
return regex.test(string)
52+
}
53+
54+
export default extractor

0 commit comments

Comments
 (0)