mirror of
https://github.com/tenrok/BBob.git
synced 2026-05-15 11:59:37 +03:00
chore(core): string node walk plugin test
This commit is contained in:
@@ -3,12 +3,12 @@
|
||||
## Usage
|
||||
|
||||
```js
|
||||
const bbob = require('@bbob/core');
|
||||
const render = require('@bbob/html');
|
||||
const presetHTML5 = require('@bbob/preset-html5');
|
||||
import bbob from '@bbob/core'
|
||||
import { render } from '@bbob/html'
|
||||
import presetHTML5 from '@bbob/preset-html5'
|
||||
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>
|
||||
```
|
||||
|
||||
@@ -19,6 +19,10 @@ export default function bbob(plugs) {
|
||||
const parseFn = options.parser || parse;
|
||||
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
|
||||
? input || []
|
||||
: parseFn(input, options);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import render from '@bbob/html'
|
||||
import { TagNode } from '@bbob/parser'
|
||||
import core from '../src'
|
||||
|
||||
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 => {
|
||||
if (node.tag === 'mytag') {
|
||||
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', () => {
|
||||
const testPlugin = () => (tree) => tree.match([{ tag: 'mytag1' }, { tag: 'mytag2' }], node => {
|
||||
if (node.attrs) {
|
||||
|
||||
Reference in New Issue
Block a user