2019-02-03 16:49:00 +02:00
|
|
|
import { JSDOM } from 'jsdom';
|
2018-05-17 02:14:30 +03:00
|
|
|
import config from '../config';
|
2018-09-05 20:28:04 +03:00
|
|
|
import { intersperse } from '../prelude/array';
|
2019-03-14 14:23:15 +02:00
|
|
|
import { MfmForest, MfmTree } from './prelude';
|
2019-04-07 15:50:36 +03:00
|
|
|
import { IMentionedRemoteUsers } from '../models/entities/note';
|
2020-08-07 05:27:37 +03:00
|
|
|
import { wellKnownServices } from '../well-known-services';
|
2018-08-18 18:31:25 +03:00
|
|
|
|
2019-04-12 19:43:22 +03:00
|
|
|
export function toHtml(tokens: MfmForest | null, mentionedRemoteUsers: IMentionedRemoteUsers = []) {
|
2018-07-01 07:46:34 +03:00
|
|
|
if (tokens == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-03-31 13:53:30 +03:00
|
|
|
const { window } = new JSDOM('');
|
|
|
|
|
2018-11-20 22:11:00 +02:00
|
|
|
const doc = window.document;
|
|
|
|
|
2018-12-20 12:41:04 +02:00
|
|
|
function appendChildren(children: MfmForest, targetElement: any): void {
|
|
|
|
for (const child of children.map(t => handlers[t.node.type](t))) targetElement.appendChild(child);
|
2018-03-31 13:53:30 +03:00
|
|
|
}
|
|
|
|
|
2018-12-20 12:41:04 +02:00
|
|
|
const handlers: { [key: string]: (token: MfmTree) => any } = {
|
2018-11-20 22:11:00 +02:00
|
|
|
bold(token) {
|
|
|
|
const el = doc.createElement('b');
|
2018-12-09 06:15:32 +02:00
|
|
|
appendChildren(token.children, el);
|
2018-11-20 22:11:00 +02:00
|
|
|
return el;
|
|
|
|
},
|
|
|
|
|
2018-12-05 13:11:54 +02:00
|
|
|
small(token) {
|
|
|
|
const el = doc.createElement('small');
|
2018-12-09 06:15:32 +02:00
|
|
|
appendChildren(token.children, el);
|
2018-12-05 13:11:54 +02:00
|
|
|
return el;
|
|
|
|
},
|
|
|
|
|
2018-12-03 18:28:21 +02:00
|
|
|
strike(token) {
|
|
|
|
const el = doc.createElement('del');
|
2018-12-09 06:15:32 +02:00
|
|
|
appendChildren(token.children, el);
|
2018-12-03 18:28:21 +02:00
|
|
|
return el;
|
|
|
|
},
|
|
|
|
|
2018-12-05 10:39:26 +02:00
|
|
|
italic(token) {
|
|
|
|
const el = doc.createElement('i');
|
2018-12-09 06:15:32 +02:00
|
|
|
appendChildren(token.children, el);
|
2018-12-05 10:39:26 +02:00
|
|
|
return el;
|
|
|
|
},
|
|
|
|
|
2020-11-07 16:41:21 +02:00
|
|
|
fn(token) {
|
2019-01-27 09:18:04 +02:00
|
|
|
const el = doc.createElement('i');
|
|
|
|
appendChildren(token.children, el);
|
|
|
|
return el;
|
|
|
|
},
|
|
|
|
|
2018-11-20 22:11:00 +02:00
|
|
|
blockCode(token) {
|
|
|
|
const pre = doc.createElement('pre');
|
|
|
|
const inner = doc.createElement('code');
|
2020-11-10 14:00:14 +02:00
|
|
|
inner.textContent = token.node.props.code;
|
2018-11-20 22:11:00 +02:00
|
|
|
pre.appendChild(inner);
|
|
|
|
return pre;
|
|
|
|
},
|
|
|
|
|
2018-11-25 06:36:40 +02:00
|
|
|
center(token) {
|
|
|
|
const el = doc.createElement('div');
|
2018-12-09 06:15:32 +02:00
|
|
|
appendChildren(token.children, el);
|
2018-11-25 06:36:40 +02:00
|
|
|
return el;
|
|
|
|
},
|
|
|
|
|
2018-11-20 22:11:00 +02:00
|
|
|
emoji(token) {
|
2020-02-02 05:10:23 +02:00
|
|
|
return doc.createTextNode(token.node.props.emoji ? token.node.props.emoji : `\u200B:${token.node.props.name}:\u200B`);
|
2018-11-20 22:11:00 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
hashtag(token) {
|
|
|
|
const a = doc.createElement('a');
|
2018-12-20 12:41:04 +02:00
|
|
|
a.href = `${config.url}/tags/${token.node.props.hashtag}`;
|
|
|
|
a.textContent = `#${token.node.props.hashtag}`;
|
2018-11-20 22:11:00 +02:00
|
|
|
a.setAttribute('rel', 'tag');
|
|
|
|
return a;
|
|
|
|
},
|
|
|
|
|
|
|
|
inlineCode(token) {
|
|
|
|
const el = doc.createElement('code');
|
2018-12-20 12:41:04 +02:00
|
|
|
el.textContent = token.node.props.code;
|
2018-11-20 22:11:00 +02:00
|
|
|
return el;
|
|
|
|
},
|
|
|
|
|
2019-01-25 16:08:06 +02:00
|
|
|
mathInline(token) {
|
|
|
|
const el = doc.createElement('code');
|
|
|
|
el.textContent = token.node.props.formula;
|
|
|
|
return el;
|
|
|
|
},
|
|
|
|
|
|
|
|
mathBlock(token) {
|
2018-11-20 22:11:00 +02:00
|
|
|
const el = doc.createElement('code');
|
2018-12-20 12:41:04 +02:00
|
|
|
el.textContent = token.node.props.formula;
|
2018-11-20 22:11:00 +02:00
|
|
|
return el;
|
|
|
|
},
|
|
|
|
|
|
|
|
link(token) {
|
|
|
|
const a = doc.createElement('a');
|
2018-12-20 12:41:04 +02:00
|
|
|
a.href = token.node.props.url;
|
2018-12-09 06:15:32 +02:00
|
|
|
appendChildren(token.children, a);
|
2018-11-20 22:11:00 +02:00
|
|
|
return a;
|
|
|
|
},
|
|
|
|
|
|
|
|
mention(token) {
|
|
|
|
const a = doc.createElement('a');
|
2018-12-20 12:41:04 +02:00
|
|
|
const { username, host, acct } = token.node.props;
|
2020-08-07 05:27:37 +03:00
|
|
|
const wellKnown = wellKnownServices.find(x => x[0] === host);
|
|
|
|
if (wellKnown) {
|
|
|
|
a.href = wellKnown[1](username);
|
|
|
|
} else {
|
|
|
|
const remoteUserInfo = mentionedRemoteUsers.find(remoteUser => remoteUser.username === username && remoteUser.host === host);
|
|
|
|
a.href = remoteUserInfo ? (remoteUserInfo.url ? remoteUserInfo.url : remoteUserInfo.uri) : `${config.url}/${acct}`;
|
|
|
|
a.className = 'u-url mention';
|
2018-12-12 18:33:18 +02:00
|
|
|
}
|
2018-11-20 22:11:00 +02:00
|
|
|
a.textContent = acct;
|
|
|
|
return a;
|
|
|
|
},
|
|
|
|
|
|
|
|
quote(token) {
|
|
|
|
const el = doc.createElement('blockquote');
|
2018-12-09 06:15:32 +02:00
|
|
|
appendChildren(token.children, el);
|
2018-11-20 22:11:00 +02:00
|
|
|
return el;
|
|
|
|
},
|
|
|
|
|
|
|
|
text(token) {
|
|
|
|
const el = doc.createElement('span');
|
2019-02-03 16:49:00 +02:00
|
|
|
const nodes = (token.node.props.text as string).split(/\r\n|\r|\n/).map(x => doc.createTextNode(x) as Node);
|
2018-11-20 22:11:00 +02:00
|
|
|
|
2019-02-03 16:49:00 +02:00
|
|
|
for (const x of intersperse<Node | 'br'>('br', nodes)) {
|
2018-12-08 20:40:40 +02:00
|
|
|
el.appendChild(x === 'br' ? doc.createElement('br') : x);
|
2018-11-20 22:11:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return el;
|
|
|
|
},
|
|
|
|
|
|
|
|
url(token) {
|
|
|
|
const a = doc.createElement('a');
|
2018-12-20 12:41:04 +02:00
|
|
|
a.href = token.node.props.url;
|
|
|
|
a.textContent = token.node.props.url;
|
2018-11-20 22:11:00 +02:00
|
|
|
return a;
|
|
|
|
},
|
|
|
|
|
|
|
|
search(token) {
|
|
|
|
const a = doc.createElement('a');
|
2020-10-17 14:12:00 +03:00
|
|
|
a.href = `https://www.google.com/search?q=${token.node.props.query}`;
|
2018-12-20 12:41:04 +02:00
|
|
|
a.textContent = token.node.props.content;
|
2018-11-20 22:11:00 +02:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-09 06:15:32 +02:00
|
|
|
appendChildren(tokens, doc.body);
|
2018-11-20 22:11:00 +02:00
|
|
|
|
|
|
|
return `<p>${doc.body.innerHTML}</p>`;
|
2019-01-30 09:56:27 +02:00
|
|
|
}
|