activitypub: add MathML rendering

Ref: #343 (comment)
Ref: FEP-dc88
Changelog: Changed
This commit is contained in:
Johann150 2023-09-24 17:37:32 +02:00
parent 1e7d2cf54c
commit f6c3d44265
Signed by: Johann150
GPG key ID: 9EE6577A2A06F8F1
2 changed files with 12 additions and 6 deletions

View file

@ -60,6 +60,7 @@
"json5-loader": "4.0.1",
"jsonld": "6.0.0",
"jsrsasign": "10.5.25",
"katex": "^0.16.0",
"koa": "2.13.4",
"koa-bodyparser": "4.3.0",
"koa-favicon": "2.1.0",

View file

@ -1,4 +1,5 @@
import { JSDOM } from 'jsdom';
import katex from 'katex';
import * as mfm from 'mfm-js';
import config from '@/config/index.js';
import { UserProfiles } from '@/models/index.js';
@ -6,6 +7,14 @@ import { extractMentions } from '@/misc/extract-mentions.js';
import { intersperse } from '@/prelude/array.js';
import { toPunyNullable } from '@/misc/convert-host.js';
function toMathMl(code: string): HTMLElement {
const rendered = katex.renderToString(code, {
throwOnError: false,
output: 'mathml',
});
return JSDOM.fragment(rendered).querySelector('math');
}
// Transforms MFM to HTML, given the MFM text and a list of user IDs that are
// mentioned in the text. If the list of mentions is not given, all mentions
// from the text will be extracted.
@ -98,15 +107,11 @@ export async function toHtml(mfmText: string, mentions?: string[]): Promise<stri
},
async mathInline(node) {
const el = doc.createElement('code');
el.textContent = node.props.formula;
return el;
return toMathMl(node.props.formula);
},
async mathBlock(node) {
const el = doc.createElement('code');
el.textContent = node.props.formula;
return el;
return toMathMl(node.props.formula);
},
async link(node) {