-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.ts
48 lines (40 loc) · 1.26 KB
/
test.ts
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
import { readFile } from 'fs/promises'
import { globSync } from 'glob'
import { toMatchFile } from 'jest-file-snapshot'
import type { Nodes } from 'mdast'
import rehypeStringify from 'rehype-stringify'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import { Processor, unified } from 'unified'
import { removePosition } from 'unist-util-remove-position'
import { rehypeAstro, remarkAstro } from './src'
function remarkRemovePosition() {
return (tree: Nodes) => {
removePosition(tree)
return tree
}
}
function remarkJson(this: Processor) {
this.Compiler = (root) => JSON.stringify(root, undefined, 2)
}
const processors = {
json: unified()
.use(remarkParse)
.use(remarkRemovePosition)
.use(remarkAstro)
.use(remarkJson),
html: unified()
.use(remarkParse)
.use(remarkRehype)
.use(rehypeAstro)
.use(rehypeStringify),
}
expect.extend({ toMatchFile })
describe.each(globSync('src/replacements/*/test.md'))('convert %s', (path) => {
const stem = path.replace(/\.md$/, '')
let input: Buffer
beforeAll(async () => (input = await readFile(path)))
test.each(Object.entries(processors))(`to ${stem}.%s`, (ext, proc) =>
expect(proc.processSync(input).value).toMatchFile(`${stem}.${ext}`)
)
})