Skip to content
Snippets Groups Projects
Commit e06ff15a authored by marihachi's avatar marihachi
Browse files

Merge branch 'work-32' into develop

parents 985e2ab9 d95206fe
No related branches found
No related tags found
No related merge requests found
...@@ -386,7 +386,7 @@ fnArg ...@@ -386,7 +386,7 @@ fnArg
// inline: text // inline: text
text text
= . { return createNode('text', { text: text() }); } = .
// //
// General // General
......
import { MfmNode, MfmText } from './node'; import { MfmNode } from './node';
export function createNode(type: string, props?: Record<string, any>, children?: MfmNode[]): MfmNode { export function createNode(type: string, props?: Record<string, any>, children?: MfmNode[]): MfmNode {
const node: any = { type }; const node: any = { type };
...@@ -11,66 +11,36 @@ export function createNode(type: string, props?: Record<string, any>, children?: ...@@ -11,66 +11,36 @@ export function createNode(type: string, props?: Record<string, any>, children?:
return node; return node;
} }
/** export function mergeText(nodes: (MfmNode | string)[]): MfmNode[] {
* @param predicate specifies whether to group the previous item and the current item const dest: MfmNode[] = [];
* @returns grouped items const storedChars: string[] = [];
*/
export function groupContinuous<T>(arr: T[], predicate: (prev: T, current: T) => boolean): T[][] { /**
const dest: any[][] = []; * Generate a text node from the stored chars, And push it.
*/
function generateText() {
if (storedChars.length > 0) {
const textNode = createNode('text', { text: storedChars.join('') });
dest.push(textNode);
storedChars.length = 0;
}
}
for (let i = 0; i < arr.length; i++) { for (const node of nodes) {
if (i != 0 && predicate(arr[i - 1], arr[i])) { if (typeof node == 'string') {
dest[dest.length - 1].push(arr[i]); // Store the char.
storedChars.push(node);
} }
else { else {
dest.push([arr[i]]); generateText();
dest.push(node);
} }
} }
generateText();
return dest; return dest;
} }
export function mergeGroupedTrees(groupedTrees: MfmNode[][]): MfmNode[] {
return groupedTrees.reduce((acc, val) => acc.concat(val), ([] as MfmNode[]));
}
export function mergeText(trees: MfmNode[] | undefined, recursive?: boolean): MfmNode[] | undefined {
let dest: MfmNode[];
let groupes: MfmNode[][];
if (trees == null) {
return trees;
}
// group trees
groupes = groupContinuous(trees, (prev, current) => prev.type == current.type);
// concatinate text
groupes = groupes.map((group) => {
if (group[0].type == 'text') {
return [
createNode('text', {
text: group.map(i => (i as MfmText).props.text).join('')
})
];
}
return group;
});
// merge groups
dest = mergeGroupedTrees(groupes);
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;
}
}
export function stringifyNode(node: MfmNode): string { export function stringifyNode(node: MfmNode): string {
switch(node.type) { switch(node.type) {
// block // block
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment