mirror of
https://activitypub.software/TransFem-org/sfm-js
synced 2024-11-25 07:25:13 +00:00
wip
This commit is contained in:
parent
ae2296d6e1
commit
4fe75b1341
2 changed files with 18 additions and 26 deletions
|
@ -386,7 +386,7 @@ fnArg
|
||||||
// inline: text
|
// inline: text
|
||||||
|
|
||||||
text
|
text
|
||||||
= . { return createNode('text', { text: text() }); }
|
= .
|
||||||
|
|
||||||
//
|
//
|
||||||
// General
|
// General
|
||||||
|
|
40
src/util.ts
40
src/util.ts
|
@ -30,46 +30,38 @@ export function groupContinuous<T>(arr: T[], predicate: (prev: T, current: T) =>
|
||||||
return dest;
|
return dest;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function mergeGroupedTrees(groupedTrees: MfmNode[][]): MfmNode[] {
|
export function mergeGroupedTrees<T>(groupedTrees: T[][]): T[] {
|
||||||
return groupedTrees.reduce((acc, val) => acc.concat(val), ([] as MfmNode[]));
|
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
|
// 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
|
// concatinate text
|
||||||
groupes = groupes.map((group) => {
|
const concatGroupes = groupes.map((group) => {
|
||||||
if (group[0].type == 'text') {
|
if (typeof group[0] == 'string') {
|
||||||
return [
|
return [
|
||||||
createNode('text', {
|
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
|
// 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 {
|
export function stringifyNode(node: MfmNode): string {
|
||||||
switch(node.type) {
|
switch(node.type) {
|
||||||
|
|
Loading…
Reference in a new issue