1 | import core from '@bbob/core';
|
2 | import { attrsToString } from '@bbob/plugin-helper';
|
3 |
|
4 | const SELFCLOSE_END_TAG = '/>';
|
5 | const CLOSE_START_TAG = '</';
|
6 | const START_TAG = '<';
|
7 | const END_TAG = '>';
|
8 |
|
9 | const 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 |
|
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 |
|
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 |
|
33 | return renderNodes(node, { stripTags });
|
34 | }
|
35 |
|
36 | return '';
|
37 | };
|
38 |
|
39 | const renderNodes = (nodes, { stripTags = false } = {}) => []
|
40 | .concat(nodes)
|
41 | .reduce((r, node) => r + renderNode(node, { stripTags }), '');
|
42 |
|
43 | const toHTML = (source, plugins, options) => core(plugins)
|
44 | .process(source, { ...options, render: renderNodes }).html;
|
45 |
|
46 | export const render = renderNodes;
|
47 | export default toHTML;
|