Newer
Older
import { isMfmBlock, MfmNode, TEXT } from './node';
export function createNode(type: string, props?: Record<string, any>, children?: MfmNode[]): MfmNode {
const node: any = { type };
if (props != null) {
node.props = props;
}
if (children != null) {
node.children = children;
}
export function mergeText(nodes: (MfmNode | string)[]): MfmNode[] {
const dest: MfmNode[] = [];
const storedChars: string[] = [];
/**
* Generate a text node from the stored chars, And push it.
*/
function generateText() {
if (storedChars.length > 0) {
dest.push(TEXT(storedChars.join('')));
for (const node of nodes) {
if (typeof node == 'string') {
// Store the char.
storedChars.push(node);
export function stringifyNode(node: MfmNode): string {
switch(node.type) {
// block
case 'quote': {
return stringifyTree(node.children).split('\n').map(line => `> ${line}`).join('\n');
}
case 'search': {
return node.props.content;
}
case 'blockCode': {
return `\`\`\`${ node.props.lang ?? '' }\n${ node.props.code }\n\`\`\``;
}
case 'mathBlock': {
return `\\[\n${ node.props.formula }\n\\]`;
}
case 'center': {
return `:${ node.props.name }:`;
}
case 'unicodeEmoji': {
return node.props.emoji;
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
}
case 'bold': {
return `**${ stringifyTree(node.children) }**`;
}
case 'small': {
return `<small>${ stringifyTree(node.children) }</small>`;
}
case 'italic': {
return `<i>${ stringifyTree(node.children) }</i>`;
}
case 'strike': {
return `~~${ stringifyTree(node.children) }~~`;
}
case 'inlineCode': {
return `\`${ node.props.code }\``;
}
case 'mathInline': {
return `\\(${ node.props.formula }\\)`;
}
case 'mention': {
return node.props.acct;
}
case 'hashtag': {
return `#${ node.props.hashtag }`;
}
case 'url': {
return node.props.url;
}
case 'link': {
const prefix = node.props.silent ? '?' : '';
return `${ prefix }[${ stringifyTree(node.children) }](${ node.props.url })`;
}
case 'fn': {
const argFields = Object.keys(node.props.args).map(key => {
const value = node.props.args[key];
if (value === true) {
return key;
}
else {
return `${ key }=${ value }`;
}
});
const args = (argFields.length > 0) ? '.' + argFields.join(',') : '';
return `[${ node.props.name }${ args } ${ stringifyTree(node.children) }]`;
}
case 'text': {
return node.props.text;
}
}
throw new Error('unknown mfm node');
}
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
enum stringifyState {
none = 0,
inline,
block
};
export function stringifyTree(nodes: MfmNode[]): string {
let dest: MfmNode[] = [];
let state: stringifyState = stringifyState.none;
for (const node of nodes) {
// 文脈に合わせて改行を追加する。
// none -> inline : No
// none -> block : No
// inline -> inline : No
// inline -> block : Yes
// block -> inline : Yes
// block -> block : Yes
let pushLf: boolean = true;
if (isMfmBlock(node)) {
if (state == stringifyState.none) {
pushLf = false;
}
state = stringifyState.block;
}
else {
if (state == stringifyState.none || state == stringifyState.inline) {
pushLf = false;
}
state = stringifyState.inline;
}
if (pushLf) {
}
dest.push(node);
}
return dest.map(n => stringifyNode(n)).join('');
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
//
// dynamic consuming
//
/*
1. If you want to consume 3 chars, call the setConsumeCount.
```
setConsumeCount(3);
```
2. And the rule to consume the input is as below:
```
rule = (&{ return consumeDynamically(); } .)+
```
*/
let consumeCount = 0;
/**
* set the length of dynamic consuming.
*/
export function setConsumeCount(count: number) {
consumeCount = count;
}
/**
* consume the input and returns matching result.
*/
export function consumeDynamically() {
const matched = (consumeCount > 0);
if (matched) {
consumeCount--;
}
return matched;
}