From 4fe75b1341a23fc2163b8d41c1951151c68a9061 Mon Sep 17 00:00:00 2001 From: marihachi <marihachi0620@gmail.com> Date: Sat, 27 Mar 2021 16:54:04 +0900 Subject: [PATCH] wip --- src/parser.pegjs | 2 +- src/util.ts | 42 +++++++++++++++++------------------------- 2 files changed, 18 insertions(+), 26 deletions(-) diff --git a/src/parser.pegjs b/src/parser.pegjs index 34dd956..9a3d977 100644 --- a/src/parser.pegjs +++ b/src/parser.pegjs @@ -386,7 +386,7 @@ fnArg // inline: text text - = . { return createNode('text', { text: text() }); } + = . // // General diff --git a/src/util.ts b/src/util.ts index 8b96d18..d286550 100644 --- a/src/util.ts +++ b/src/util.ts @@ -30,45 +30,37 @@ export function groupContinuous<T>(arr: T[], predicate: (prev: T, current: T) => return dest; } -export function mergeGroupedTrees(groupedTrees: MfmNode[][]): MfmNode[] { - return groupedTrees.reduce((acc, val) => acc.concat(val), ([] as MfmNode[])); +export function mergeGroupedTrees<T>(groupedTrees: T[][]): T[] { + return groupedTrees.reduce((acc, val) => acc.concat(val), ([] as T[])); } -export function mergeText(trees: MfmNode[] | undefined, recursive?: boolean): MfmNode[] | undefined { - let dest: MfmNode[]; - let groupes: MfmNode[][]; - - if (trees == null) { - return trees; - } - +export function mergeText(trees: (MfmNode | string)[]): MfmNode[] { // group trees - groupes = groupContinuous(trees, (prev, current) => prev.type == current.type); + const groupes = groupContinuous(trees, (prev, current) => { + if (typeof prev == 'string' || typeof current == 'string') { + return (typeof prev == 'string' && typeof current == 'string'); + } + else { + return (prev.type == current.type); + } + }); // concatinate text - groupes = groupes.map((group) => { - if (group[0].type == 'text') { + const concatGroupes = groupes.map((group) => { + if (typeof group[0] == 'string') { return [ createNode('text', { - text: group.map(i => (i as MfmText).props.text).join('') + text: (group as string[]).join('') }) ]; } - return group; + return (group as MfmNode[]); }); // merge groups - dest = mergeGroupedTrees(groupes); + const dest = mergeGroupedTrees(concatGroupes); - if (recursive) { - return dest.map(tree => { - // apply recursively to children - return createNode(tree.type, tree.props, recursive ? mergeText(tree.children) : tree.children); - }); - } - else { - return dest; - } + return dest; } export function stringifyNode(node: MfmNode): string { -- GitLab