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

add type definition of MFM nodes.

parent 1c396215
No related branches found
No related tags found
No related merge requests found
......@@ -23,9 +23,8 @@ async function entryPoint() {
.replace(/\\t/g, '\t')
.replace(/\\u00a0/g, '\u00a0');
let result: any;
try {
result = parse(input);
const result = parse(input);
console.log(JSON.stringify(result));
}
catch (err) {
......
import peg from 'pegjs';
import { MfmNode } from './util';
import { MfmNode } from './node';
const parser: peg.Parser = require('./parser');
export function parse(input: string): MfmNode[] {
......@@ -11,3 +11,31 @@ export function parsePlain(input: string): MfmNode[] {
const nodes = parser.parse(input, { startRule: 'plainParser' });
return nodes;
}
export {
MfmNode,
MfmBlock,
MfmInline,
// block
MfmQuote,
MfmSearch,
MfmCodeBlock,
MfmMathBlock,
MfmCenter,
// inline
MfmEmoji,
MfmBold,
MfmSmall,
MfmItalic,
MfmStrike,
MfmInlineCode,
MfmMathInline,
MfmMention,
MfmHashtag,
MfmUrl,
MfmLink,
MfmFn,
MfmText
} from './node';
export type MfmNode = MfmBlock | MfmInline;
export type MfmBlock = MfmQuote | MfmSearch | MfmCodeBlock | MfmMathBlock | MfmCenter;
export type MfmQuote = {
type: 'quote';
props: { };
children: MfmNode[];
};
export type MfmSearch = {
type: 'search';
props: {
q: string;
content: string;
};
children: [];
};
export type MfmCodeBlock = {
type: 'blockCode';
props: {
code: string;
lang: string | null;
};
children: [];
};
export type MfmMathBlock = {
type: 'mathBlock';
props: {
formula: string;
};
children: [];
};
export type MfmCenter = {
type: 'center';
props: {
};
children: MfmNode[];
};
export type MfmInline = MfmEmoji | MfmBold | MfmSmall | MfmItalic | MfmStrike | MfmInlineCode |
MfmMathInline | MfmMention | MfmHashtag | MfmUrl | MfmLink | MfmFn | MfmText;
export type MfmEmoji = {
type: 'emoji';
props: { emoji: string; name: undefined; } | { emoji: undefined; name: string; };
children: [];
};
export type MfmBold = {
type: 'bold';
props: { };
children: MfmInline[];
};
export type MfmSmall = {
type: 'small';
props: { };
children: MfmInline[];
};
export type MfmItalic = {
type: 'italic';
props: { };
children: MfmInline[];
};
export type MfmStrike = {
type: 'strike';
props: { };
children: MfmInline[];
};
export type MfmInlineCode = {
type: 'inlineCode';
props: {
code: string;
};
children: [];
};
export type MfmMathInline = {
type: 'mathInline';
props: {
formula: string;
};
children: [];
};
export type MfmMention = {
type: 'mention';
props: {
username: string;
host: string | null;
acct: string;
};
children: [];
};
export type MfmHashtag = {
type: 'hashtag';
props: {
hashtag: string;
};
children: [];
};
export type MfmUrl = {
type: 'url';
props: {
url: string;
};
children: [];
};
export type MfmLink = {
type: 'link';
props: {
silent: boolean;
url: string;
};
children: MfmInline[];
};
export type MfmFn = {
type: 'fn';
props: {
name: string;
args: Record<string, string | true>;
};
children: MfmInline[];
};
export type MfmText = {
type: 'text';
props: {
text: string;
};
children: [];
};
export type MfmNode = {
type: string;
props: Record<string, any>;
children: MfmNode[];
};
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 };
const node = { type, props, children } as MfmNode;
return node;
}
......@@ -46,7 +42,7 @@ export function mergeText(trees: MfmNode[], recursive?: boolean): MfmNode[] {
if (group[0].type == 'text') {
return [
createNode('text', {
text: group.map(i => i.props.text).join('')
text: group.map(i => (i as MfmText).props.text).join('')
})
];
}
......
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