diff --git a/src/index.ts b/src/index.ts
index 9ba9e47..1a561ee 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -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 `
${ toString(node.children) }`;
+ }
+ // 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 `${ toString(node.children) }`;
+ }
+ case 'italic': {
+ return `${ toString(node.children) }`;
+ }
+ 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,