mirror of
https://github.com/tenrok/BBob.git
synced 2026-06-20 20:00:33 +03:00
chore(core): string node walk plugin test
This commit is contained in:
@@ -3,12 +3,12 @@
|
|||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const bbob = require('@bbob/core');
|
import bbob from '@bbob/core'
|
||||||
const render = require('@bbob/html');
|
import { render } from '@bbob/html'
|
||||||
const presetHTML5 = require('@bbob/preset-html5');
|
import presetHTML5 from '@bbob/preset-html5'
|
||||||
const code = `[i]Text[/i]`;
|
const code = `[i]Text[/i]`;
|
||||||
|
|
||||||
const html = bbob([presetHTML5()]).process(code, { render }).html;
|
const html = bbob(presetHTML5()).process(code, { render }).html;
|
||||||
|
|
||||||
console.log(html); // <span style="font-style: italic;">Text</span>
|
console.log(html); // <span style="font-style: italic;">Text</span>
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -19,6 +19,10 @@ export default function bbob(plugs) {
|
|||||||
const parseFn = options.parser || parse;
|
const parseFn = options.parser || parse;
|
||||||
const renderFn = options.render;
|
const renderFn = options.render;
|
||||||
|
|
||||||
|
if (typeof parseFn !== 'function') {
|
||||||
|
throw new Error('"parser" is not a function, please pass to "process(input, { parser })" right function');
|
||||||
|
}
|
||||||
|
|
||||||
let tree = options.skipParse
|
let tree = options.skipParse
|
||||||
? input || []
|
? input || []
|
||||||
: parseFn(input, options);
|
: parseFn(input, options);
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import render from '@bbob/html'
|
import render from '@bbob/html'
|
||||||
|
import { TagNode } from '@bbob/parser'
|
||||||
import core from '../src'
|
import core from '../src'
|
||||||
|
|
||||||
const stringify = val => JSON.stringify(val);
|
const stringify = val => JSON.stringify(val);
|
||||||
@@ -21,7 +22,7 @@ describe('@bbob/core', () => {
|
|||||||
]))
|
]))
|
||||||
});
|
});
|
||||||
|
|
||||||
test('plugin walk api', () => {
|
test('plugin walk api node', () => {
|
||||||
const testPlugin = () => (tree) => tree.walk(node => {
|
const testPlugin = () => (tree) => tree.walk(node => {
|
||||||
if (node.tag === 'mytag') {
|
if (node.tag === 'mytag') {
|
||||||
node.attrs = {
|
node.attrs = {
|
||||||
@@ -55,6 +56,38 @@ describe('@bbob/core', () => {
|
|||||||
]));
|
]));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('plugin walk api string', () => {
|
||||||
|
const testPlugin = () => (tree) => tree.walk(node => {
|
||||||
|
if (node === ':)') {
|
||||||
|
return TagNode.create('test-tag')
|
||||||
|
}
|
||||||
|
|
||||||
|
return node
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = process([testPlugin()], '[mytag]Large Text :)[/mytag]');
|
||||||
|
const ast = res.tree;
|
||||||
|
|
||||||
|
expect(ast).toBeInstanceOf(Array);
|
||||||
|
expect(ast.walk).toBeInstanceOf(Function);
|
||||||
|
expect(stringify(ast)).toEqual(stringify([
|
||||||
|
{
|
||||||
|
tag: 'mytag',
|
||||||
|
attrs: {},
|
||||||
|
content: [
|
||||||
|
'Large',
|
||||||
|
' ',
|
||||||
|
'Text',
|
||||||
|
{
|
||||||
|
tag: 'test-tag',
|
||||||
|
attrs: {},
|
||||||
|
content: [],
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]));
|
||||||
|
});
|
||||||
|
|
||||||
test('plugin match api', () => {
|
test('plugin match api', () => {
|
||||||
const testPlugin = () => (tree) => tree.match([{ tag: 'mytag1' }, { tag: 'mytag2' }], node => {
|
const testPlugin = () => (tree) => tree.match([{ tag: 'mytag1' }, { tag: 'mytag2' }], node => {
|
||||||
if (node.attrs) {
|
if (node.attrs) {
|
||||||
|
|||||||
Reference in New Issue
Block a user