mirror of
https://activitypub.software/TransFem-org/sfm-js
synced 2024-11-24 23:15:14 +00:00
Unused node.props and node.children are not generated.
This commit is contained in:
parent
e55c910896
commit
c9eb5bfc62
4 changed files with 64 additions and 53 deletions
34
src/node.ts
34
src/node.ts
|
@ -4,7 +4,7 @@ export type MfmBlock = MfmQuote | MfmSearch | MfmCodeBlock | MfmMathBlock | MfmC
|
|||
|
||||
export type MfmQuote = {
|
||||
type: 'quote';
|
||||
props: { };
|
||||
props?: { };
|
||||
children: MfmNode[];
|
||||
};
|
||||
|
||||
|
@ -14,7 +14,7 @@ export type MfmSearch = {
|
|||
q: string;
|
||||
content: string;
|
||||
};
|
||||
children: [];
|
||||
children?: [];
|
||||
};
|
||||
|
||||
export type MfmCodeBlock = {
|
||||
|
@ -23,7 +23,7 @@ export type MfmCodeBlock = {
|
|||
code: string;
|
||||
lang: string | null;
|
||||
};
|
||||
children: [];
|
||||
children?: [];
|
||||
};
|
||||
|
||||
export type MfmMathBlock = {
|
||||
|
@ -31,14 +31,12 @@ export type MfmMathBlock = {
|
|||
props: {
|
||||
formula: string;
|
||||
};
|
||||
children: [];
|
||||
children?: [];
|
||||
};
|
||||
|
||||
export type MfmCenter = {
|
||||
type: 'center';
|
||||
props: {
|
||||
|
||||
};
|
||||
props?: { };
|
||||
children: MfmInline[];
|
||||
};
|
||||
|
||||
|
@ -51,30 +49,30 @@ export type MfmEmoji = {
|
|||
emoji?: string;
|
||||
name?: string;
|
||||
};
|
||||
children: [];
|
||||
children?: [];
|
||||
};
|
||||
|
||||
export type MfmBold = {
|
||||
type: 'bold';
|
||||
props: { };
|
||||
props?: { };
|
||||
children: MfmInline[];
|
||||
};
|
||||
|
||||
export type MfmSmall = {
|
||||
type: 'small';
|
||||
props: { };
|
||||
props?: { };
|
||||
children: MfmInline[];
|
||||
};
|
||||
|
||||
export type MfmItalic = {
|
||||
type: 'italic';
|
||||
props: { };
|
||||
props?: { };
|
||||
children: MfmInline[];
|
||||
};
|
||||
|
||||
export type MfmStrike = {
|
||||
type: 'strike';
|
||||
props: { };
|
||||
props?: { };
|
||||
children: MfmInline[];
|
||||
};
|
||||
|
||||
|
@ -83,7 +81,7 @@ export type MfmInlineCode = {
|
|||
props: {
|
||||
code: string;
|
||||
};
|
||||
children: [];
|
||||
children?: [];
|
||||
};
|
||||
|
||||
export type MfmMathInline = {
|
||||
|
@ -91,7 +89,7 @@ export type MfmMathInline = {
|
|||
props: {
|
||||
formula: string;
|
||||
};
|
||||
children: [];
|
||||
children?: [];
|
||||
};
|
||||
|
||||
export type MfmMention = {
|
||||
|
@ -101,7 +99,7 @@ export type MfmMention = {
|
|||
host: string | null;
|
||||
acct: string;
|
||||
};
|
||||
children: [];
|
||||
children?: [];
|
||||
};
|
||||
|
||||
export type MfmHashtag = {
|
||||
|
@ -109,7 +107,7 @@ export type MfmHashtag = {
|
|||
props: {
|
||||
hashtag: string;
|
||||
};
|
||||
children: [];
|
||||
children?: [];
|
||||
};
|
||||
|
||||
export type MfmUrl = {
|
||||
|
@ -117,7 +115,7 @@ export type MfmUrl = {
|
|||
props: {
|
||||
url: string;
|
||||
};
|
||||
children: [];
|
||||
children?: [];
|
||||
};
|
||||
|
||||
export type MfmLink = {
|
||||
|
@ -143,5 +141,5 @@ export type MfmText = {
|
|||
props: {
|
||||
text: string;
|
||||
};
|
||||
children: [];
|
||||
children?: [];
|
||||
};
|
||||
|
|
|
@ -65,7 +65,7 @@ quote
|
|||
{
|
||||
const lines = [head, ...tails];
|
||||
const children = applyParser(lines.join('\n'), 'fullParser');
|
||||
return createNode('quote', { }, children);
|
||||
return createNode('quote', null, children);
|
||||
}
|
||||
|
||||
quoteLine
|
||||
|
@ -131,7 +131,7 @@ mathBlockLine
|
|||
center
|
||||
= BEGIN "<center>" content:(!("</center>" END) i:inline { return i; })+ "</center>" END
|
||||
{
|
||||
return createNode('center', { }, mergeText(content));
|
||||
return createNode('center', null, mergeText(content));
|
||||
}
|
||||
|
||||
//
|
||||
|
@ -191,12 +191,12 @@ big
|
|||
bold
|
||||
= "**" content:(!"**" i:inline { return i; })+ "**"
|
||||
{
|
||||
return createNode('bold', { }, mergeText(content));
|
||||
return createNode('bold', null, mergeText(content));
|
||||
}
|
||||
/ "__" content:$(!"__" c:([a-z0-9]i / _) { return c; })+ "__"
|
||||
{
|
||||
const parsedContent = applyParser(content, 'inlineParser');
|
||||
return createNode('bold', { }, parsedContent);
|
||||
return createNode('bold', null, parsedContent);
|
||||
}
|
||||
|
||||
// inline: small
|
||||
|
@ -204,7 +204,7 @@ bold
|
|||
small
|
||||
= "<small>" content:(!"</small>" i:inline { return i; })+ "</small>"
|
||||
{
|
||||
return createNode('small', { }, mergeText(content));
|
||||
return createNode('small', null, mergeText(content));
|
||||
}
|
||||
|
||||
// inline: italic
|
||||
|
@ -212,17 +212,17 @@ small
|
|||
italic
|
||||
= "<i>" content:(!"</i>" i:inline { return i; })+ "</i>"
|
||||
{
|
||||
return createNode('italic', { }, mergeText(content));
|
||||
return createNode('italic', null, mergeText(content));
|
||||
}
|
||||
/ "*" content:$(!"*" ([a-z0-9]i / _))+ "*"
|
||||
{
|
||||
const parsedContent = applyParser(content, 'inlineParser');
|
||||
return createNode('italic', { }, parsedContent);
|
||||
return createNode('italic', null, parsedContent);
|
||||
}
|
||||
/ "_" content:$(!"_" ([a-z0-9]i / _))+ "_"
|
||||
{
|
||||
const parsedContent = applyParser(content, 'inlineParser');
|
||||
return createNode('italic', { }, parsedContent);
|
||||
return createNode('italic', null, parsedContent);
|
||||
}
|
||||
|
||||
// inline: strike
|
||||
|
@ -230,7 +230,7 @@ italic
|
|||
strike
|
||||
= "~~" content:(!("~" / LF) i:inline { return i; })+ "~~"
|
||||
{
|
||||
return createNode('strike', { }, mergeText(content));
|
||||
return createNode('strike', null, mergeText(content));
|
||||
}
|
||||
|
||||
// inline: inlineCode
|
||||
|
|
23
src/util.ts
23
src/util.ts
|
@ -1,9 +1,13 @@
|
|||
import { MfmNode, MfmText } from './node';
|
||||
|
||||
export function createNode(type: string, props: Record<string, any>, children?: MfmNode[]): MfmNode {
|
||||
props = props ?? {};
|
||||
children = children ?? [];
|
||||
const node = { type, props, children } as MfmNode;
|
||||
export function createNode(type: string, props?: Record<string, any>, children?: MfmNode[]): MfmNode {
|
||||
const node: any = { type };
|
||||
if (props != null) {
|
||||
node.props = props;
|
||||
}
|
||||
if (children != null) {
|
||||
node.children = children;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
|
@ -30,10 +34,14 @@ export function mergeGroupedTrees(groupedTrees: MfmNode[][]): MfmNode[] {
|
|||
return groupedTrees.reduce((acc, val) => acc.concat(val), ([] as MfmNode[]));
|
||||
}
|
||||
|
||||
export function mergeText(trees: MfmNode[], recursive?: boolean): 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);
|
||||
|
||||
|
@ -52,11 +60,16 @@ export function mergeText(trees: MfmNode[], recursive?: boolean): MfmNode[] {
|
|||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// dynamic consuming
|
||||
|
|
34
test/node.ts
34
test/node.ts
|
@ -4,23 +4,23 @@ import {
|
|||
MfmNode, MfmQuote, MfmSearch, MfmSmall, MfmStrike, MfmText, MfmUrl
|
||||
} from '../built';
|
||||
|
||||
export const QUOTE = (children: MfmNode[]): MfmQuote => { return { type:'quote', props: { }, children }; };
|
||||
export const SEARCH = (q: string, content: string): MfmSearch => { return { type:'search', props: { q, content }, children: [] }; };
|
||||
export const CODE_BLOCK = (code: string, lang: string | null): MfmCodeBlock => { return { type:'blockCode', props: { code, lang }, children: [] }; };
|
||||
export const MATH_BLOCK = (formula: string): MfmMathBlock => { return { type:'mathBlock', props: { formula }, children: [] }; };
|
||||
export const CENTER = (children: MfmInline[]): MfmCenter => { return { type:'center', props: { }, children }; };
|
||||
export const QUOTE = (children: MfmNode[]): MfmQuote => { return { type:'quote', children }; };
|
||||
export const SEARCH = (q: string, content: string): MfmSearch => { return { type:'search', props: { q, content } }; };
|
||||
export const CODE_BLOCK = (code: string, lang: string | null): MfmCodeBlock => { return { type:'blockCode', props: { code, lang } }; };
|
||||
export const MATH_BLOCK = (formula: string): MfmMathBlock => { return { type:'mathBlock', props: { formula } }; };
|
||||
export const CENTER = (children: MfmInline[]): MfmCenter => { return { type:'center', children }; };
|
||||
|
||||
export const BOLD = (children: MfmInline[]): MfmBold => { return { type:'bold', props: { }, children }; };
|
||||
export const SMALL = (children: MfmInline[]): MfmSmall => { return { type:'small', props: { }, children }; };
|
||||
export const ITALIC = (children: MfmInline[]): MfmItalic => { return { type:'italic', props: { }, children }; };
|
||||
export const STRIKE = (children: MfmInline[]): MfmStrike => { return { type:'strike', props: { }, children }; };
|
||||
export const INLINE_CODE = (code: string): MfmInlineCode => { return { type:'inlineCode', props: { code }, children: [] }; };
|
||||
export const MATH_INLINE = (formula: string): MfmMathInline => { return { type:'mathInline', props: { formula }, children: [] }; };
|
||||
export const MENTION = (username: string, host: string | null, acct: string): MfmMention => { return { type:'mention', props: { username, host, acct }, children: [] }; };
|
||||
export const HASHTAG = (value: string): MfmHashtag => { return { type:'hashtag', props: { hashtag: value }, children: [] }; };
|
||||
export const N_URL = (value: string): MfmUrl => { return { type:'url', props: { url: value }, children: [] }; };
|
||||
export const BOLD = (children: MfmInline[]): MfmBold => { return { type:'bold', children }; };
|
||||
export const SMALL = (children: MfmInline[]): MfmSmall => { return { type:'small', children }; };
|
||||
export const ITALIC = (children: MfmInline[]): MfmItalic => { return { type:'italic', children }; };
|
||||
export const STRIKE = (children: MfmInline[]): MfmStrike => { return { type:'strike', children }; };
|
||||
export const INLINE_CODE = (code: string): MfmInlineCode => { return { type:'inlineCode', props: { code } }; };
|
||||
export const MATH_INLINE = (formula: string): MfmMathInline => { return { type:'mathInline', props: { formula } }; };
|
||||
export const MENTION = (username: string, host: string | null, acct: string): MfmMention => { return { type:'mention', props: { username, host, acct } }; };
|
||||
export const HASHTAG = (value: string): MfmHashtag => { return { type:'hashtag', props: { hashtag: value } }; };
|
||||
export const N_URL = (value: string): MfmUrl => { return { type:'url', props: { url: value } }; };
|
||||
export const LINK = (silent: boolean, url: string, children: MfmInline[]): MfmLink => { return { type:'link', props: { silent, url }, children }; };
|
||||
export const CUSTOM_EMOJI = (name: string): MfmEmoji => { return { type:'emoji', props: { name: name }, children: [] }; };
|
||||
export const CUSTOM_EMOJI = (name: string): MfmEmoji => { return { type:'emoji', props: { name: name } }; };
|
||||
export const FN = (name: string, args: MfmFn['props']['args'], children: MfmFn['children']): MfmFn => { return { type:'fn', props: { name, args }, children }; };
|
||||
export const UNI_EMOJI = (value: string): MfmEmoji => { return { type:'emoji', props: { emoji: value }, children: [] }; };
|
||||
export const TEXT = (value: string): MfmText => { return { type:'text', props: { text: value }, children: [] }; };
|
||||
export const UNI_EMOJI = (value: string): MfmEmoji => { return { type:'emoji', props: { emoji: value } }; };
|
||||
export const TEXT = (value: string): MfmText => { return { type:'text', props: { text: value } }; };
|
||||
|
|
Loading…
Reference in a new issue