diff options
Diffstat (limited to 'packages/backend/src/core/MfmService.ts')
| -rw-r--r-- | packages/backend/src/core/MfmService.ts | 48 |
1 files changed, 45 insertions, 3 deletions
diff --git a/packages/backend/src/core/MfmService.ts b/packages/backend/src/core/MfmService.ts index dc47e38562..6c2f673217 100644 --- a/packages/backend/src/core/MfmService.ts +++ b/packages/backend/src/core/MfmService.ts @@ -179,6 +179,40 @@ export class MfmService { break; } + // this is here only to catch upstream changes! + case 'ruby--': { + let ruby: [string, string][] = []; + for (const child of node.childNodes) { + if (child.nodeName === 'rp') { + continue; + } + if (treeAdapter.isTextNode(child) && !/\s|\[|\]/.test(child.value)) { + ruby.push([child.value, '']); + continue; + } + if (child.nodeName === 'rt' && ruby.length > 0) { + const rt = getText(child); + if (/\s|\[|\]/.test(rt)) { + // If any space is included in rt, it is treated as a normal text + ruby = []; + appendChildren(node.childNodes); + break; + } else { + ruby.at(-1)![1] = rt; + continue; + } + } + // If any other element is included in ruby, it is treated as a normal text + ruby = []; + appendChildren(node.childNodes); + break; + } + for (const [base, rt] of ruby) { + text += `$[ruby ${base} ${rt}]`; + } + break; + } + // block code (<pre><code>) case 'pre': { if (node.childNodes.length === 1 && node.childNodes[0].nodeName === 'code') { @@ -277,16 +311,24 @@ export class MfmService { continue; } if (child.nodeName === 'rt') { - text += '$[ruby $[group '; + // the only case in which we don't need a `$[group ]` + // is when both sides of the ruby are simple words + const needsGroup = nonRtNodes.length > 1 || + /\s|\[|\]/.test(getText(nonRtNodes[0])) || + /\s|\[|\]/.test(getText(child)); + text += '$[ruby '; + if (needsGroup) text += '$[group '; appendChildren(nonRtNodes); - text += '] '; + if (needsGroup) text += ']'; + text += ' '; analyze(child); - text += '] '; + text += ']'; nonRtNodes = []; continue; } nonRtNodes.push(child); } + appendChildren(nonRtNodes); } break; } |