'use strict'; import unified from 'unified'; import {readFileSync as file} from 'fs'; import {join} from 'path'; import test from 'ava'; import raw from 'rehype-raw'; import reParse from 'remark-parse'; import stringify from 'rehype-stringify'; import remark2rehype from 'remark-rehype'; import parse5 from 'parse5'; import plugin from '..'; 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 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); /* * TODO : * - Invalid scope * - Invalid extended * - aria attributes */ const mainTestString = `Inline *test*{style="em:4"} paragraph. Use **multiple**{ style="color:pink"} inline ~~block~~ tag. Line \`tagCode\`{ style="color:yellow"}.`; test('basic-default', t => { const {contents} = renderDefault(mainTestString); t.deepEqual(parse(contents), parse('

Inline test paragraph. Use multiple inline block tag. Line tagCode.

')); }); test('basic', t => { const {contents} = render(mainTestString); t.deepEqual(parse(contents), parse(`

Inline test paragraph. Use multiple inline block tag. Line tagCode.

`)); }); test('basic-raw', t => { const {contents} = renderRaw(mainTestString); t.deepEqual(parse(contents), parse(`

Inline test paragraph. Use multiple inline block tag. Line tagCode.

`)); }); test('em', t => { const {contents} = render('textexamplenointerest **Important**{style=4em} still no interest'); t.deepEqual(parse(contents), parse(`

textexamplenointerest Important still no interest

`)); }); test('readme-default', t => { const fileExample = file(join(__dirname, 'readMeTest.txt')); const {contents} = renderDefault(fileExample); t.deepEqual(parse(contents), parse(`

alt

Hot babe with computer

This is a title

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.

`)); }); test('readme', t => { const fileExample = file(join(__dirname, 'readMeTest.txt')); const {contents} = render(fileExample); t.deepEqual(parse(contents), parse(`

alt

Hot babe with computer

This is a title

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.

`)); }); test('extended', t => { const renderExtended = generateExtendParser({extends: {image: ['quality']}}); const extentedString = `*Wait* ! This is an awesome image : ![Awesome image](aws://image.jpg){ quality="80" awesomeness="max" } `; const {contents} = renderExtended(extentedString); t.deepEqual(parse(contents), parse(`

Wait ! This is an awesome image : 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 : Awesome image

`)); }); test('extended-global', t => { const renderExtended = generateExtendParser({extend: {'*': ['exAttr']}}); const globalString = ` *Wait* ! You are **beautiful**{ exAttr="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: ['exAttr']}}); const invalidString = `*Wait* ! You are **beautiful**{ exAttr="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!

`)); });