activitypub: parse MathML to MFM
Some checks failed
ci/woodpecker/push/lint-foundkey-js Pipeline was successful
ci/woodpecker/push/lint-client Pipeline failed
ci/woodpecker/push/lint-sw Pipeline failed
ci/woodpecker/push/lint-backend Pipeline failed
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/test Pipeline failed

Ref: FEP-dc88
Changelog: Changed
This commit is contained in:
Johann150 2023-09-24 19:28:55 +02:00
parent 5bb10de1e0
commit 2fcea24817
Signed by: Johann150
GPG key ID: 9EE6577A2A06F8F1

View file

@ -176,6 +176,53 @@ export function fromHtml(html: string, quoteUri?: string | null): string {
break;
}
// inline or block KaTeX
case 'math': {
// This node should contain <semantics>[...]<annotation/>[...]</semantics> tag with the "source code".
if (node.childNodes.length !== 1 || node.childNodes[0].nodeName !== 'semantics')
break;
const semantics = node.childNodes[0];
// only select well formed annotations
const annotations = semantics.childNodes
.filter(node =>
node.nodeName === 'annotation'
&& node.childNodes.length === 1
&& node.childNodes[0].nodeName === '#text'
);
if (annotations.length === 0)
break;
let annotation = annotations[0];
// try to prefer a TeX annotation if there are multiple annotations
const filteredAnnotations = annotations.filter(node => node.attrs.some(attribute => attribute.name === 'encoding' && attribute.value === 'application/x-tex'));
if (filteredAnnotations.length > 0) {
annotation = filteredAnnotations[0];
}
const formula = annotation.childNodes[0].value;
if (annotation.attrs.some(attribute => attribute.name === 'encoding' && attribute.value === 'application/x-tex')) {
// can be rendered as KaTeX, now decide if it is possible to render as inline or not
if (/[\r\n]/.test(formula)) {
// line break, this must be rendered as a block
text += '\n\\[' + formula + '\\]\n';
} else {
// render as inline
text += '\\(' + formula + '\\)';
}
} else {
// not KaTeX, but if there is a plaintext annotation it can still be rendered as code
if (/[\r\n]/.test(formula)) {
// line break, this must be rendered as a block
text += '\n```\n' + formula + '\n```\n';
} else {
// render as inline
text += '`' + formula + '`';
}
}
break;
}
case 'blockquote': {
const t = getText(node);
if (t) {