From 5cc6dd928aab776392209cd58012af8bf9a2d2b4 Mon Sep 17 00:00:00 2001 From: ache Date: Mon, 31 Dec 2018 16:05:48 +0100 Subject: More tests --- __tests__/index.js | 52 +++++++++++++++++----- src/index.js | 126 ----------------------------------------------------- 2 files changed, 42 insertions(+), 136 deletions(-) diff --git a/__tests__/index.js b/__tests__/index.js index 5d4c995..d4729df 100644 --- a/__tests__/index.js +++ b/__tests__/index.js @@ -1,11 +1,14 @@ -import test from 'ava'; -import unified from 'unified'; -import raw from 'rehype-raw'; -import reParse from 'remark-parse'; -import stringify from 'rehype-stringify'; -import remark2rehype from 'remark-rehype'; +'user strict'; -import plugin from '../app'; +import plugin from '..'; + +const test = require('ava'); +const unified = require('unified'); +const raw = require('rehype-raw'); +const reParse = require('remark-parse'); +const stringify = require('rehype-stringify'); +const remark2rehype = require('remark-rehype'); +const parse5 = require('parse5'); const render = text => unified() .use(reParse) @@ -24,15 +27,44 @@ const renderRaw = text => unified() test('simple citation', t => { const {contents} = render('>This is a citation'); - t.is(contents, '
\n

This is a citation

\n
'); + t.deepEqual(parse5.parse(contents), + parse5.parse('
\n

This is a citation

\n
') + ); }); test('simple citation raw', t => { const {contents} = renderRaw('>This is a citation'); - t.is(contents, '
\n

This is a citation

\n
'); + t.deepEqual(parse5.parse(contents), + parse5.parse('
\n

This is a citation

\n
') + ); }); test('question', t => { const {contents} = renderRaw('>!question\nWhat does the šŸ¦Š say ?'); - t.snapshot(contents); + t.deepEqual(parse5.parse(contents), + parse5.parse('

What does the šŸ¦Š say ?

')); +}); + +test('attention', t => { + const {contents} = renderRaw('>!attention\nBe carefull'); + t.deepEqual(parse5.parse(contents), + parse5.parse('

Be carefull

')); +}); + +test('good', t => { + const {contents} = renderRaw('>!good\nšŸŽ‰'); + t.deepEqual(parse5.parse(contents), + parse5.parse('

šŸŽ‰

')); +}); + +test('bad', t => { + const {contents} = renderRaw('>!bad\nā˜¢ļø'); + t.deepEqual(parse5.parse(contents), + parse5.parse('

ā˜¢ļø

')); +}); + +test('secret', t => { + const {contents} = renderRaw('>!secret Don\'t tell others about it\nšŸ¤«'); + t.deepEqual(parse5.parse(contents), + parse5.parse('

šŸ¤«

Don\'t tell others about it
')); }); diff --git a/src/index.js b/src/index.js index 6f8b5d5..e69de29 100644 --- a/src/index.js +++ b/src/index.js @@ -1,126 +0,0 @@ -const visit = require('unist-util-visit'); - -/** - * Given the MDAST ast, look for all fenced Blockquote - * - * @param {object} ast - * @param {vFile} vFile - * @return {function} - */ -function visitBlockquote(ast) { - return visit(ast, 'blockquote', (node, index, parent) => { - const firstNode = node.children[0]; - - if (firstNode.type === 'paragraph') { - if (firstNode.children[0].type === 'text') { - const firstChild = firstNode.children[0]; - if (firstChild.value.startsWith('!secret')) { - node.type = 'div'; - firstChild.value = firstChild.value.substr(7); - let sum = ''; - if (firstChild.value.indexOf('\n') >= 0) { - sum = firstChild.value.substr(0, - firstChild.value.indexOf('\n')); - firstChild.value = firstChild.value.substr( - firstChild.value.indexOf('\n')); - } else { - sum = firstChild.value; - firstChild.value = ''; - } - - const secret = { - type: 'special-box-secret', - children: [ - node, - { - type: 'summary', - data: { - hName: 'summary', - hChildren: [{ - type: 'text', - value: sum || 'Spoiler', - }], - }, - }, - ], - data: { - hName: 'details', - hProperties: { - className: 'special-box secret', - }, - }, - }; - - parent.children.splice(index, 1, secret); - - return node; - } else if (firstChild.value.startsWith('!information') || - firstChild.value.startsWith('!good') || - firstChild.value.startsWith('!bad') || - firstChild.value.startsWith('!comment') || - firstChild.value.startsWith('!attention') || - firstChild.value.startsWith('!question')) { - node.type = 'div'; - node.data = { - hName: 'div', - hProperties: { - className: 'special-box-content', - }, - }; - let type = ''; - if (firstChild.value.indexOf('\n') > 0) { - type = firstChild.value.substr(1, firstChild.value.indexOf('\n')); - firstChild.value = firstChild.value.substr(firstChild.value.indexOf('\n')); - } else { - type = firstChild.value.substr(1); - firstChild.value = ''; - } - - const box = { - type: 'special-box-div', - children: [node], - data: { - hName: 'div', - hProperties: { - className: `special-box ${type}`, - }, - }, - }; - - parent.children.splice(index, 1, box); - - return node; - } - } - } - return node; - }); -} - -/** - * Returns the transformer which acst on the MDAST tree and given VFile. - * - * @link https://github.com/unifiedjs/unified#function-transformernode-file-next - * @link https://github.com/syntax-tree/mdast - * @link https://github.com/vfile/vfile - * @return {function} - */ -function box() { - /** - * @param {object} ast MDAST - * @param {vFile} vFile - * @param {function} next - * @return {object} - */ - return function (ast, vFile, next) { // Transformer - visitBlockquote(ast); - - if (typeof next === 'function') { - return next(null, ast, vFile); - } - - return ast; - }; -} - -module.exports = box; -- cgit v1.2.3