UNPKG

1.15 kBJavaScriptView Raw
1var parser = require('js-yaml')
2var optionalByteOrderMark = '\\ufeff?'
3var pattern = '^('
4 + optionalByteOrderMark
5 + '(= yaml =|---)'
6 + '$([\\s\\S]*?)'
7 + '\\2'
8 + '$'
9 + (process.platform === 'win32' ? '\\r?' : '')
10 + '(?:\\n)?)'
11// NOTE: If this pattern uses the 'g' flag the `regex` variable definition will
12// need to be moved down into the functions that use it.
13var regex = new RegExp(pattern, 'm')
14
15module.exports = extractor
16module.exports.test = test
17
18function 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
29function 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
46function test(string){
47 string = string || ''
48
49 return regex.test(string)
50}