forked from laysent/remark-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
61 lines (53 loc) · 2.32 KB
/
index.test.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
const unified = require('unified');
const remarkParse = require('remark-parse');
const stringify = require('rehype-stringify');
const remark2rehype = require('remark-rehype');
const remarkStringify = require('remark-stringify');
const plugin = require('.');
const render = (text, config) => unified()
.use(remarkParse)
.use(plugin, config)
.use(remark2rehype)
.use(stringify)
.processSync(text);
const renderToMarkdown = (text, config) => unified()
.use(remarkParse)
.use(remarkStringify)
.use(plugin, config)
.processSync(text);
const configToTest = {
'no-config': undefined,
'empty object': {},
customParenthesis: { parenthesis: '{}' },
singleParenthesis: { parenthesis: ':' },
};
const markdown = 'According to [Wiki](https://en.wikipedia.org/wiki/Furigana), {Furigana}^(振り仮名) is a Japanese reading aid.\n';
for (const [configName, config] of Object.entries(configToTest)) { // eslint-disable-line no-restricted-syntax, max-len
it(`should render ruby tag (${configName})`, () => {
const { contents } = render(markdown, config);
expect(contents).toMatchSnapshot();
});
it(`should compile to markdown (${configName})`, () => {
const { contents: contentsA } = renderToMarkdown(markdown, config);
const { contents: contentsB } = renderToMarkdown(contentsA, config);
expect(markdown).toBe(contentsA);
expect(contentsA).toBe(contentsB);
});
it(`should not render ruby tag when syntax not found (${configName})`, () => {
const noRubyMarkdown = 'According to [Wiki](https://en.wikipedia.org/wiki/Furigana), {Furigana}(振り仮名) is a Japanese reading aid.\n';
const { contents } = render(noRubyMarkdown, config);
expect(contents.indexOf('<ruby>') < 0).toBe(true);
});
it(`should handle delimitation in ruby (${configName})`, () => {
const nested = '{first, [second part] and [third]}^(eins, [zwei] und [drei])';
const { contents } = render(nested, config);
expect(contents).toMatchSnapshot();
});
it(`should compile delimitation text to markdown (${configName})`, () => {
const nested = '{[first][second part][third]}^([eins][zwei][drei])\n';
const { contents: contentsA } = renderToMarkdown(nested, config);
const { contents: contentsB } = renderToMarkdown(contentsA, config);
expect(nested).toBe(contentsA);
expect(contentsA).toBe(contentsB);
});
}