Merge branch 'work-31' into develop

This commit is contained in:
marihachi 2021-03-27 01:41:35 +09:00
commit bf31395071
2 changed files with 23 additions and 1 deletions

View file

@ -24,6 +24,15 @@ export function toString(node: MfmNode | MfmNode[]): string {
} }
} }
export function inspect(tree: MfmNode[], action: (node: MfmNode) => void): void {
for (const node of tree) {
action(node);
if (node.children != null) {
inspect(node.children, action);
}
}
}
export { export {
MfmNode, MfmNode,
MfmBlock, MfmBlock,

View file

@ -1,5 +1,5 @@
import assert from 'assert'; import assert from 'assert';
import { parse, parsePlain } from '../built/index'; import { inspect, parse, parsePlain, toString } from '../built/index';
import { createNode } from '../built/util'; import { createNode } from '../built/util';
import { import {
TEXT, CENTER, FN, UNI_EMOJI, MENTION, CUSTOM_EMOJI, HASHTAG, N_URL, BOLD, SMALL, ITALIC, STRIKE, QUOTE TEXT, CENTER, FN, UNI_EMOJI, MENTION, CUSTOM_EMOJI, HASHTAG, N_URL, BOLD, SMALL, ITALIC, STRIKE, QUOTE
@ -401,3 +401,16 @@ https://github.com/syuilo/ai
]; ];
assert.deepStrictEqual(parse(input), output); assert.deepStrictEqual(parse(input), output);
}); });
describe('inspect', () => {
it('replace text', () => {
const input = 'good morning [tada everynyan!]';
const result = parse(input);
inspect(result, node => {
if (node.type == 'text') {
node.props.text = node.props.text.replace(/good morning/g, 'hello');
}
});
assert.strictEqual(toString(result), 'hello [tada everynyan!]');
});
});