2016-12-30 02:57:17 +00:00
|
|
|
const riot = require('riot');
|
2017-01-04 06:32:04 +00:00
|
|
|
const nyaize = require('nyaize').default;
|
2016-12-30 02:57:17 +00:00
|
|
|
|
2016-12-31 09:41:52 +00:00
|
|
|
module.exports = function(tokens, shouldBreak, escape) {
|
|
|
|
if (shouldBreak == null) {
|
|
|
|
shouldBreak = true;
|
2016-12-28 22:49:51 +00:00
|
|
|
}
|
|
|
|
if (escape == null) {
|
|
|
|
escape = true;
|
|
|
|
}
|
2016-12-30 02:57:17 +00:00
|
|
|
|
|
|
|
const me = riot.mixin('i').me;
|
|
|
|
|
|
|
|
let text = tokens.map(function(token) {
|
2016-12-28 22:49:51 +00:00
|
|
|
switch (token.type) {
|
|
|
|
case 'text':
|
|
|
|
if (escape) {
|
|
|
|
return token.content
|
|
|
|
.replace(/>/g, '>')
|
|
|
|
.replace(/</g, '<')
|
2016-12-31 09:41:52 +00:00
|
|
|
.replace(/(\r\n|\n|\r)/g, shouldBreak ? '<br>' : ' ');
|
2016-12-28 22:49:51 +00:00
|
|
|
} else {
|
|
|
|
return token.content
|
2016-12-31 09:41:52 +00:00
|
|
|
.replace(/(\r\n|\n|\r)/g, shouldBreak ? '<br>' : ' ');
|
2016-12-28 22:49:51 +00:00
|
|
|
}
|
|
|
|
case 'bold':
|
|
|
|
return '<strong>' + token.bold + '</strong>';
|
|
|
|
case 'link':
|
|
|
|
return '<mk-url href="' + token.content + '" target="_blank"></mk-url>';
|
|
|
|
case 'mention':
|
|
|
|
return '<a href="' + CONFIG.url + '/' + token.username + '" target="_blank" data-user-preview="' + token.content + '">' + token.content + '</a>';
|
|
|
|
case 'hashtag': // TODO
|
|
|
|
return '<a>' + token.content + '</a>';
|
|
|
|
}
|
|
|
|
}).join('');
|
2016-12-30 02:57:17 +00:00
|
|
|
|
|
|
|
if (me && me.data && me.data.nya) {
|
2017-01-04 06:32:04 +00:00
|
|
|
text = nyaize(text);
|
2016-12-30 02:57:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return text;
|
2016-12-28 22:49:51 +00:00
|
|
|
}
|