From c9e1dabf401ecac9f667e82930d9d2d08e45fd58 Mon Sep 17 00:00:00 2001 From: Nikolay Kostyurin Date: Fri, 29 Mar 2019 10:29:16 +0200 Subject: [PATCH] feat(html): @bbob/html now can be used without @bbob/core --- packages/bbob-html/src/index.js | 18 ++++--------- packages/bbob-html/test/index.test.js | 9 ++----- packages/bbob-plugin-helper/src/index.js | 25 +++++++++++++++++++ .../bbob-plugin-helper/test/index.test.js | 9 +++++++ 4 files changed, 41 insertions(+), 20 deletions(-) diff --git a/packages/bbob-html/src/index.js b/packages/bbob-html/src/index.js index 47cd1f4..10acab4 100644 --- a/packages/bbob-html/src/index.js +++ b/packages/bbob-html/src/index.js @@ -1,14 +1,5 @@ import core from '@bbob/core'; -import { attrValue } from '@bbob/plugin-helper'; - -/** - * Transforms attrs to html params string - * @param values - */ -const attrs = values => - Object.keys(values) - .reduce((arr, key) => [...arr, attrValue(key, values[key])], ['']) - .join(' '); +import { attrsToString } from '@bbob/plugin-helper'; const SELFCLOSE_END_TAG = '/>'; const CLOSE_START_TAG = ' { } if (node.content === null) { - return [START_TAG, node.tag, attrs(node.attrs), SELFCLOSE_END_TAG].join(''); + return [START_TAG, node.tag, attrsToString(node.attrs), SELFCLOSE_END_TAG].join(''); } // eslint-disable-next-line no-use-before-define - return [START_TAG, node.tag, attrs(node.attrs), END_TAG, renderNodes(node.content), CLOSE_START_TAG, node.tag, END_TAG].join(''); + return [START_TAG, node.tag, attrsToString(node.attrs), END_TAG, renderNodes(node.content), CLOSE_START_TAG, node.tag, END_TAG].join(''); } if (Array.isArray(node)) { @@ -49,7 +40,8 @@ const renderNodes = (nodes, { stripTags = false } = {}) => [] .concat(nodes) .reduce((r, node) => r + renderNode(node, { stripTags }), ''); -const toHTML = (source, plugins) => core(plugins).process(source, { render: renderNodes }).html; +const toHTML = (source, plugins, options) => core(plugins) + .process(source, { ...options, render: renderNodes }).html; export const render = renderNodes; export default toHTML; diff --git a/packages/bbob-html/test/index.test.js b/packages/bbob-html/test/index.test.js index 4fad699..453912c 100644 --- a/packages/bbob-html/test/index.test.js +++ b/packages/bbob-html/test/index.test.js @@ -1,11 +1,6 @@ -import core from '@bbob/core' -import {render} from '../src'; +import toHTML, {render} from '../src'; -const process = (input, params) => { - const ast = core().process(input).tree; - - return render(ast, params) -}; +const process = (input, params) => toHTML(input, [], params); describe('@bbob/html', () => { test('render bbcode tag with single param as html tag', () => { diff --git a/packages/bbob-plugin-helper/src/index.js b/packages/bbob-plugin-helper/src/index.js index 10628a3..5da62b8 100644 --- a/packages/bbob-plugin-helper/src/index.js +++ b/packages/bbob-plugin-helper/src/index.js @@ -14,12 +14,27 @@ const getNodeLength = (node) => { return 0; }; +/** + * Appends value to Tag Node + * @param {TagNode} node + * @param value + */ const appendToNode = (node, value) => { node.content.push(value); }; +/** + * Replaces " to &qquot; + * @param {String} value + */ const escapeQuote = value => value.replace(/"/g, '"'); +/** + * Acept name and value and return valid html5 attribute string + * @param {String} name + * @param {String} value + * @return {string} + */ const attrValue = (name, value) => { const type = typeof value; @@ -33,7 +48,17 @@ const attrValue = (name, value) => { return types[type] ? types[type]() : ''; }; +/** + * Transforms attrs to html params string + * @param values + */ +const attrsToString = values => + Object.keys(values) + .reduce((arr, key) => [...arr, attrValue(key, values[key])], ['']) + .join(' '); + export { + attrsToString, attrValue, appendToNode, getNodeLength, diff --git a/packages/bbob-plugin-helper/test/index.test.js b/packages/bbob-plugin-helper/test/index.test.js index ba23186..4f4fde1 100644 --- a/packages/bbob-plugin-helper/test/index.test.js +++ b/packages/bbob-plugin-helper/test/index.test.js @@ -1,4 +1,5 @@ import { + attrsToString, attrValue, appendToNode, getNodeLength, @@ -69,5 +70,13 @@ describe('@bbob/plugin-helper', () => { test('isEOL', () => { expect(isEOL('\n')).toBe(true) + }); + + test('attrsToString', () => { + expect(attrsToString({ + tag: 'test', + foo: 'bar', + disabled: true + })).toBe(` tag="test" foo="bar" disabled`) }) });