|
1 | 1 | import * as MarkdownIt from 'markdown-it'
|
2 |
| -import { tocPlugin } from '@vuepress/markdown' |
| 2 | +import { anchorPlugin, tocPlugin, slugify } from '@vuepress/markdown' |
3 | 3 |
|
4 | 4 | const fixtures = {
|
5 | 5 | simpleTree: `\
|
@@ -75,4 +75,72 @@ describe('@vuepress/markdown > plugins > tocPlugin', () => {
|
75 | 75 | })
|
76 | 76 | })
|
77 | 77 | })
|
| 78 | + |
| 79 | + describe('should include html elements and should escape texts', () => { |
| 80 | + const md = MarkdownIt({ |
| 81 | + html: true, |
| 82 | + }) |
| 83 | + .use(anchorPlugin, { slugify }) |
| 84 | + .use(tocPlugin, { slugify }) |
| 85 | + |
| 86 | + const testCases: [string, { slug: string; title: string; h2: string }][] = [ |
| 87 | + // html element should be kept as is |
| 88 | + [ |
| 89 | + `\ |
| 90 | +[[toc]] |
| 91 | +## foo <bar /> |
| 92 | +`, |
| 93 | + { |
| 94 | + slug: 'foo', |
| 95 | + title: 'foo <bar />', |
| 96 | + h2: 'foo <bar />', |
| 97 | + }, |
| 98 | + ], |
| 99 | + // inline code should be escaped |
| 100 | + [ |
| 101 | + `\ |
| 102 | +[[toc]] |
| 103 | +## foo <bar /> \`<code />\` |
| 104 | +`, |
| 105 | + { |
| 106 | + slug: 'foo-code', |
| 107 | + title: 'foo <bar /> <code />', |
| 108 | + h2: 'foo <bar /> <code><code /></code>', |
| 109 | + }, |
| 110 | + ], |
| 111 | + // text should be escaped |
| 112 | + [ |
| 113 | + `\ |
| 114 | +[[toc]] |
| 115 | +## foo <bar/> "baz" |
| 116 | +`, |
| 117 | + { |
| 118 | + slug: 'foo-baz', |
| 119 | + title: 'foo <bar/> "baz"', |
| 120 | + h2: 'foo <bar/> "baz"', |
| 121 | + }, |
| 122 | + ], |
| 123 | + // text should be escaped |
| 124 | + [ |
| 125 | + `\ |
| 126 | +[[toc]] |
| 127 | +## < test > |
| 128 | +`, |
| 129 | + { |
| 130 | + slug: 'test', |
| 131 | + title: '< test >', |
| 132 | + h2: '< test >', |
| 133 | + }, |
| 134 | + ], |
| 135 | + ] |
| 136 | + |
| 137 | + testCases.forEach(([source, expected], i) => |
| 138 | + it(`case ${i}`, () => { |
| 139 | + expect(md.render(source)).toEqual(`\ |
| 140 | +<nav class="table-of-contents"><ul><li><a href="#${expected.slug}">${expected.title}</a></li></ul></nav> |
| 141 | +<h2 id="${expected.slug}">${expected.h2}</h2> |
| 142 | +`) |
| 143 | + }) |
| 144 | + ) |
| 145 | + }) |
78 | 146 | })
|
0 commit comments