2
0
mirror of https://github.com/tenrok/BBob.git synced 2026-06-14 18:42:24 +03:00

feat: new @bbob/html api (#4)

This commit is contained in:
Nikolay Kostyurin
2018-10-12 00:43:20 +02:00
committed by GitHub
parent bf993813fc
commit 575c1bb932
12 changed files with 66 additions and 45 deletions
-1
View File
@@ -10,7 +10,6 @@
"lib": "lib"
},
"dependencies": {
"@bbob/core": "^2.1.0",
"@bbob/html": "^2.1.1",
"@bbob/preset-html5": "^2.1.2",
"commander": "^2.15.1"
-3
View File
@@ -21,9 +21,6 @@
"dependencies": {
"@bbob/parser": "^2.2.0"
},
"devDependencies": {
"@bbob/html": "^2.1.1"
},
"peerDependencies": {
"@bbob/html": "^1.x",
"@bbob/preset-html5": "^1.x",
+2 -3
View File
@@ -1,17 +1,16 @@
import render from '@bbob/html'
import { TagNode } from '@bbob/parser'
import core from '../src'
const stringify = val => JSON.stringify(val);
const process = (plugins, input) => core(plugins).process(input, { render });
const process = (plugins, input) => core(plugins).process(input, { render: stringify });
describe('@bbob/core', () => {
test('parse bbcode string to ast and html', () => {
const res = process([], '[style size="15px"]Large Text[/style]');
const ast = res.tree;
expect(res.html).toBe(`<style size="15px">Large Text</style>`);
expect(res.html).toBe('[{"tag":"style","attrs":{"size":"15px"},"content":["Large"," ","Text"]}]');
expect(ast).toBeInstanceOf(Array);
expect(stringify(ast)).toEqual(stringify([
{
+2 -4
View File
@@ -4,10 +4,8 @@
"description": "HTML renderer for BBCode pareser BBob",
"keywords": [],
"dependencies": {
"@bbob/plugin-helper": "^2.0.1"
},
"devDependencies": {
"@bbob/parser": "^2.2.0"
"@bbob/plugin-helper": "^2.0.1",
"@bbob/core": "^2.0.1"
},
"main": "lib/index.js",
"module": "es/index.js",
+4 -1
View File
@@ -1,3 +1,4 @@
import core from '@bbob/core';
import { attrValue } from '@bbob/plugin-helper';
/**
@@ -48,5 +49,7 @@ const renderNodes = (nodes, { stripTags = false } = {}) => []
.concat(nodes)
.reduce((r, node) => r + renderNode(node, { stripTags }), '');
const toHTML = (source, plugins) => core(plugins).process(source, { render: renderNodes }).html;
export const render = renderNodes;
export default renderNodes;
export default toHTML;
+2 -2
View File
@@ -1,8 +1,8 @@
import {parse} from '@bbob/parser'
import core from '@bbob/core'
import {render} from '../src';
const process = (input, params) => {
const ast = parse(input);
const ast = core().process(input).tree;
return render(ast, params)
};
-1
View File
@@ -14,7 +14,6 @@
"@bbob/preset": "^2.1.0"
},
"devDependencies": {
"@bbob/core": "^2.1.0",
"@bbob/html": "^2.1.1"
},
"main": "lib/index.js",
@@ -1,8 +1,7 @@
import core from '@bbob/core'
import { render } from '@bbob/html'
import html from '@bbob/html'
import preset from '../src'
const parse = input => core([preset()]).process(input, {render}).html;
const parse = input => html(input, preset());
describe('@bbob/preset-html5', () => {
test('[b]bolded text[/b]', () => {
+3 -4
View File
@@ -1,7 +1,6 @@
const React = require('react');
const PropTypes = require('prop-types');
const { render } = require('./render');
import React from 'react';
import PropTypes from 'prop-types';
import { render } from './render';
const content = (children, plugins) => React.Children.map(children, child =>
(typeof child === 'string' ? render(child, plugins) : child));
+4 -12
View File
@@ -1,33 +1,25 @@
import React from 'react';
import core from '@bbob/core';
import html from '@bbob/html';
import * as html from '@bbob/html';
import { isTagNode, isStringNode } from '@bbob/plugin-helper';
const toAST = (source, plugins) => core(plugins)
.process(source, {
render: input => html(input, { stripTags: true }),
render: input => html.render(input, { stripTags: true }),
}).tree;
function tagToReactElement(node) {
if (node.content === null) {
return React.createElement(
node.tag,
node.attrs,
null,
);
}
return React.createElement(
node.tag,
node.attrs,
// eslint-disable-next-line no-use-before-define
renderToReactNodes(node.content),
node.content ? renderToReactNodes(node.content) : null,
);
}
function renderToReactNodes(nodes) {
const els = nodes.reduce((arr, node) => {
const els = [].concat(nodes).reduce((arr, node) => {
if (isTagNode(node)) {
arr.push(tagToReactElement(node));
} else if (isStringNode(node)) {