Skip to content
Snippets Groups Projects
index.ts 1.77 KiB
Newer Older
marihachi's avatar
marihachi committed
import peg from 'pegjs';
import { MfmNode, MfmPlainNode } from './node';
marihachi's avatar
marihachi committed
import { stringifyNode, stringifyTree } from './util';
marihachi's avatar
marihachi committed
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[] {
marihachi's avatar
marihachi committed
	const nodes = parser.parse(input, { startRule: 'plainParser' });
	return nodes;
}
marihachi's avatar
marihachi committed
export function toString(tree: MfmNode[]): string
marihachi's avatar
marihachi committed
export function toString(node: MfmNode): string
export function toString(node: MfmNode | MfmNode[]): string {
	if (Array.isArray(node)) {
marihachi's avatar
marihachi committed
		return stringifyTree(node);
marihachi's avatar
marihachi committed
	}
	else {
marihachi's avatar
marihachi committed
		return stringifyNode(node);
marihachi's avatar
marihachi committed
	}
marihachi's avatar
marihachi committed
}

marihachi's avatar
marihachi committed
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);
		}
	}
}

marihachi's avatar
marihachi committed
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 { NodeType } from './node';
export {
	MfmNode,
	MfmBlock,
	MfmInline,

	// block
	MfmQuote,
	MfmSearch,
	MfmCodeBlock,
	MfmMathBlock,
	MfmCenter,

	// inline
	MfmUnicodeEmoji,
marihachi's avatar
marihachi committed
	MfmEmojiCode,
	MfmBold,
	MfmSmall,
	MfmItalic,
	MfmStrike,
	MfmInlineCode,
	MfmMathInline,
	MfmMention,
	MfmHashtag,
	MfmUrl,
	MfmLink,
	MfmFn,
	MfmText
} from './node';