diff --git a/src/api.ts b/src/api.ts new file mode 100644 index 0000000000000000000000000000000000000000..88e17e8b567c9de96ecbdec0a304d36368399858 --- /dev/null +++ b/src/api.ts @@ -0,0 +1,58 @@ +import peg from 'pegjs'; +import { MfmNode, MfmPlainNode } from './node'; +import { stringifyNode, stringifyTree } from './util'; + +const parser: peg.Parser = require('./parser'); + +export function parse(input: string): MfmNode[] { + const nodes = parser.parse(input, { startRule: 'fullParser' }); + return nodes; +} + +export function parsePlain(input: string): MfmPlainNode[] { + const nodes = parser.parse(input, { startRule: 'plainParser' }); + return nodes; +} + +export function toString(tree: MfmNode[]): string +export function toString(node: MfmNode): string +export function toString(node: MfmNode | MfmNode[]): string { + if (Array.isArray(node)) { + return stringifyTree(node); + } + else { + return stringifyNode(node); + } +} + +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 function extract(nodes: MfmNode[], type: (MfmNode['type'] | MfmNode['type'][])): MfmNode[] { + function predicate(node: MfmNode, type: (MfmNode['type'] | MfmNode['type'][])): boolean { + if (Array.isArray(type)) { + return (type.some(i => i == node.type)); + } + else { + return (type == node.type); + } + } + + const dest = [] as MfmNode[]; + for (const node of nodes) { + if (predicate(node, type)) { + dest.push(node); + } + if (node.children != null) { + dest.push(...extract(node.children, type)); + } + } + + return dest; +} diff --git a/src/index.ts b/src/index.ts index 7c47787c5243e68852fe18aaf5f43a7119fc509c..292d5aced092e6ba89b297a1b0d72e23040493f3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,60 +1,10 @@ -import peg from 'pegjs'; -import { MfmNode, MfmPlainNode } from './node'; -import { stringifyNode, stringifyTree } from './util'; -const parser: peg.Parser = require('./parser'); - -export function parse(input: string): MfmNode[] { - const nodes = parser.parse(input, { startRule: 'fullParser' }); - return nodes; -} - -export function parsePlain(input: string): MfmPlainNode[] { - const nodes = parser.parse(input, { startRule: 'plainParser' }); - return nodes; -} - -export function toString(tree: MfmNode[]): string -export function toString(node: MfmNode): string -export function toString(node: MfmNode | MfmNode[]): string { - if (Array.isArray(node)) { - return stringifyTree(node); - } - else { - return stringifyNode(node); - } -} - -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 function extract(nodes: MfmNode[], type: (MfmNode['type'] | MfmNode['type'][])): MfmNode[] { - function predicate(node: MfmNode, type: (MfmNode['type'] | MfmNode['type'][])): boolean { - if (Array.isArray(type)) { - return (type.some(i => i == node.type)); - } - else { - return (type == node.type); - } - } - - const dest = [] as MfmNode[]; - for (const node of nodes) { - if (predicate(node, type)) { - dest.push(node); - } - if (node.children != null) { - dest.push(...extract(node.children, type)); - } - } - - return dest; -} +export { + parse, + parsePlain, + toString, + inspect, + extract +} from './api'; export { NodeType } from './node';