2018-08-04 11:39:59 +09:00
|
|
|
const jsdom = require('jsdom');
|
|
|
|
const { JSDOM } = jsdom;
|
2018-05-17 08:14:30 +09:00
|
|
|
import config from '../config';
|
2018-06-17 15:58:23 +09:00
|
|
|
import { INote } from '../models/note';
|
2018-09-06 02:28:04 +09:00
|
|
|
import { intersperse } from '../prelude/array';
|
2019-01-30 14:51:30 +09:00
|
|
|
import { MfmForest, MfmTree } from './types';
|
2018-08-19 00:31:25 +09:00
|
|
|
|
2019-01-30 16:56:27 +09:00
|
|
|
export function toHtml(tokens: MfmForest, mentionedRemoteUsers: INote['mentionedRemoteUsers'] = []) {
|
2018-07-01 13:46:34 +09:00
|
|
|
if (tokens == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-03-31 19:53:30 +09:00
|
|
|
const { window } = new JSDOM('');
|
|
|
|
|
2018-11-21 05:11:00 +09:00
|
|
|
const doc = window.document;
|
|
|
|
|
2018-12-20 19:41:04 +09: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 19:53:30 +09:00
|
|
|
}
|
|
|
|
|
2018-12-20 19:41:04 +09:00
|
|
|
const handlers: { [key: string]: (token: MfmTree) => any } = {
|
2018-11-21 05:11:00 +09:00
|
|
|
bold(token) {
|
|
|
|
const el = doc.createElement('b');
|
2018-12-09 13:15:32 +09:00
|
|
|
appendChildren(token.children, el);
|
2018-11-21 05:11:00 +09:00
|
|
|
return el;
|
|
|
|
},
|
|
|
|
|
|
|
|
big(token) {
|
|
|
|
const el = doc.createElement('strong');
|
2018-12-09 13:15:32 +09:00
|
|
|
appendChildren(token.children, el);
|
2018-11-21 05:11:00 +09:00
|
|
|
return el;
|
|
|
|
},
|
|
|
|
|
2018-12-05 20:11:54 +09:00
|
|
|
small(token) {
|
|
|
|
const el = doc.createElement('small');
|
2018-12-09 13:15:32 +09:00
|
|
|
appendChildren(token.children, el);
|
2018-12-05 20:11:54 +09:00
|
|
|
return el;
|
|
|
|
},
|
|
|
|
|
2018-12-04 01:28:21 +09:00
|
|
|
strike(token) {
|
|
|
|
const el = doc.createElement('del');
|
2018-12-09 13:15:32 +09:00
|
|
|
appendChildren(token.children, el);
|
2018-12-04 01:28:21 +09:00
|
|
|
return el;
|
|
|
|
},
|
|
|
|
|
2018-12-05 17:39:26 +09:00
|
|
|
italic(token) {
|
|
|
|
const el = doc.createElement('i');
|
2018-12-09 13:15:32 +09:00
|
|
|
appendChildren(token.children, el);
|
2018-12-05 17:39:26 +09:00
|
|
|
return el;
|
|
|
|
},
|
|
|
|
|
2018-11-21 05:11:00 +09:00
|
|
|
motion(token) {
|
|
|
|
const el = doc.createElement('i');
|
2018-12-09 13:15:32 +09:00
|
|
|
appendChildren(token.children, el);
|
2018-11-21 05:11:00 +09:00
|
|
|
return el;
|
|
|
|
},
|
|
|
|
|
2019-01-27 16:18:04 +09:00
|
|
|
spin(token) {
|
|
|
|
const el = doc.createElement('i');
|
|
|
|
appendChildren(token.children, el);
|
|
|
|
return el;
|
|
|
|
},
|
|
|
|
|
2019-01-27 19:12:45 +09:00
|
|
|
jump(token) {
|
|
|
|
const el = doc.createElement('i');
|
|
|
|
appendChildren(token.children, el);
|
|
|
|
return el;
|
|
|
|
},
|
|
|
|
|
2019-01-27 16:31:00 +09:00
|
|
|
flip(token) {
|
|
|
|
const el = doc.createElement('span');
|
|
|
|
appendChildren(token.children, el);
|
|
|
|
return el;
|
|
|
|
},
|
|
|
|
|
2018-11-21 05:11:00 +09:00
|
|
|
blockCode(token) {
|
|
|
|
const pre = doc.createElement('pre');
|
|
|
|
const inner = doc.createElement('code');
|
2018-12-20 19:41:04 +09:00
|
|
|
inner.innerHTML = token.node.props.code;
|
2018-11-21 05:11:00 +09:00
|
|
|
pre.appendChild(inner);
|
|
|
|
return pre;
|
|
|
|
},
|
|
|
|
|
2018-11-25 13:36:40 +09:00
|
|
|
center(token) {
|
|
|
|
const el = doc.createElement('div');
|
2018-12-09 13:15:32 +09:00
|
|
|
appendChildren(token.children, el);
|
2018-11-25 13:36:40 +09:00
|
|
|
return el;
|
|
|
|
},
|
|
|
|
|
2018-11-21 05:11:00 +09:00
|
|
|
emoji(token) {
|
2018-12-20 19:41:04 +09:00
|
|
|
return doc.createTextNode(token.node.props.emoji ? token.node.props.emoji : `:${token.node.props.name}:`);
|
2018-11-21 05:11:00 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
hashtag(token) {
|
|
|
|
const a = doc.createElement('a');
|
2018-12-20 19:41:04 +09:00
|
|
|
a.href = `${config.url}/tags/${token.node.props.hashtag}`;
|
|
|
|
a.textContent = `#${token.node.props.hashtag}`;
|
2018-11-21 05:11:00 +09:00
|
|
|
a.setAttribute('rel', 'tag');
|
|
|
|
return a;
|
|
|
|
},
|
|
|
|
|
|
|
|
inlineCode(token) {
|
|
|
|
const el = doc.createElement('code');
|
2018-12-20 19:41:04 +09:00
|
|
|
el.textContent = token.node.props.code;
|
2018-11-21 05:11:00 +09:00
|
|
|
return el;
|
|
|
|
},
|
|
|
|
|
2019-01-25 23:08:06 +09:00
|
|
|
mathInline(token) {
|
|
|
|
const el = doc.createElement('code');
|
|
|
|
el.textContent = token.node.props.formula;
|
|
|
|
return el;
|
|
|
|
},
|
|
|
|
|
|
|
|
mathBlock(token) {
|
2018-11-21 05:11:00 +09:00
|
|
|
const el = doc.createElement('code');
|
2018-12-20 19:41:04 +09:00
|
|
|
el.textContent = token.node.props.formula;
|
2018-11-21 05:11:00 +09:00
|
|
|
return el;
|
|
|
|
},
|
|
|
|
|
|
|
|
link(token) {
|
|
|
|
const a = doc.createElement('a');
|
2018-12-20 19:41:04 +09:00
|
|
|
a.href = token.node.props.url;
|
2018-12-09 13:15:32 +09:00
|
|
|
appendChildren(token.children, a);
|
2018-11-21 05:11:00 +09:00
|
|
|
return a;
|
|
|
|
},
|
|
|
|
|
|
|
|
mention(token) {
|
|
|
|
const a = doc.createElement('a');
|
2018-12-20 19:41:04 +09:00
|
|
|
const { username, host, acct } = token.node.props;
|
2018-12-13 01:33:18 +09:00
|
|
|
switch (host) {
|
|
|
|
case 'github.com':
|
|
|
|
a.href = `https://github.com/${username}`;
|
|
|
|
break;
|
|
|
|
case 'twitter.com':
|
|
|
|
a.href = `https://twitter.com/${username}`;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
const remoteUserInfo = mentionedRemoteUsers.find(remoteUser => remoteUser.username === username && remoteUser.host === host);
|
|
|
|
a.href = remoteUserInfo ? remoteUserInfo.uri : `${config.url}/${acct}`;
|
|
|
|
break;
|
|
|
|
}
|
2018-11-21 05:11:00 +09:00
|
|
|
a.textContent = acct;
|
|
|
|
return a;
|
|
|
|
},
|
|
|
|
|
|
|
|
quote(token) {
|
|
|
|
const el = doc.createElement('blockquote');
|
2018-12-09 13:15:32 +09:00
|
|
|
appendChildren(token.children, el);
|
2018-11-21 05:11:00 +09:00
|
|
|
return el;
|
|
|
|
},
|
|
|
|
|
|
|
|
title(token) {
|
|
|
|
const el = doc.createElement('h1');
|
2018-12-09 13:15:32 +09:00
|
|
|
appendChildren(token.children, el);
|
2018-11-21 05:11:00 +09:00
|
|
|
return el;
|
|
|
|
},
|
|
|
|
|
|
|
|
text(token) {
|
|
|
|
const el = doc.createElement('span');
|
2019-01-29 20:33:28 +09:00
|
|
|
const nodes = (token.node.props.text as string).split(/\r\n|\r|\n/).map(x => doc.createTextNode(x));
|
2018-11-21 05:11:00 +09:00
|
|
|
|
|
|
|
for (const x of intersperse('br', nodes)) {
|
2018-12-09 03:40:40 +09:00
|
|
|
el.appendChild(x === 'br' ? doc.createElement('br') : x);
|
2018-11-21 05:11:00 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return el;
|
|
|
|
},
|
|
|
|
|
|
|
|
url(token) {
|
|
|
|
const a = doc.createElement('a');
|
2018-12-20 19:41:04 +09:00
|
|
|
a.href = token.node.props.url;
|
|
|
|
a.textContent = token.node.props.url;
|
2018-11-21 05:11:00 +09:00
|
|
|
return a;
|
|
|
|
},
|
|
|
|
|
|
|
|
search(token) {
|
|
|
|
const a = doc.createElement('a');
|
2018-12-20 19:41:04 +09:00
|
|
|
a.href = `https://www.google.com/?#q=${token.node.props.query}`;
|
|
|
|
a.textContent = token.node.props.content;
|
2018-11-21 05:11:00 +09:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-09 13:15:32 +09:00
|
|
|
appendChildren(tokens, doc.body);
|
2018-11-21 05:11:00 +09:00
|
|
|
|
|
|
|
return `<p>${doc.body.innerHTML}</p>`;
|
2019-01-30 16:56:27 +09:00
|
|
|
}
|