From 0182d2cac4e46e731b1d55692ba327df8db12b3d Mon Sep 17 00:00:00 2001
From: marihachi <marihachi0620@gmail.com>
Date: Sat, 20 Mar 2021 20:04:09 +0900
Subject: [PATCH] add type definition of MFM nodes.

---
 src/cli/parse.ts |   3 +-
 src/index.ts     |  30 +++++++++-
 src/node.ts      | 144 +++++++++++++++++++++++++++++++++++++++++++++++
 src/util.ts      |  10 +---
 4 files changed, 177 insertions(+), 10 deletions(-)
 create mode 100644 src/node.ts

diff --git a/src/cli/parse.ts b/src/cli/parse.ts
index 8682c71..a2550c5 100644
--- a/src/cli/parse.ts
+++ b/src/cli/parse.ts
@@ -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) {
diff --git a/src/index.ts b/src/index.ts
index df0424e..9ba9e47 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,5 +1,5 @@
 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';
diff --git a/src/node.ts b/src/node.ts
new file mode 100644
index 0000000..888cf35
--- /dev/null
+++ b/src/node.ts
@@ -0,0 +1,144 @@
+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: [];
+};
diff --git a/src/util.ts b/src/util.ts
index f61a6e4..83b2dc6 100644
--- a/src/util.ts
+++ b/src/util.ts
@@ -1,13 +1,9 @@
-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('')
 				})
 			];
 		}
-- 
GitLab