2016-12-30 03:57:17 +01:00
|
|
|
const riot = require('riot');
|
2017-01-04 07:32:04 +01:00
|
|
|
const nyaize = require('nyaize').default;
|
2017-03-01 06:32:18 +01:00
|
|
|
//const emojinize = require('emojinize');
|
2017-02-21 20:19:53 +01:00
|
|
|
const CONFIG = require('./config');
|
2016-12-30 03:57:17 +01:00
|
|
|
|
2017-03-01 06:19:59 +01:00
|
|
|
const escape = text =>
|
|
|
|
text
|
2017-02-08 17:07:06 +01:00
|
|
|
.replace(/>/g, '>')
|
|
|
|
.replace(/</g, '<');
|
|
|
|
|
2017-03-01 06:19:59 +01:00
|
|
|
module.exports = (tokens, shouldBreak) => {
|
2016-12-31 10:41:52 +01:00
|
|
|
if (shouldBreak == null) {
|
|
|
|
shouldBreak = true;
|
2016-12-28 23:49:51 +01:00
|
|
|
}
|
2016-12-30 03:57:17 +01:00
|
|
|
|
|
|
|
const me = riot.mixin('i').me;
|
|
|
|
|
2017-03-01 06:19:59 +01:00
|
|
|
let text = tokens.map(token => {
|
2016-12-28 23:49:51 +01:00
|
|
|
switch (token.type) {
|
|
|
|
case 'text':
|
2017-02-08 17:07:06 +01:00
|
|
|
return escape(token.content)
|
|
|
|
.replace(/(\r\n|\n|\r)/g, shouldBreak ? '<br>' : ' ');
|
2016-12-28 23:49:51 +01:00
|
|
|
case 'bold':
|
2017-02-08 17:07:06 +01:00
|
|
|
return '<strong>' + escape(token.bold) + '</strong>';
|
2016-12-28 23:49:51 +01:00
|
|
|
case 'link':
|
2017-02-08 17:07:06 +01:00
|
|
|
return '<mk-url href="' + escape(token.content) + '" target="_blank"></mk-url>';
|
2016-12-28 23:49:51 +01:00
|
|
|
case 'mention':
|
2017-02-11 00:49:50 +01:00
|
|
|
return '<a href="' + CONFIG.url + '/' + escape(token.username) + '" target="_blank" data-user-preview="' + token.content + '" ' + (me && me.username == token.username ? 'data-is-me' : '') + '>' + token.content + '</a>';
|
2016-12-28 23:49:51 +01:00
|
|
|
case 'hashtag': // TODO
|
2017-02-08 17:07:06 +01:00
|
|
|
return '<a>' + escape(token.content) + '</a>';
|
|
|
|
case 'code':
|
2017-02-11 18:38:47 +01:00
|
|
|
return '<pre><code>' + token.html + '</code></pre>';
|
2017-02-11 15:41:57 +01:00
|
|
|
case 'inline-code':
|
2017-02-11 18:38:47 +01:00
|
|
|
return '<code>' + token.html + '</code>';
|
2017-02-28 21:33:27 +01:00
|
|
|
case 'emoji':
|
2017-03-01 06:32:18 +01:00
|
|
|
return '<i>' + token.content + '</i>';
|
|
|
|
//return emojinize.encode(token.content)
|
2016-12-28 23:49:51 +01:00
|
|
|
}
|
|
|
|
}).join('');
|
2016-12-30 03:57:17 +01:00
|
|
|
|
2017-03-01 06:22:56 +01:00
|
|
|
// Remove needless whitespaces
|
2017-03-01 06:22:29 +01:00
|
|
|
text = text
|
|
|
|
.replace(/ <code>/g, '<code>').replace(/<\/code> /g, '</code>')
|
|
|
|
.replace(/<br><code><pre>/g, '<code><pre>').replace(/<\/code><\/pre><br>/g, '</code></pre>');
|
2017-02-08 17:07:06 +01:00
|
|
|
|
2016-12-30 03:57:17 +01:00
|
|
|
if (me && me.data && me.data.nya) {
|
2017-01-04 07:32:04 +01:00
|
|
|
text = nyaize(text);
|
2016-12-30 03:57:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
2017-03-01 06:19:59 +01:00
|
|
|
};
|