mirror of
https://github.com/tenrok/BBob.git
synced 2026-06-20 20:00:33 +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
|
#@bbob/html
|
||||||
> Converts bbob-parser AST tree to html
|
> Converts @bbob/parser AST tree to html
|
||||||
|
|||||||
@@ -9,6 +9,11 @@ const attrs = values =>
|
|||||||
.reduce((arr, key) => [...arr, attrValue(key, values[key])], [''])
|
.reduce((arr, key) => [...arr, attrValue(key, values[key])], [''])
|
||||||
.join(' ');
|
.join(' ');
|
||||||
|
|
||||||
|
const SELFCLOSE_END_TAG = '/>';
|
||||||
|
const CLOSE_START_TAG = '</';
|
||||||
|
const START_TAG = '<';
|
||||||
|
const END_TAG = '>';
|
||||||
|
|
||||||
const renderNode = (node, { stripTags = false }) => {
|
const renderNode = (node, { stripTags = false }) => {
|
||||||
if (!node) return '';
|
if (!node) return '';
|
||||||
const type = typeof node;
|
const type = typeof node;
|
||||||
@@ -24,11 +29,11 @@ const renderNode = (node, { stripTags = false }) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (node.content === null) {
|
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
|
// 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)) {
|
if (Array.isArray(node)) {
|
||||||
|
|||||||
@@ -1,25 +1,5 @@
|
|||||||
/* eslint-disable indent */
|
/* eslint-disable indent */
|
||||||
import { isTagNode } from '@bbob/plugin-helper';
|
import { createPreset } from '@bbob/preset';
|
||||||
import defaultTags from './defaultTags';
|
import defaultTags from './defaultTags';
|
||||||
|
|
||||||
function process(tags, tree, core) {
|
export default createPreset(defaultTags);
|
||||||
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;
|
|
||||||
|
|||||||
@@ -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]', () => {
|
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 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);
|
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 */
|
/* eslint-disable indent */
|
||||||
import { isTagNode } from '@bbob/plugin-helper';
|
import { isTagNode } from '@bbob/plugin-helper';
|
||||||
|
|
||||||
function process(tags, tree, ...rest) {
|
function process(tags, tree, core) {
|
||||||
tree.walk(node => (isTagNode(node) && tags[node.tag]
|
tree.walk(node => (isTagNode(node) && tags[node.tag]
|
||||||
? tags[node.tag](node, ...rest)
|
? tags[node.tag](node, core)
|
||||||
: node));
|
: node));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param tags
|
* @param defTags
|
||||||
* @return {function(*=, *=)}
|
* @return {function(*=, *=)}
|
||||||
*/
|
*/
|
||||||
function createPreset(tags) {
|
function createPreset(defTags) {
|
||||||
const instance = (tree, ...rest) => process(tags, tree, ...rest);
|
const instance = (opts = {}) => {
|
||||||
|
instance.options = Object.assign(instance.options || {}, opts);
|
||||||
instance.extend = (callback) => {
|
return (tree, core) => process(defTags, tree, core);
|
||||||
const newTags = callback(tags);
|
|
||||||
|
|
||||||
return () => createPreset(newTags);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
instance.extend = callback => createPreset(callback(defTags, instance.options));
|
||||||
|
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user