UNPKG

1.35 kBJavaScriptView Raw
1import core from '@bbob/core';
2import { attrsToString } from '@bbob/plugin-helper';
3
4const SELFCLOSE_END_TAG = '/>';
5const CLOSE_START_TAG = '</';
6const START_TAG = '<';
7const END_TAG = '>';
8
9const renderNode = (node, { stripTags = false }) => {
10 if (!node) return '';
11 const type = typeof node;
12
13 if (type === 'string' || type === 'number') {
14 return node;
15 }
16
17 if (type === 'object') {
18 if (stripTags === true) {
19 // eslint-disable-next-line no-use-before-define
20 return renderNodes(node.content, { stripTags });
21 }
22
23 if (node.content === null) {
24 return [START_TAG, node.tag, attrsToString(node.attrs), SELFCLOSE_END_TAG].join('');
25 }
26
27 // eslint-disable-next-line no-use-before-define
28 return [START_TAG, node.tag, attrsToString(node.attrs), END_TAG, renderNodes(node.content), CLOSE_START_TAG, node.tag, END_TAG].join('');
29 }
30
31 if (Array.isArray(node)) {
32 // eslint-disable-next-line no-use-before-define
33 return renderNodes(node, { stripTags });
34 }
35
36 return '';
37};
38
39const renderNodes = (nodes, { stripTags = false } = {}) => []
40 .concat(nodes)
41 .reduce((r, node) => r + renderNode(node, { stripTags }), '');
42
43const toHTML = (source, plugins, options) => core(plugins)
44 .process(source, { ...options, render: renderNodes }).html;
45
46export const render = renderNodes;
47export default toHTML;