mirror of
https://github.com/tenrok/BBob.git
synced 2026-05-24 14:04:06 +03:00
47 lines
1.1 KiB
JavaScript
47 lines
1.1 KiB
JavaScript
const { attrValue } = require('@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 renderNode = (node, { stripTags = false }) => {
|
|
if (!node) return '';
|
|
const type = typeof node;
|
|
|
|
if (type === 'string' || type === 'number') {
|
|
return node;
|
|
}
|
|
|
|
if (type === 'object') {
|
|
if (stripTags === true) {
|
|
// eslint-disable-next-line no-use-before-define
|
|
return renderNodes(node.content, { stripTags });
|
|
}
|
|
|
|
if (node.content === null) {
|
|
return `<${node.tag}${attrs(node.attrs)} />`;
|
|
}
|
|
|
|
// eslint-disable-next-line no-use-before-define
|
|
return `<${node.tag}${attrs(node.attrs)}>${renderNodes(node.content)}</${node.tag}>`;
|
|
}
|
|
|
|
if (Array.isArray(node)) {
|
|
// eslint-disable-next-line no-use-before-define
|
|
return renderNodes(node, { stripTags });
|
|
}
|
|
|
|
return '';
|
|
};
|
|
|
|
const renderNodes = (nodes, { stripTags = false } = {}) => []
|
|
.concat(nodes)
|
|
.reduce((r, node) => r + renderNode(node, { stripTags }), '');
|
|
|
|
module.exports = renderNodes;
|