1 | var parser = require('js-yaml')
|
2 | var optionalByteOrderMark = '\\ufeff?'
|
3 | var pattern = '^('
|
4 | + optionalByteOrderMark
|
5 | + '(= yaml =|---)'
|
6 | + '$([\\s\\S]*?)'
|
7 | + '\\2'
|
8 | + '$'
|
9 | + (process.platform === 'win32' ? '\\r?' : '')
|
10 | + '(?:\\n)?)'
|
11 |
|
12 |
|
13 | var regex = new RegExp(pattern, 'm')
|
14 |
|
15 | module.exports = extractor
|
16 | module.exports.test = test
|
17 |
|
18 | function extractor(string) {
|
19 | string = string || ''
|
20 |
|
21 | var lines = string.split(/(\r?\n)/)
|
22 | if (lines[0] && /= yaml =|---/.test(lines[0])) {
|
23 | return parse(string)
|
24 | } else {
|
25 | return { attributes: {}, body: string }
|
26 | }
|
27 | }
|
28 |
|
29 | function parse(string) {
|
30 | var match = regex.exec(string)
|
31 |
|
32 | if (! match) {
|
33 | return {
|
34 | attributes: {},
|
35 | body: string
|
36 | }
|
37 | }
|
38 |
|
39 | var yaml = match[match.length - 1].replace(/^\s+|\s+$/g, '')
|
40 | var attributes = parser.load(yaml) || {}
|
41 | var body = string.replace(match[0], '')
|
42 |
|
43 | return { attributes: attributes, body: body }
|
44 | }
|
45 |
|
46 | function test(string){
|
47 | string = string || ''
|
48 |
|
49 | return regex.test(string)
|
50 | }
|