jormungandr-bite/src/mfm/html.ts

182 lines
4.2 KiB
TypeScript
Raw Normal View History

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