diff --git a/tests/src/__snapshots__/index.test.ts.snap b/tests/src/__snapshots__/index.test.ts.snap index 57b5f84..dc496b6 100644 --- a/tests/src/__snapshots__/index.test.ts.snap +++ b/tests/src/__snapshots__/index.test.ts.snap @@ -1,5 +1,128 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`File extension - plugin Options: directiveSyntax should render both file markups 1`] = ` +"

file.txt

+

video.mp4

+" +`; + +exports[`File extension - plugin Options: directiveSyntax should render only new (directive) file markup 1`] = ` +"

{% file src="../file" name="file.txt" %}

+

video.mp4

+" +`; + +exports[`File extension - plugin Options: directiveSyntax should render only old file markup 1`] = ` +"

file.txt

+

:filevideo.mp4

+" +`; + +exports[`File extension - plugin dyrective syntax should add allowed attrs 1`] = ` +"

readme.md

+" +`; + +exports[`File extension - plugin dyrective syntax should generate file token 1`] = ` +[ + Token { + "attrs": null, + "block": true, + "children": null, + "content": "", + "hidden": false, + "info": "", + "level": 0, + "map": [ + 0, + 1, + ], + "markup": "", + "meta": null, + "nesting": 1, + "tag": "p", + "type": "paragraph_open", + }, + Token { + "attrs": null, + "block": true, + "children": [ + Token { + "attrs": [ + [ + "class", + "yfm-file", + ], + [ + "href", + "path/to/video", + ], + [ + "download", + "video.mp4", + ], + ], + "block": false, + "children": null, + "content": "video.mp4", + "hidden": false, + "info": "", + "level": 0, + "map": null, + "markup": ":file", + "meta": null, + "nesting": 0, + "tag": "", + "type": "yfm_file", + }, + ], + "content": ":file[video.mp4](path/to/video)", + "hidden": false, + "info": "", + "level": 1, + "map": [ + 0, + 1, + ], + "markup": "", + "meta": null, + "nesting": 0, + "tag": "", + "type": "inline", + }, + Token { + "attrs": null, + "block": true, + "children": null, + "content": "", + "hidden": false, + "info": "", + "level": 0, + "map": null, + "markup": "", + "meta": null, + "nesting": -1, + "tag": "p", + "type": "paragraph_close", + }, +] +`; + +exports[`File extension - plugin dyrective syntax should not add attrs not from whitelist 1`] = ` +"

a.md

+" +`; + +exports[`File extension - plugin dyrective syntax should render file directive 1`] = ` +"

video.mp4

+" +`; + +exports[`File extension - plugin dyrective syntax should render file inside text 1`] = ` +"

beforefile.txtafter

+" +`; + exports[`File extension - plugin should add extra attrs 1`] = ` "

file.txt

" diff --git a/tests/src/index.test.ts b/tests/src/index.test.ts index d0b9278..2380414 100644 --- a/tests/src/index.test.ts +++ b/tests/src/index.test.ts @@ -1,5 +1,6 @@ import MarkdownIt from 'markdown-it'; import transform from '@diplodoc/transform'; +import dd from 'ts-dedent'; import {type TransformOptions, transform as fileTransformer} from '../../src/plugin'; @@ -7,8 +8,8 @@ function html(markup: string, opts?: TransformOptions) { return transform(markup, { // override the default markdown-it-attrs delimiters, // to make it easier to check html for non-valid file markup - leftDelimiter: '[', - rightDelimiter: ']', + leftDelimiter: '[[', + rightDelimiter: ']]', plugins: [fileTransformer({bundle: false, ...opts})], }).result.html; } @@ -175,4 +176,73 @@ describe('File extension - plugin', () => { }); expect(md.render('{% file src="../file" name="file.txt" %}')).toMatchSnapshot(); }); + + describe('dyrective syntax', () => { + it('should render file directive', () => { + expect( + html(':file[video.mp4](path/to/video)', {directiveSyntax: 'only'}), + ).toMatchSnapshot(); + }); + + it('should render file inside text', () => { + expect( + html('before:file[file.txt](../../docs/readme.md)after', {directiveSyntax: 'only'}), + ).toMatchSnapshot(); + }); + + it('should not render file without file url', () => { + expect(html(':file[file.txt]', {directiveSyntax: 'only'})).toBe( + `

:file[file.txt]

\n`, + ); + }); + + it('should not render file without file name', () => { + expect(html(':file(path/to/file)', {directiveSyntax: 'only'})).toBe( + `

:file(path/to/file)

\n`, + ); + }); + + it('should not add attrs not from whitelist', () => { + expect( + html(':file[a.md](a.md){data-list=1}', {directiveSyntax: 'only'}), + ).toMatchSnapshot(); + }); + + it('should add allowed attrs', () => { + expect( + html( + ':file[readme.md](../readme){referrerpolicy=origin rel=external target=\'_blank\' type="text/plain" hreflang=en}', + { + directiveSyntax: 'only', + }, + ), + ).toMatchSnapshot(); + }); + + it('should generate file token', () => { + expect( + tokens(':file[video.mp4](path/to/video)', {directiveSyntax: 'only'}), + ).toMatchSnapshot(); + }); + }); + + describe('Options: directiveSyntax', () => { + const markup = dd` + {% file src="../file" name="file.txt" %} + + :file[video.mp4](path/to/video) + `; + + it('should render only old file markup', () => { + expect(html(markup, {directiveSyntax: 'disabled'})).toMatchSnapshot(); + }); + + it('should render only new (directive) file markup', () => { + expect(html(markup, {directiveSyntax: 'only'})).toMatchSnapshot(); + }); + + it('should render both file markups', () => { + expect(html(markup, {directiveSyntax: 'enabled'})).toMatchSnapshot(); + }); + }); });