'use strict'; const {join} = require('path'); const file = require('fs').readFileSync; const test = require('ava'); const raw = require('rehype-raw'); const reParse = require('remark-parse'); const reFootnt = require('remark-footnotes'); const stringify = require('rehype-stringify'); const remark2rehype = require('remark-rehype'); const unified = require('unified'); const parse5 = require('parse5'); const plugin = require('..'); const renderDefault = text => unified() .use(reParse) .use(plugin) .use(remark2rehype) .use(stringify) .processSync(text); const render = text => unified() .use(reParse) .use(plugin, {allowDangerousDOMEventHandlers: false, scope: 'permissive'}) .use(remark2rehype) .use(stringify) .processSync(text); const renderFootnotes = text => unified() .use(reParse) .use(reFootnt, {inlineNotes: true}) .use(plugin, {allowDangerousDOMEventHandlers: false, scope: 'permissive'}) .use(remark2rehype) .use(stringify) .processSync(text); const renderRaw = text => unified() .use(reParse) .use(plugin, {allowDangerousDOMEventHandlers: false, scope: 'permissive'}) .use(remark2rehype, {allowDangerousHtml: true}) .use(raw) .use(stringify) .processSync(text); const generateExtendParser = extendsOptions => text => unified() .use(reParse) .use(plugin, extendsOptions) .use(remark2rehype) .use(stringify) .processSync(text); const parse = x => parse5.parse(x); const mainTestString = 'Inline *test*{style="em:4"} paragraph. Use **multiple**{ style="color:pink"} inline ~~block~~ tag. Line `tagCode`{ style="color:yellow"}.'; /* Basic tests */ test('basic-default', t => { const {contents} = renderDefault(mainTestString); t.deepEqual(parse(contents), parse('
Inline test paragraph. Use multiple inline block tag. Line tagCode
.
Inline test paragraph. Use multiple inline block tag. Line tagCode
.
Inline test paragraph. Use multiple inline block tag. Line tagCode
.
textexamplenointerest Important still no interest
')); }); test('fenced code', t => { const fencedCodeString = `~~~lang info=string This is an awesome code ~~~ `; const {contents} = render(fencedCodeString); t.deepEqual(parse(contents), parse(`This is an awesome code
`));
});
test('fenced code brackets', t => {
const fencedCodeString = `~~~lang {info=string}
This is an awesome code
~~~
`;
const {contents} = render(fencedCodeString);
t.deepEqual(parse(contents), parse(`This is an awesome code
`));
});
test('fenced code brackets and spaces', t => {
const fencedCodeString = `~~~lang {info=string}
This is an awesome code
~~~
`;
const {contents} = render(fencedCodeString);
t.deepEqual(parse(contents), parse(`This is an awesome code
`));
});
test('image', t => {
const imageMd = '![Test image](url.com){ alt="This is alt" longdesc="qsdf"}';
const {contents} = render(imageMd);
t.deepEqual(parse(contents), parse(''));
});
test('link', t => {
const linkMd = 'This is a link :[Test link](ache.one){ ping="https://ache.one/big.brother"}';
const {contents} = render(linkMd);
t.deepEqual(parse(contents), parse('This is a link :Test link
')); }); test('autolink', t => { const linkMd = 'This is a link :This is a link :https://ache.one
')); }); test('header', t => { const headerMd = ` Title of the article ==================== {data-id="title"} `; const {contents} = renderDefault(headerMd); t.deepEqual(parse(contents), parse('Hey ! That looks cool ! No, that\'s not !')); }); test('linkReference', t => { const linkRef = `[Google][google]{hreflang="en"} [google]: https://google.com `; const {contents} = renderDefault(linkRef); t.deepEqual(parse(contents), parse('
')); }); test('footnote', t => { const footnotes = `Since XP is good we should always use XP[^xp]{data-id=xp} [^xp]: Apply XP principe to XP. `; const {contents} = renderFootnotes(footnotes); t.deepEqual(parse(contents), parse(`Since XP is good we should always use XPxp
Npm stand for node packet manager.
This is a Unicorn !
Your problem is at line 18. My mistake, it's at line 14.
You can use the fprintf
function to format the output to a file.
Npm stand for node packet manager.
This is a Unicorn !
Your problem is at line 18. My mistake, it's at line 14.
You can use the fprintf
function to format the output to a file.
Wait ! This is an awesome image :
`)); }); test('extended Dangerous', t => { const renderExtended = generateExtendParser({extend: {image: ['quality', 'onload']}}); const dangerousString = `*Wait* ! This is an awesome image : ![Awesome image](aws://image.jpg){ quality="80" awesomeness="max" onload="launchAwesomeFunction();" } `; const {contents} = renderExtended(dangerousString); t.deepEqual(parse(contents), parse(`Wait ! This is an awesome image :
`)); }); test('extended-global', t => { const renderExtended = generateExtendParser({extend: {'*': ['ex-attr']}}); const globalString = ' *Wait* ! You are **beautiful**{ ex-attr="true" } !'; const {contents} = renderExtended(globalString); t.deepEqual(parse(contents), parse('Wait ! You are beautiful !
')); }); test('extended-invalid-scope', t => { const renderExtended = generateExtendParser({scope: 'invalid', extend: {strong: ['ex-attr']}}); const invalidString = '*Wait* ! You are **beautiful**{ ex-attr="true" onload="qdss" pss="NOK" } !'; const {contents} = renderExtended(invalidString); t.deepEqual(parse(contents), parse('Wait ! You are beautiful !
')); }); test('invalid-scope', t => { const renderExtended = generateExtendParser({extend: 'exAttr'}); const invalidString = ' *Wait* ! I **love**{ exAttr="true" onload="qdss" pss="NOK" } you !'; const {contents} = renderExtended(invalidString); t.deepEqual(parse(contents), parse('Wait ! I love you !
')); }); test('invalid-extend', t => { const renderExtended = generateExtendParser({extend: 'exAttr'}); const invalidString = ' *Wait* ! I **love**{ exAttr="true" onload="qdss" attr="NOK" style="color: red;"} you!'; const {contents} = renderExtended(invalidString); t.deepEqual(parse(contents), parse('Wait ! I love you!
')); }); /* Special attributes tests * * aria attributes: Focused on accessibility. They have the form aria-* * Global custom attributes: User ended attributes. They have the form data-* * */ test('global-aria', t => { const invalidString = ' *Wait* ! I **love**{ style="color: pink;" aria-love="true" } you!'; const {contents} = renderDefault(invalidString); t.deepEqual(parse(contents), parse('Wait ! I love you!
')); }); test('global custom attribute', t => { const renderExtended = generateExtendParser({extends: {image: ['quality']}}); const extentedString = `*Wait* ! This is a test image : ![test](img.jpg){data-id=2} `; const {contents} = renderExtended(extentedString); t.deepEqual(parse(contents), parse(`Wait ! This is a test image :
`)); t.notDeepEqual(parse(contents), parse(`Wait ! This is a test image :
`)); }); test('global custom attributes 2', t => { const renderExtended = generateExtendParser({extends: {image: ['quality']}}); const extentedString = `*Wait* ! This is a test image : ![test](img.jpg){data-id-node=2} `; const {contents} = renderExtended(extentedString); t.deepEqual(parse(contents), parse(`Wait ! This is a test image :
`)); t.notDeepEqual(parse(contents), parse(`Wait ! This is a test image :
`)); }); test('global custom attributes 3', t => { const renderExtended = generateExtendParser({extends: {image: ['quality']}}); const extentedString = `*Wait* ! This is a test image : ![test](img.jpg){data--id=2} `; const {contents} = renderExtended(extentedString); t.deepEqual(parse(contents), parse(`Wait ! This is a test image :
`)); }); test('global custom attributes 4', t => { const renderExtended = generateExtendParser({extends: {image: ['quality']}}); const extentedString = `*Wait* ! This is a test image : ![test](img.jpg){data-i=2} `; const {contents} = renderExtended(extentedString); t.deepEqual(parse(contents), parse(`Wait ! This is a test image :
`)); t.notDeepEqual(parse(contents), parse(`Wait ! This is a test image :
`)); });