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

Unused node.props and node.children are not generated.

parent e55c9108
No related branches found
No related tags found
No related merge requests found
...@@ -4,7 +4,7 @@ export type MfmBlock = MfmQuote | MfmSearch | MfmCodeBlock | MfmMathBlock | MfmC ...@@ -4,7 +4,7 @@ export type MfmBlock = MfmQuote | MfmSearch | MfmCodeBlock | MfmMathBlock | MfmC
export type MfmQuote = { export type MfmQuote = {
type: 'quote'; type: 'quote';
props: { }; props?: { };
children: MfmNode[]; children: MfmNode[];
}; };
...@@ -14,7 +14,7 @@ export type MfmSearch = { ...@@ -14,7 +14,7 @@ export type MfmSearch = {
q: string; q: string;
content: string; content: string;
}; };
children: []; children?: [];
}; };
export type MfmCodeBlock = { export type MfmCodeBlock = {
...@@ -23,7 +23,7 @@ export type MfmCodeBlock = { ...@@ -23,7 +23,7 @@ export type MfmCodeBlock = {
code: string; code: string;
lang: string | null; lang: string | null;
}; };
children: []; children?: [];
}; };
export type MfmMathBlock = { export type MfmMathBlock = {
...@@ -31,14 +31,12 @@ export type MfmMathBlock = { ...@@ -31,14 +31,12 @@ export type MfmMathBlock = {
props: { props: {
formula: string; formula: string;
}; };
children: []; children?: [];
}; };
export type MfmCenter = { export type MfmCenter = {
type: 'center'; type: 'center';
props: { props?: { };
};
children: MfmInline[]; children: MfmInline[];
}; };
...@@ -51,30 +49,30 @@ export type MfmEmoji = { ...@@ -51,30 +49,30 @@ export type MfmEmoji = {
emoji?: string; emoji?: string;
name?: string; name?: string;
}; };
children: []; children?: [];
}; };
export type MfmBold = { export type MfmBold = {
type: 'bold'; type: 'bold';
props: { }; props?: { };
children: MfmInline[]; children: MfmInline[];
}; };
export type MfmSmall = { export type MfmSmall = {
type: 'small'; type: 'small';
props: { }; props?: { };
children: MfmInline[]; children: MfmInline[];
}; };
export type MfmItalic = { export type MfmItalic = {
type: 'italic'; type: 'italic';
props: { }; props?: { };
children: MfmInline[]; children: MfmInline[];
}; };
export type MfmStrike = { export type MfmStrike = {
type: 'strike'; type: 'strike';
props: { }; props?: { };
children: MfmInline[]; children: MfmInline[];
}; };
...@@ -83,7 +81,7 @@ export type MfmInlineCode = { ...@@ -83,7 +81,7 @@ export type MfmInlineCode = {
props: { props: {
code: string; code: string;
}; };
children: []; children?: [];
}; };
export type MfmMathInline = { export type MfmMathInline = {
...@@ -91,7 +89,7 @@ export type MfmMathInline = { ...@@ -91,7 +89,7 @@ export type MfmMathInline = {
props: { props: {
formula: string; formula: string;
}; };
children: []; children?: [];
}; };
export type MfmMention = { export type MfmMention = {
...@@ -101,7 +99,7 @@ export type MfmMention = { ...@@ -101,7 +99,7 @@ export type MfmMention = {
host: string | null; host: string | null;
acct: string; acct: string;
}; };
children: []; children?: [];
}; };
export type MfmHashtag = { export type MfmHashtag = {
...@@ -109,7 +107,7 @@ export type MfmHashtag = { ...@@ -109,7 +107,7 @@ export type MfmHashtag = {
props: { props: {
hashtag: string; hashtag: string;
}; };
children: []; children?: [];
}; };
export type MfmUrl = { export type MfmUrl = {
...@@ -117,7 +115,7 @@ export type MfmUrl = { ...@@ -117,7 +115,7 @@ export type MfmUrl = {
props: { props: {
url: string; url: string;
}; };
children: []; children?: [];
}; };
export type MfmLink = { export type MfmLink = {
...@@ -143,5 +141,5 @@ export type MfmText = { ...@@ -143,5 +141,5 @@ export type MfmText = {
props: { props: {
text: string; text: string;
}; };
children: []; children?: [];
}; };
...@@ -65,7 +65,7 @@ quote ...@@ -65,7 +65,7 @@ quote
{ {
const lines = [head, ...tails]; const lines = [head, ...tails];
const children = applyParser(lines.join('\n'), 'fullParser'); const children = applyParser(lines.join('\n'), 'fullParser');
return createNode('quote', { }, children); return createNode('quote', null, children);
} }
quoteLine quoteLine
...@@ -131,7 +131,7 @@ mathBlockLine ...@@ -131,7 +131,7 @@ mathBlockLine
center center
= BEGIN "<center>" content:(!("</center>" END) i:inline { return i; })+ "</center>" END = 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 ...@@ -191,12 +191,12 @@ big
bold bold
= "**" content:(!"**" i:inline { return i; })+ "**" = "**" content:(!"**" i:inline { return i; })+ "**"
{ {
return createNode('bold', { }, mergeText(content)); return createNode('bold', null, mergeText(content));
} }
/ "__" content:$(!"__" c:([a-z0-9]i / _) { return c; })+ "__" / "__" content:$(!"__" c:([a-z0-9]i / _) { return c; })+ "__"
{ {
const parsedContent = applyParser(content, 'inlineParser'); const parsedContent = applyParser(content, 'inlineParser');
return createNode('bold', { }, parsedContent); return createNode('bold', null, parsedContent);
} }
// inline: small // inline: small
...@@ -204,7 +204,7 @@ bold ...@@ -204,7 +204,7 @@ bold
small small
= "<small>" content:(!"</small>" i:inline { return i; })+ "</small>" = "<small>" content:(!"</small>" i:inline { return i; })+ "</small>"
{ {
return createNode('small', { }, mergeText(content)); return createNode('small', null, mergeText(content));
} }
// inline: italic // inline: italic
...@@ -212,17 +212,17 @@ small ...@@ -212,17 +212,17 @@ small
italic italic
= "<i>" content:(!"</i>" i:inline { return i; })+ "</i>" = "<i>" content:(!"</i>" i:inline { return i; })+ "</i>"
{ {
return createNode('italic', { }, mergeText(content)); return createNode('italic', null, mergeText(content));
} }
/ "*" content:$(!"*" ([a-z0-9]i / _))+ "*" / "*" content:$(!"*" ([a-z0-9]i / _))+ "*"
{ {
const parsedContent = applyParser(content, 'inlineParser'); const parsedContent = applyParser(content, 'inlineParser');
return createNode('italic', { }, parsedContent); return createNode('italic', null, parsedContent);
} }
/ "_" content:$(!"_" ([a-z0-9]i / _))+ "_" / "_" content:$(!"_" ([a-z0-9]i / _))+ "_"
{ {
const parsedContent = applyParser(content, 'inlineParser'); const parsedContent = applyParser(content, 'inlineParser');
return createNode('italic', { }, parsedContent); return createNode('italic', null, parsedContent);
} }
// inline: strike // inline: strike
...@@ -230,7 +230,7 @@ italic ...@@ -230,7 +230,7 @@ italic
strike strike
= "~~" content:(!("~" / LF) i:inline { return i; })+ "~~" = "~~" content:(!("~" / LF) i:inline { return i; })+ "~~"
{ {
return createNode('strike', { }, mergeText(content)); return createNode('strike', null, mergeText(content));
} }
// inline: inlineCode // inline: inlineCode
......
import { MfmNode, MfmText } from './node'; import { MfmNode, MfmText } 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 {
props = props ?? {}; const node: any = { type };
children = children ?? []; if (props != null) {
const node = { type, props, children } as MfmNode; node.props = props;
}
if (children != null) {
node.children = children;
}
return node; return node;
} }
...@@ -30,10 +34,14 @@ export function mergeGroupedTrees(groupedTrees: MfmNode[][]): MfmNode[] { ...@@ -30,10 +34,14 @@ export function mergeGroupedTrees(groupedTrees: MfmNode[][]): MfmNode[] {
return groupedTrees.reduce((acc, val) => acc.concat(val), ([] as 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 dest: MfmNode[];
let groupes: MfmNode[][]; let groupes: MfmNode[][];
if (trees == null) {
return trees;
}
// group trees // group trees
groupes = groupContinuous(trees, (prev, current) => prev.type == current.type); groupes = groupContinuous(trees, (prev, current) => prev.type == current.type);
...@@ -52,10 +60,15 @@ export function mergeText(trees: MfmNode[], recursive?: boolean): MfmNode[] { ...@@ -52,10 +60,15 @@ export function mergeText(trees: MfmNode[], recursive?: boolean): MfmNode[] {
// merge groups // merge groups
dest = mergeGroupedTrees(groupes); dest = mergeGroupedTrees(groupes);
return dest.map(tree => { if (recursive) {
// apply recursively to children return dest.map(tree => {
return createNode(tree.type, tree.props, recursive ? mergeText(tree.children) : tree.children); // apply recursively to children
}); return createNode(tree.type, tree.props, recursive ? mergeText(tree.children) : tree.children);
});
}
else {
return dest;
}
} }
// //
......
...@@ -4,23 +4,23 @@ import { ...@@ -4,23 +4,23 @@ import {
MfmNode, MfmQuote, MfmSearch, MfmSmall, MfmStrike, MfmText, MfmUrl MfmNode, MfmQuote, MfmSearch, MfmSmall, MfmStrike, MfmText, MfmUrl
} from '../built'; } from '../built';
export const QUOTE = (children: MfmNode[]): MfmQuote => { return { type:'quote', 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 }, 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 }, children: [] }; }; 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 }, children: [] }; }; export const MATH_BLOCK = (formula: string): MfmMathBlock => { return { type:'mathBlock', props: { formula } }; };
export const CENTER = (children: MfmInline[]): MfmCenter => { return { type:'center', props: { }, children }; }; export const CENTER = (children: MfmInline[]): MfmCenter => { return { type:'center', children }; };
export const BOLD = (children: MfmInline[]): MfmBold => { return { type:'bold', props: { }, children }; }; export const BOLD = (children: MfmInline[]): MfmBold => { return { type:'bold', children }; };
export const SMALL = (children: MfmInline[]): MfmSmall => { return { type:'small', props: { }, children }; }; export const SMALL = (children: MfmInline[]): MfmSmall => { return { type:'small', children }; };
export const ITALIC = (children: MfmInline[]): MfmItalic => { return { type:'italic', props: { }, children }; }; export const ITALIC = (children: MfmInline[]): MfmItalic => { return { type:'italic', children }; };
export const STRIKE = (children: MfmInline[]): MfmStrike => { return { type:'strike', props: { }, children }; }; export const STRIKE = (children: MfmInline[]): MfmStrike => { return { type:'strike', children }; };
export const INLINE_CODE = (code: string): MfmInlineCode => { return { type:'inlineCode', props: { code }, 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 }, children: [] }; }; 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 }, children: [] }; }; 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 }, children: [] }; }; 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 }, children: [] }; }; 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 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 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 UNI_EMOJI = (value: string): MfmEmoji => { return { type:'emoji', props: { emoji: value } }; };
export const TEXT = (value: string): MfmText => { return { type:'text', props: { text: value }, children: [] }; }; export const TEXT = (value: string): MfmText => { return { type:'text', props: { text: value } }; };
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