Skip to content
Snippets Groups Projects
Commit 7544188d authored by marihachi's avatar marihachi
Browse files

Merge branch 'work-28' into develop

parents 9099f54b 3f584285
No related branches found
No related tags found
No related merge requests found
......@@ -12,6 +12,98 @@ export function parsePlain(input: string): MfmNode[] {
return nodes;
}
function nodeStringify(node: MfmNode): string {
switch(node.type) {
// block
case 'quote': {
return toString(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 `<center>${ toString(node.children) }</center>`;
}
// inline
case 'emoji': {
if (node.props.name) {
return `:${ node.props.name }:`;
}
else if (node.props.emoji) {
return node.props.emoji;
}
else {
return '';
}
}
case 'bold': {
return `**${ toString(node.children) }**`;
}
case 'small': {
return `<small>${ toString(node.children) }</small>`;
}
case 'italic': {
return `<i>${ toString(node.children) }</i>`;
}
case 'strike': {
return `~~${ toString(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 }[${ toString(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.join(',');
return `[${ node.props.name }${ args } ${ toString(node.children) }]`;
}
case 'text': {
return node.props.text;
}
}
throw new Error('unknown mfm node');
}
export function toString(nodes: MfmNode[]): string
export function toString(node: MfmNode): string
export function toString(node: MfmNode | MfmNode[]): string {
if (Array.isArray(node)) {
return node.map(n => nodeStringify(n)).join('');
}
else {
return nodeStringify(node);
}
}
export {
MfmNode,
MfmBlock,
......
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