From 1a6f927b835670fea2df85d5fd1680eb036742ce Mon Sep 17 00:00:00 2001 From: marihachi Date: Wed, 24 Mar 2021 01:03:57 +0900 Subject: [PATCH 1/3] wip --- src/index.ts | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/src/index.ts b/src/index.ts index 9ba9e47..811dc1c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,6 +12,72 @@ 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': { + break; + } + case 'blockCode': { + break; + } + case 'mathBlock': { + break; + } + case 'center': { + break; + } + // inline + case 'emoji': { + break; + } + case 'bold': { + break; + } + case 'small': { + break; + } + case 'italic': { + break; + } + case 'strike': { + break; + } + case 'inlineCode': { + break; + } + case 'mathInline': { + break; + } + case 'mention': { + break; + } + case 'hashtag': { + break; + } + case 'url': { + break; + } + case 'link': { + break; + } + case 'fn': { + break; + } + case 'text': { + return node.props.text; + } + } + return ''; +} + +export function toString(nodes: MfmNode[]): string { + return nodes.map(node => nodeStringify(node)).join(''); +} + export { MfmNode, MfmBlock, From 5c4c242ee24ee6c01c98324c2e48e3b4e9ea0294 Mon Sep 17 00:00:00 2001 From: marihachi Date: Fri, 26 Mar 2021 10:02:24 +0900 Subject: [PATCH 2/3] toString --- src/index.ts | 53 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/src/index.ts b/src/index.ts index 811dc1c..b13370b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,59 +19,78 @@ function nodeStringify(node: MfmNode): string { return toString(node.children).split('\n').map(line => `>${line}`).join('\n'); } case 'search': { - break; + return node.props.content; } case 'blockCode': { - break; + return `\`\`\`${ node.props.lang ?? '' }\n${ node.props.code }\n\`\`\``; } case 'mathBlock': { - break; + return `\\[\n${ node.props.formula }\n\\]`; } case 'center': { - break; + return `
${ toString(node.children) }
`; } // inline case 'emoji': { - break; + if (node.props.name) { + return `:${ node.props.name }:`; + } + else if (node.props.emoji) { + return node.props.emoji; + } + else { + return ''; + } } case 'bold': { - break; + return `**${ toString(node.children) }**`; } case 'small': { - break; + return `${ toString(node.children) }`; } case 'italic': { - break; + return `${ toString(node.children) }`; } case 'strike': { - break; + return `~~${ toString(node.children) }~~`; } case 'inlineCode': { - break; + return `\`${ node.props.code }\``; } case 'mathInline': { - break; + return `\\(${ node.props.formula }\\)`; } case 'mention': { - break; + return node.props.acct; } case 'hashtag': { - break; + return `#${ node.props.hashtag }`; } case 'url': { - break; + return node.props.url; } case 'link': { - break; + const prefix = node.props.silent ? '?' : ''; + return `${ prefix }[${ toString(node.children) }](${ node.props.url })`; } case 'fn': { - break; + 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; } } - return ''; + throw new Error('unknown mfm node'); } export function toString(nodes: MfmNode[]): string { From 3f584285fe92a9d5904f6ea7ec2bba0f4ff995a3 Mon Sep 17 00:00:00 2001 From: marihachi Date: Fri, 26 Mar 2021 23:02:07 +0900 Subject: [PATCH 3/3] toString --- src/index.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index b13370b..1a561ee 100644 --- a/src/index.ts +++ b/src/index.ts @@ -93,8 +93,15 @@ function nodeStringify(node: MfmNode): string { throw new Error('unknown mfm node'); } -export function toString(nodes: MfmNode[]): string { - return nodes.map(node => nodeStringify(node)).join(''); +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 {