From cbb00a5a66d5059c1a781d8759ebb2642685f7dc Mon Sep 17 00:00:00 2001 From: marihachi Date: Sat, 10 Apr 2021 18:09:44 +0900 Subject: [PATCH] export node generation macros. --- src/index.ts | 32 +++++++++++++++++++++++++++++--- src/node.ts | 19 +++++++++++++++++++ test/api.ts | 2 +- test/node.ts | 24 ------------------------ test/parser.ts | 33 +++++++-------------------------- 5 files changed, 56 insertions(+), 54 deletions(-) delete mode 100644 test/node.ts diff --git a/src/index.ts b/src/index.ts index 292d5ac..030f451 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,13 +6,14 @@ export { extract } from './api'; -export { NodeType } from './node'; - export { + NodeType, MfmNode, MfmBlock, - MfmInline, + MfmInline +} from './node'; +export { // block MfmQuote, MfmSearch, @@ -36,3 +37,28 @@ export { MfmFn, MfmText } from './node'; + +export { + // block + QUOTE, + SEARCH, + CODE_BLOCK, + MATH_BLOCK, + CENTER, + + // inline + UNI_EMOJI, + EMOJI_CODE, + BOLD, + SMALL, + ITALIC, + STRIKE, + INLINE_CODE, + MATH_INLINE, + MENTION, + HASHTAG, + N_URL, + LINK, + FN, + TEXT +} from './node'; diff --git a/src/node.ts b/src/node.ts index bb7e2b3..ac1e29f 100644 --- a/src/node.ts +++ b/src/node.ts @@ -14,6 +14,7 @@ export type MfmQuote = { props?: { }; children: MfmNode[]; }; +export const QUOTE = (children: MfmNode[]): NodeType<'quote'> => { return { type:'quote', children }; }; export type MfmSearch = { type: 'search'; @@ -23,6 +24,7 @@ export type MfmSearch = { }; children?: []; }; +export const SEARCH = (query: string, content: string): NodeType<'search'> => { return { type:'search', props: { query, content } }; }; export type MfmCodeBlock = { type: 'blockCode'; @@ -32,6 +34,7 @@ export type MfmCodeBlock = { }; children?: []; }; +export const CODE_BLOCK = (code: string, lang: string | null): NodeType<'blockCode'> => { return { type:'blockCode', props: { code, lang } }; }; export type MfmMathBlock = { type: 'mathBlock'; @@ -40,12 +43,14 @@ export type MfmMathBlock = { }; children?: []; }; +export const MATH_BLOCK = (formula: string): NodeType<'mathBlock'> => { return { type:'mathBlock', props: { formula } }; }; export type MfmCenter = { type: 'center'; props?: { }; children: MfmInline[]; }; +export const CENTER = (children: MfmInline[]): NodeType<'center'> => { return { type:'center', children }; }; export type MfmInline = MfmUnicodeEmoji | MfmEmojiCode | MfmBold | MfmSmall | MfmItalic | MfmStrike | MfmInlineCode | MfmMathInline | MfmMention | MfmHashtag | MfmUrl | MfmLink | MfmFn | MfmText; @@ -57,6 +62,7 @@ export type MfmUnicodeEmoji = { }; children?: []; }; +export const UNI_EMOJI = (value: string): NodeType<'unicodeEmoji'> => { return { type:'unicodeEmoji', props: { emoji: value } }; }; export type MfmEmojiCode = { type: 'emojiCode'; @@ -65,30 +71,35 @@ export type MfmEmojiCode = { }; children?: []; }; +export const EMOJI_CODE = (name: string): NodeType<'emojiCode'> => { return { type:'emojiCode', props: { name: name } }; }; export type MfmBold = { type: 'bold'; props?: { }; children: MfmInline[]; }; +export const BOLD = (children: MfmInline[]): NodeType<'bold'> => { return { type:'bold', children }; }; export type MfmSmall = { type: 'small'; props?: { }; children: MfmInline[]; }; +export const SMALL = (children: MfmInline[]): NodeType<'small'> => { return { type:'small', children }; }; export type MfmItalic = { type: 'italic'; props?: { }; children: MfmInline[]; }; +export const ITALIC = (children: MfmInline[]): NodeType<'italic'> => { return { type:'italic', children }; }; export type MfmStrike = { type: 'strike'; props?: { }; children: MfmInline[]; }; +export const STRIKE = (children: MfmInline[]): NodeType<'strike'> => { return { type:'strike', children }; }; export type MfmInlineCode = { type: 'inlineCode'; @@ -97,6 +108,7 @@ export type MfmInlineCode = { }; children?: []; }; +export const INLINE_CODE = (code: string): NodeType<'inlineCode'> => { return { type:'inlineCode', props: { code } }; }; export type MfmMathInline = { type: 'mathInline'; @@ -105,6 +117,7 @@ export type MfmMathInline = { }; children?: []; }; +export const MATH_INLINE = (formula: string): NodeType<'mathInline'> => { return { type:'mathInline', props: { formula } }; }; export type MfmMention = { type: 'mention'; @@ -115,6 +128,7 @@ export type MfmMention = { }; children?: []; }; +export const MENTION = (username: string, host: string | null, acct: string): NodeType<'mention'> => { return { type:'mention', props: { username, host, acct } }; }; export type MfmHashtag = { type: 'hashtag'; @@ -123,6 +137,7 @@ export type MfmHashtag = { }; children?: []; }; +export const HASHTAG = (value: string): NodeType<'hashtag'> => { return { type:'hashtag', props: { hashtag: value } }; }; export type MfmUrl = { type: 'url'; @@ -131,6 +146,7 @@ export type MfmUrl = { }; children?: []; }; +export const N_URL = (value: string): NodeType<'url'> => { return { type:'url', props: { url: value } }; }; export type MfmLink = { type: 'link'; @@ -140,6 +156,7 @@ export type MfmLink = { }; children: MfmInline[]; }; +export const LINK = (silent: boolean, url: string, children: MfmInline[]): NodeType<'link'> => { return { type:'link', props: { silent, url }, children }; }; export type MfmFn = { type: 'fn'; @@ -149,6 +166,7 @@ export type MfmFn = { }; children: MfmInline[]; }; +export const FN = (name: string, args: MfmFn['props']['args'], children: MfmFn['children']): NodeType<'fn'> => { return { type:'fn', props: { name, args }, children }; }; export type MfmText = { type: 'text'; @@ -157,6 +175,7 @@ export type MfmText = { }; children?: []; }; +export const TEXT = (value: string): NodeType<'text'> => { return { type:'text', props: { text: value } }; }; export type NodeType = T extends 'quote' ? MfmQuote : diff --git a/test/api.ts b/test/api.ts index 5e5343d..ed5ccee 100644 --- a/test/api.ts +++ b/test/api.ts @@ -2,7 +2,7 @@ import assert from 'assert'; import * as mfm from '../built/index'; import { TEXT, CENTER, FN, UNI_EMOJI, MENTION, EMOJI_CODE, HASHTAG, N_URL, BOLD, SMALL, ITALIC, STRIKE, QUOTE, MATH_BLOCK, SEARCH, CODE_BLOCK, LINK -} from './node'; +} from '../built/index'; describe('API', () => { describe('toString', () => { diff --git a/test/node.ts b/test/node.ts deleted file mode 100644 index b3e4a07..0000000 --- a/test/node.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { - MfmFn, MfmInline, MfmNode, NodeType -} from '../built'; - -export const QUOTE = (children: MfmNode[]): NodeType<'quote'> => { return { type:'quote', children }; }; -export const SEARCH = (query: string, content: string): NodeType<'search'> => { return { type:'search', props: { query, content } }; }; -export const CODE_BLOCK = (code: string, lang: string | null): NodeType<'blockCode'> => { return { type:'blockCode', props: { code, lang } }; }; -export const MATH_BLOCK = (formula: string): NodeType<'mathBlock'> => { return { type:'mathBlock', props: { formula } }; }; -export const CENTER = (children: MfmInline[]): NodeType<'center'> => { return { type:'center', children }; }; - -export const BOLD = (children: MfmInline[]): NodeType<'bold'> => { return { type:'bold', children }; }; -export const SMALL = (children: MfmInline[]): NodeType<'small'> => { return { type:'small', children }; }; -export const ITALIC = (children: MfmInline[]): NodeType<'italic'> => { return { type:'italic', children }; }; -export const STRIKE = (children: MfmInline[]): NodeType<'strike'> => { return { type:'strike', children }; }; -export const INLINE_CODE = (code: string): NodeType<'inlineCode'> => { return { type:'inlineCode', props: { code } }; }; -export const MATH_INLINE = (formula: string): NodeType<'mathInline'> => { return { type:'mathInline', props: { formula } }; }; -export const MENTION = (username: string, host: string | null, acct: string): NodeType<'mention'> => { return { type:'mention', props: { username, host, acct } }; }; -export const HASHTAG = (value: string): NodeType<'hashtag'> => { return { type:'hashtag', props: { hashtag: value } }; }; -export const N_URL = (value: string): NodeType<'url'> => { return { type:'url', props: { url: value } }; }; -export const LINK = (silent: boolean, url: string, children: MfmInline[]): NodeType<'link'> => { return { type:'link', props: { silent, url }, children }; }; -export const EMOJI_CODE = (name: string): NodeType<'emojiCode'> => { return { type:'emojiCode', props: { name: name } }; }; -export const FN = (name: string, args: MfmFn['props']['args'], children: MfmFn['children']): NodeType<'fn'> => { return { type:'fn', props: { name, args }, children }; }; -export const UNI_EMOJI = (value: string): NodeType<'unicodeEmoji'> => { return { type:'unicodeEmoji', props: { emoji: value } }; }; -export const TEXT = (value: string): NodeType<'text'> => { return { type:'text', props: { text: value } }; }; diff --git a/test/parser.ts b/test/parser.ts index 9bfc684..c889ec5 100644 --- a/test/parser.ts +++ b/test/parser.ts @@ -1,9 +1,8 @@ import assert from 'assert'; import * as mfm from '../built/index'; -import { createNode } from '../built/util'; import { TEXT, CENTER, FN, UNI_EMOJI, MENTION, EMOJI_CODE, HASHTAG, N_URL, BOLD, SMALL, ITALIC, STRIKE, QUOTE, MATH_BLOCK, SEARCH, CODE_BLOCK, LINK -} from './node'; +} from '../built/index'; describe('parser', () => { describe('text', () => { @@ -75,60 +74,42 @@ describe('parser', () => { it('Search', () => { const input = 'MFM 書き方 123 Search'; const output = [ - createNode('search', { - query: 'MFM 書き方 123', - content: input - }) + SEARCH('MFM 書き方 123', input) ]; assert.deepStrictEqual(mfm.parse(input), output); }); it('[Search]', () => { const input = 'MFM 書き方 123 [Search]'; const output = [ - createNode('search', { - query: 'MFM 書き方 123', - content: input - }) + SEARCH('MFM 書き方 123', input) ]; assert.deepStrictEqual(mfm.parse(input), output); }); it('search', () => { const input = 'MFM 書き方 123 search'; const output = [ - createNode('search', { - query: 'MFM 書き方 123', - content: input - }) + SEARCH('MFM 書き方 123', input) ]; assert.deepStrictEqual(mfm.parse(input), output); }); it('[search]', () => { const input = 'MFM 書き方 123 [search]'; const output = [ - createNode('search', { - query: 'MFM 書き方 123', - content: input - }) + SEARCH('MFM 書き方 123', input) ]; assert.deepStrictEqual(mfm.parse(input), output); }); it('検索', () => { const input = 'MFM 書き方 123 検索'; const output = [ - createNode('search', { - query: 'MFM 書き方 123', - content: input - }) + SEARCH('MFM 書き方 123', input) ]; assert.deepStrictEqual(mfm.parse(input), output); }); it('[検索]', () => { const input = 'MFM 書き方 123 [検索]'; const output = [ - createNode('search', { - query: 'MFM 書き方 123', - content: input - }) + SEARCH('MFM 書き方 123', input) ]; assert.deepStrictEqual(mfm.parse(input), output); });