mirror of
https://github.com/tenrok/BBob.git
synced 2026-05-30 15:24:05 +03:00
feat: base preset package '@bbob/preset'
rewrite '@bbob/preset-html5' using new package selfclosed html tags now without space
This commit is contained in:
@@ -1,2 +1,2 @@
|
||||
# bbob-html
|
||||
> Converts bbob-parser AST tree to html
|
||||
#@bbob/html
|
||||
> Converts @bbob/parser AST tree to html
|
||||
|
||||
@@ -9,6 +9,11 @@ const attrs = values =>
|
||||
.reduce((arr, key) => [...arr, attrValue(key, values[key])], [''])
|
||||
.join(' ');
|
||||
|
||||
const SELFCLOSE_END_TAG = '/>';
|
||||
const CLOSE_START_TAG = '</';
|
||||
const START_TAG = '<';
|
||||
const END_TAG = '>';
|
||||
|
||||
const renderNode = (node, { stripTags = false }) => {
|
||||
if (!node) return '';
|
||||
const type = typeof node;
|
||||
@@ -24,11 +29,11 @@ const renderNode = (node, { stripTags = false }) => {
|
||||
}
|
||||
|
||||
if (node.content === null) {
|
||||
return `<${node.tag}${attrs(node.attrs)} />`;
|
||||
return [START_TAG, node.tag, attrs(node.attrs), SELFCLOSE_END_TAG].join('');
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-use-before-define
|
||||
return `<${node.tag}${attrs(node.attrs)}>${renderNodes(node.content)}</${node.tag}>`;
|
||||
return [START_TAG, node.tag, attrs(node.attrs), END_TAG, renderNodes(node.content), CLOSE_START_TAG, node.tag, END_TAG].join('');
|
||||
}
|
||||
|
||||
if (Array.isArray(node)) {
|
||||
|
||||
@@ -1,25 +1,5 @@
|
||||
/* eslint-disable indent */
|
||||
import { isTagNode } from '@bbob/plugin-helper';
|
||||
import { createPreset } from '@bbob/preset';
|
||||
import defaultTags from './defaultTags';
|
||||
|
||||
function process(tags, tree, core) {
|
||||
tree.walk(node => (isTagNode(node) && tags[node.tag]
|
||||
? tags[node.tag](node, core)
|
||||
: node));
|
||||
}
|
||||
|
||||
function html5Preset(opts = {}) {
|
||||
const tags = Object.assign({}, defaultTags, opts.tags || {});
|
||||
|
||||
return (tree, core) => process(tags, tree, core);
|
||||
}
|
||||
|
||||
function extend(callback) {
|
||||
const tags = callback(defaultTags);
|
||||
|
||||
return () => (tree, core) => process(tags, tree, core);
|
||||
}
|
||||
|
||||
html5Preset.extend = extend;
|
||||
|
||||
export default html5Preset;
|
||||
export default createPreset(defaultTags);
|
||||
|
||||
@@ -45,7 +45,7 @@ describe('@bbob/preset-html5', () => {
|
||||
|
||||
test('[img]https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/Go-home-2.svg/100px-Go-home-2.svg.png[/img]', () => {
|
||||
const input = '[img]https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/Go-home-2.svg/100px-Go-home-2.svg.png[/img]';
|
||||
const result = '<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/Go-home-2.svg/100px-Go-home-2.svg.png" />';
|
||||
const result = '<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/Go-home-2.svg/100px-Go-home-2.svg.png"/>';
|
||||
|
||||
expect(parse(input)).toBe(result);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
#@bbob/preset
|
||||
Base function for create presets to process bbcode AST to HTML or Framework specific tags
|
||||
|
||||
## Install
|
||||
|
||||
```bash
|
||||
npm i @bbob/preset
|
||||
```
|
||||
### API
|
||||
|
||||
**For plain HTML**
|
||||
|
||||
```js
|
||||
import { createPreset } from '@bbob/core'
|
||||
|
||||
import { createPreset } from '@bbob/preset'
|
||||
|
||||
const options = {
|
||||
onlyAllowTags: ['url', 'h'],
|
||||
onError: (err) => console.warn(err.message, err.lineNumber, err.columnNumber)
|
||||
};
|
||||
const pre = parse('[url=https://github.com]hello world![/url]', options)
|
||||
```
|
||||
## Usage
|
||||
@@ -1,25 +1,24 @@
|
||||
/* eslint-disable indent */
|
||||
import { isTagNode } from '@bbob/plugin-helper';
|
||||
|
||||
function process(tags, tree, ...rest) {
|
||||
function process(tags, tree, core) {
|
||||
tree.walk(node => (isTagNode(node) && tags[node.tag]
|
||||
? tags[node.tag](node, ...rest)
|
||||
? tags[node.tag](node, core)
|
||||
: node));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param tags
|
||||
* @param defTags
|
||||
* @return {function(*=, *=)}
|
||||
*/
|
||||
function createPreset(tags) {
|
||||
const instance = (tree, ...rest) => process(tags, tree, ...rest);
|
||||
|
||||
instance.extend = (callback) => {
|
||||
const newTags = callback(tags);
|
||||
|
||||
return () => createPreset(newTags);
|
||||
function createPreset(defTags) {
|
||||
const instance = (opts = {}) => {
|
||||
instance.options = Object.assign(instance.options || {}, opts);
|
||||
return (tree, core) => process(defTags, tree, core);
|
||||
};
|
||||
|
||||
instance.extend = callback => createPreset(callback(defTags, instance.options));
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user