UNPKG

1.47 kBJavaScriptView Raw
1/* eslint-disable no-use-before-define,import/prefer-default-export */ import core from '@bbob/core';
2import * as html from '@bbob/html';
3import { isStringNode, isTagNode } from '@bbob/plugin-helper';
4const toAST = (source, plugins = [], options = {})=>core(plugins).process(source, {
5 ...options,
6 render: (input)=>html.render(input, {
7 stripTags: true
8 })
9 }).tree;
10const isContentEmpty = (content)=>!content || content.length === 0;
11function tagToVueNode(createElement, node, index) {
12 const { class: className , style , ...domProps } = node.attrs || {};
13 return createElement(node.tag, {
14 key: index,
15 class: className,
16 style,
17 domProps
18 }, isContentEmpty(node.content) ? null : renderToVueNodes(createElement, node.content));
19}
20function renderToVueNodes(createElement, nodes) {
21 return [].concat(nodes).reduce((arr, node, index)=>{
22 if (isTagNode(node)) {
23 arr.push(tagToVueNode(createElement, node, index));
24 } else if (isStringNode(node)) {
25 arr.push(node);
26 }
27 return arr;
28 }, []);
29}
30/**
31 * Converts string to Vue 2 VNodes
32 * @param createElement {CreateElement}
33 * @param source {String}
34 * @param plugins {Array<Function>}
35 * @param options {Object}
36 * @returns {Array<VNode>}
37 */ export function render(createElement, source, plugins, options) {
38 return renderToVueNodes(createElement, toAST(source, plugins, options));
39}