diff --git a/src/index.ts b/src/index.ts index fff528e..16a2afc 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 { MfmNode, MfmBlock, diff --git a/test/main.ts b/test/main.ts index 49555f5..8aabd05 100644 --- a/test/main.ts +++ b/test/main.ts @@ -1,5 +1,5 @@ import assert from 'assert'; -import { parse, parsePlain } from '../built/index'; +import { inspect, parse, parsePlain, toString } from '../built/index'; import { createNode } from '../built/util'; import { 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); }); + +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!]'); + }); +});