|
1 | 1 | import MarkdownIt from 'markdown-it'; |
2 | 2 | import transform from '@diplodoc/transform'; |
| 3 | +import dd from 'ts-dedent'; |
3 | 4 |
|
4 | 5 | import {type TransformOptions, transform as fileTransformer} from '../../src/plugin'; |
5 | 6 |
|
6 | 7 | function html(markup: string, opts?: TransformOptions) { |
7 | 8 | return transform(markup, { |
8 | 9 | // override the default markdown-it-attrs delimiters, |
9 | 10 | // to make it easier to check html for non-valid file markup |
10 | | - leftDelimiter: '[', |
11 | | - rightDelimiter: ']', |
| 11 | + leftDelimiter: '[[', |
| 12 | + rightDelimiter: ']]', |
12 | 13 | plugins: [fileTransformer({bundle: false, ...opts})], |
13 | 14 | }).result.html; |
14 | 15 | } |
@@ -175,4 +176,73 @@ describe('File extension - plugin', () => { |
175 | 176 | }); |
176 | 177 | expect(md.render('{% file src="../file" name="file.txt" %}')).toMatchSnapshot(); |
177 | 178 | }); |
| 179 | + |
| 180 | + describe('dyrective syntax', () => { |
| 181 | + it('should render file directive', () => { |
| 182 | + expect( |
| 183 | + html(':file[video.mp4](path/to/video)', {directiveSyntax: 'only'}), |
| 184 | + ).toMatchSnapshot(); |
| 185 | + }); |
| 186 | + |
| 187 | + it('should render file inside text', () => { |
| 188 | + expect( |
| 189 | + html('before:file[file.txt](../../docs/readme.md)after', {directiveSyntax: 'only'}), |
| 190 | + ).toMatchSnapshot(); |
| 191 | + }); |
| 192 | + |
| 193 | + it('should not render file without file url', () => { |
| 194 | + expect(html(':file[file.txt]', {directiveSyntax: 'only'})).toBe( |
| 195 | + `<p>:file[file.txt]</p>\n`, |
| 196 | + ); |
| 197 | + }); |
| 198 | + |
| 199 | + it('should not render file without file name', () => { |
| 200 | + expect(html(':file(path/to/file)', {directiveSyntax: 'only'})).toBe( |
| 201 | + `<p>:file(path/to/file)</p>\n`, |
| 202 | + ); |
| 203 | + }); |
| 204 | + |
| 205 | + it('should not add attrs not from whitelist', () => { |
| 206 | + expect( |
| 207 | + html(':file[a.md](a.md){data-list=1}', {directiveSyntax: 'only'}), |
| 208 | + ).toMatchSnapshot(); |
| 209 | + }); |
| 210 | + |
| 211 | + it('should add allowed attrs', () => { |
| 212 | + expect( |
| 213 | + html( |
| 214 | + ':file[readme.md](../readme){referrerpolicy=origin rel=external target=\'_blank\' type="text/plain" hreflang=en}', |
| 215 | + { |
| 216 | + directiveSyntax: 'only', |
| 217 | + }, |
| 218 | + ), |
| 219 | + ).toMatchSnapshot(); |
| 220 | + }); |
| 221 | + |
| 222 | + it('should generate file token', () => { |
| 223 | + expect( |
| 224 | + tokens(':file[video.mp4](path/to/video)', {directiveSyntax: 'only'}), |
| 225 | + ).toMatchSnapshot(); |
| 226 | + }); |
| 227 | + }); |
| 228 | + |
| 229 | + describe('Options: directiveSyntax', () => { |
| 230 | + const markup = dd` |
| 231 | + {% file src="../file" name="file.txt" %} |
| 232 | +
|
| 233 | + :file[video.mp4](path/to/video) |
| 234 | + `; |
| 235 | + |
| 236 | + it('should render only old file markup', () => { |
| 237 | + expect(html(markup, {directiveSyntax: 'disabled'})).toMatchSnapshot(); |
| 238 | + }); |
| 239 | + |
| 240 | + it('should render only new (directive) file markup', () => { |
| 241 | + expect(html(markup, {directiveSyntax: 'only'})).toMatchSnapshot(); |
| 242 | + }); |
| 243 | + |
| 244 | + it('should render both file markups', () => { |
| 245 | + expect(html(markup, {directiveSyntax: 'enabled'})).toMatchSnapshot(); |
| 246 | + }); |
| 247 | + }); |
178 | 248 | }); |
0 commit comments