export node generation macros.

This commit is contained in:
marihachi 2021-04-10 18:09:44 +09:00
parent 33c0b6c960
commit cbb00a5a66
5 changed files with 56 additions and 54 deletions

View file

@ -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';

View file

@ -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 MfmNode['type']> =
T extends 'quote' ? MfmQuote :

View file

@ -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', () => {

View file

@ -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 } }; };

View file

@ -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);
});