-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
111 lines (93 loc) · 2.68 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import fs from 'fs'
import path from 'path'
import test from 'tape'
import vfile from 'to-vfile'
import unified from 'unified'
import remark from 'remark'
import not from 'not'
import hidden from 'is-hidden'
import frontmatter from '../index.js'
var join = path.join
var read = fs.readFileSync
var write = fs.writeFileSync
var dir = fs.readdirSync
test('frontmatter()', function (t) {
t.equal(typeof frontmatter, 'function', 'should be a function')
t.doesNotThrow(function () {
remark().use(frontmatter).freeze()
}, 'should not throw if not passed options')
t.doesNotThrow(function () {
unified().use(frontmatter).freeze()
}, 'should not throw if without parser or compiler')
t.throws(
function () {
unified().use(frontmatter, [1]).freeze()
},
/^Error: Expected matter to be an object, not `1`/,
'should throw if not given a preset or a matter'
)
t.throws(
function () {
unified().use(frontmatter, ['jsonml']).freeze()
},
/^Error: Missing matter definition for `jsonml`/,
'should throw if given an unknown preset'
)
t.throws(
function () {
unified()
.use(frontmatter, [{marker: '*'}])
.freeze()
},
/^Error: Missing `type` in matter `{"marker":"\*"}`/,
'should throw if given a matter without `type`'
)
t.throws(
function () {
unified()
.use(frontmatter, [{type: 'jsonml'}])
.freeze()
},
/^Error: Missing `marker` or `fence` in matter `{"type":"jsonml"}`/,
'should throw if given a matter without `marker`'
)
t.end()
})
test('fixtures', function (t) {
var base = join('test', 'fixtures')
var entries = dir(base).filter(not(hidden))
t.plan(entries.length)
entries.forEach(each)
function each(fixture) {
t.test(fixture, function (st) {
var input = vfile.readSync(join(base, fixture, 'input.md'))
var treePath = join(base, fixture, 'tree.json')
var outputPath = join(base, fixture, 'output.md')
var output
var actual
var expected
var config
var proc
try {
config = JSON.parse(read(join(base, fixture, 'config.json')))
} catch (_) {}
proc = remark().use(frontmatter, config)
actual = JSON.parse(JSON.stringify(proc.parse(input)))
try {
output = read(outputPath, 'utf8')
} catch (_) {
output = String(input)
}
try {
expected = JSON.parse(read(treePath))
} catch (_) {
// New fixture.
write(treePath, JSON.stringify(actual, 0, 2) + '\n')
expected = actual
}
st.deepEqual(actual, expected, 'tree')
st.equal(String(proc.processSync(input)), output, 'process')
st.end()
})
}
})