diff --git a/src/api.ts b/src/api.ts index e0c63832bb7c6ba358a2ebb16bc1e0ace1c6d429..140324a1bf4b29890a05a447043b54999c4768b7 100644 --- a/src/api.ts +++ b/src/api.ts @@ -4,16 +4,25 @@ import { stringifyNode, stringifyTree } from './util'; const parser: peg.Parser = require('./parser'); +/** + * generate a node tree from the MFM string. +*/ export function parse(input: string): MfmNode[] { const nodes = parser.parse(input, { startRule: 'fullParser' }); return nodes; } +/** + * generate a node tree of plain from the MFM string. +*/ export function parsePlain(input: string): MfmPlainNode[] { const nodes = parser.parse(input, { startRule: 'plainParser' }); return nodes; } +/** + * generate a MFM string from the node tree. +*/ export function toString(tree: MfmNode[]): string export function toString(node: MfmNode): string export function toString(node: MfmNode | MfmNode[]): string { @@ -25,6 +34,9 @@ export function toString(node: MfmNode | MfmNode[]): string { } } +/** + * Inspects the node tree. +*/ export function inspect(nodes: MfmNode[], action: (node: MfmNode) => void): void { for (const node of nodes) { action(node); @@ -34,6 +46,10 @@ export function inspect(nodes: MfmNode[], action: (node: MfmNode) => void): void } } +/** + * Inspects the node tree and returns as an array the nodes that match the conditions + * of the predicate function. +*/ export function extract(nodes: MfmNode[], predicate: (node: MfmNode) => boolean): MfmNode[] { const dest = [] as MfmNode[];