2018-03-31 03:53:30 -07:00
|
|
|
import { lib as emojilib } from 'emojilib';
|
|
|
|
import { JSDOM } from 'jsdom';
|
2018-05-16 16:14:30 -07:00
|
|
|
import config from '../config';
|
2018-03-31 03:53:30 -07:00
|
|
|
|
|
|
|
const handlers = {
|
|
|
|
bold({ document }, { bold }) {
|
|
|
|
const b = document.createElement('b');
|
|
|
|
b.textContent = bold;
|
|
|
|
document.body.appendChild(b);
|
|
|
|
},
|
|
|
|
|
|
|
|
code({ document }, { code }) {
|
|
|
|
const pre = document.createElement('pre');
|
|
|
|
const inner = document.createElement('code');
|
|
|
|
inner.innerHTML = code;
|
|
|
|
pre.appendChild(inner);
|
|
|
|
document.body.appendChild(pre);
|
|
|
|
},
|
|
|
|
|
|
|
|
emoji({ document }, { content, emoji }) {
|
|
|
|
const found = emojilib[emoji];
|
|
|
|
const node = document.createTextNode(found ? found.char : content);
|
|
|
|
document.body.appendChild(node);
|
|
|
|
},
|
|
|
|
|
|
|
|
hashtag({ document }, { hashtag }) {
|
2018-06-09 11:47:09 -07:00
|
|
|
const span = document.createElement('span');
|
2018-06-09 11:48:38 -07:00
|
|
|
span.textContent = '#' + hashtag;
|
2018-06-09 11:47:09 -07:00
|
|
|
document.body.appendChild(span);
|
2018-03-31 03:53:30 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
'inline-code'({ document }, { code }) {
|
|
|
|
const element = document.createElement('code');
|
|
|
|
element.textContent = code;
|
|
|
|
document.body.appendChild(element);
|
|
|
|
},
|
|
|
|
|
|
|
|
link({ document }, { url, title }) {
|
|
|
|
const a = document.createElement('a');
|
|
|
|
a.href = url;
|
|
|
|
a.textContent = title;
|
|
|
|
document.body.appendChild(a);
|
|
|
|
},
|
|
|
|
|
|
|
|
mention({ document }, { content }) {
|
|
|
|
const a = document.createElement('a');
|
2018-05-16 16:14:30 -07:00
|
|
|
a.href = `${config.url}/${content}`;
|
2018-03-31 03:53:30 -07:00
|
|
|
a.textContent = content;
|
|
|
|
document.body.appendChild(a);
|
|
|
|
},
|
|
|
|
|
|
|
|
quote({ document }, { quote }) {
|
|
|
|
const blockquote = document.createElement('blockquote');
|
|
|
|
blockquote.textContent = quote;
|
|
|
|
document.body.appendChild(blockquote);
|
|
|
|
},
|
|
|
|
|
2018-04-21 02:25:25 -07:00
|
|
|
title({ document }, { content }) {
|
2018-04-18 23:05:39 -07:00
|
|
|
const h1 = document.createElement('h1');
|
2018-04-21 02:25:25 -07:00
|
|
|
h1.textContent = content;
|
2018-04-18 23:05:39 -07:00
|
|
|
document.body.appendChild(h1);
|
|
|
|
},
|
|
|
|
|
2018-03-31 03:53:30 -07:00
|
|
|
text({ document }, { content }) {
|
|
|
|
for (const text of content.split('\n')) {
|
|
|
|
const node = document.createTextNode(text);
|
|
|
|
document.body.appendChild(node);
|
|
|
|
|
|
|
|
const br = document.createElement('br');
|
|
|
|
document.body.appendChild(br);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
url({ document }, { url }) {
|
|
|
|
const a = document.createElement('a');
|
|
|
|
a.href = url;
|
|
|
|
a.textContent = url;
|
|
|
|
document.body.appendChild(a);
|
2018-04-21 02:59:16 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
search({ document }, { content, query }) {
|
|
|
|
const a = document.createElement('a');
|
|
|
|
a.href = `https://www.google.com/?#q=${query}`;
|
|
|
|
a.textContent = content;
|
|
|
|
document.body.appendChild(a);
|
2018-03-31 03:53:30 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default tokens => {
|
|
|
|
const { window } = new JSDOM('');
|
|
|
|
|
|
|
|
for (const token of tokens) {
|
|
|
|
handlers[token.type](window, token);
|
|
|
|
}
|
|
|
|
|
|
|
|
return `<p>${window.document.body.innerHTML}</p>`;
|
|
|
|
};
|