mirror of
https://github.com/tenrok/BBob.git
synced 2026-06-20 20:00:33 +03:00
feat(html): @bbob/html now can be used without @bbob/core
This commit is contained in:
@@ -1,14 +1,5 @@
|
|||||||
import core from '@bbob/core';
|
import core from '@bbob/core';
|
||||||
import { attrValue } from '@bbob/plugin-helper';
|
import { attrsToString } 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(' ');
|
|
||||||
|
|
||||||
const SELFCLOSE_END_TAG = '/>';
|
const SELFCLOSE_END_TAG = '/>';
|
||||||
const CLOSE_START_TAG = '</';
|
const CLOSE_START_TAG = '</';
|
||||||
@@ -30,11 +21,11 @@ const renderNode = (node, { stripTags = false }) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (node.content === null) {
|
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
|
// 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)) {
|
if (Array.isArray(node)) {
|
||||||
@@ -49,7 +40,8 @@ const renderNodes = (nodes, { stripTags = false } = {}) => []
|
|||||||
.concat(nodes)
|
.concat(nodes)
|
||||||
.reduce((r, node) => r + renderNode(node, { stripTags }), '');
|
.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 const render = renderNodes;
|
||||||
export default toHTML;
|
export default toHTML;
|
||||||
|
|||||||
@@ -1,11 +1,6 @@
|
|||||||
import core from '@bbob/core'
|
import toHTML, {render} from '../src';
|
||||||
import {render} from '../src';
|
|
||||||
|
|
||||||
const process = (input, params) => {
|
const process = (input, params) => toHTML(input, [], params);
|
||||||
const ast = core().process(input).tree;
|
|
||||||
|
|
||||||
return render(ast, params)
|
|
||||||
};
|
|
||||||
|
|
||||||
describe('@bbob/html', () => {
|
describe('@bbob/html', () => {
|
||||||
test('render bbcode tag with single param as html tag', () => {
|
test('render bbcode tag with single param as html tag', () => {
|
||||||
|
|||||||
@@ -14,12 +14,27 @@ const getNodeLength = (node) => {
|
|||||||
return 0;
|
return 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Appends value to Tag Node
|
||||||
|
* @param {TagNode} node
|
||||||
|
* @param value
|
||||||
|
*/
|
||||||
const appendToNode = (node, value) => {
|
const appendToNode = (node, value) => {
|
||||||
node.content.push(value);
|
node.content.push(value);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Replaces " to &qquot;
|
||||||
|
* @param {String} value
|
||||||
|
*/
|
||||||
const escapeQuote = value => value.replace(/"/g, '"');
|
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 attrValue = (name, value) => {
|
||||||
const type = typeof value;
|
const type = typeof value;
|
||||||
|
|
||||||
@@ -33,7 +48,17 @@ const attrValue = (name, value) => {
|
|||||||
return types[type] ? types[type]() : '';
|
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 {
|
export {
|
||||||
|
attrsToString,
|
||||||
attrValue,
|
attrValue,
|
||||||
appendToNode,
|
appendToNode,
|
||||||
getNodeLength,
|
getNodeLength,
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
|
attrsToString,
|
||||||
attrValue,
|
attrValue,
|
||||||
appendToNode,
|
appendToNode,
|
||||||
getNodeLength,
|
getNodeLength,
|
||||||
@@ -69,5 +70,13 @@ describe('@bbob/plugin-helper', () => {
|
|||||||
|
|
||||||
test('isEOL', () => {
|
test('isEOL', () => {
|
||||||
expect(isEOL('\n')).toBe(true)
|
expect(isEOL('\n')).toBe(true)
|
||||||
|
});
|
||||||
|
|
||||||
|
test('attrsToString', () => {
|
||||||
|
expect(attrsToString({
|
||||||
|
tag: 'test',
|
||||||
|
foo: 'bar',
|
||||||
|
disabled: true
|
||||||
|
})).toBe(` tag="test" foo="bar" disabled`)
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user