monkeeShark/src/web/app/common/scripts/text-compiler.js

46 lines
1.3 KiB
JavaScript
Raw Normal View History

2016-12-30 02:57:17 +00:00
const riot = require('riot');
const nyaize = require('nyaize').default;
2016-12-30 02:57:17 +00:00
2017-02-08 16:07:06 +00:00
const escape = function(text) {
return text
.replace(/>/g, '>')
.replace(/</g, '&lt;');
};
module.exports = function(tokens, shouldBreak, shouldEscape) {
2016-12-31 09:41:52 +00:00
if (shouldBreak == null) {
shouldBreak = true;
2016-12-28 22:49:51 +00:00
}
2017-02-08 16:07:06 +00:00
if (shouldEscape != null) {
alert('do not use this option')
2016-12-28 22:49:51 +00:00
}
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':
2017-02-08 16:07:06 +00:00
return escape(token.content)
.replace(/(\r\n|\n|\r)/g, shouldBreak ? '<br>' : ' ');
2016-12-28 22:49:51 +00:00
case 'bold':
2017-02-08 16:07:06 +00:00
return '<strong>' + escape(token.bold) + '</strong>';
2016-12-28 22:49:51 +00:00
case 'link':
2017-02-08 16:07:06 +00:00
return '<mk-url href="' + escape(token.content) + '" target="_blank"></mk-url>';
2016-12-28 22:49:51 +00:00
case 'mention':
2017-02-08 16:07:06 +00:00
return '<a href="' + CONFIG.url + '/' + escape(token.username) + '" target="_blank" data-user-preview="' + token.content + '">' + token.content + '</a>';
2016-12-28 22:49:51 +00:00
case 'hashtag': // TODO
2017-02-08 16:07:06 +00:00
return '<a>' + escape(token.content) + '</a>';
case 'code':
return '<pre><code>' + escape(token.code) + '</code></pre>';
2016-12-28 22:49:51 +00:00
}
}).join('');
2016-12-30 02:57:17 +00:00
2017-02-08 16:07:06 +00:00
text = text.replace(/<br><code><pre>/g, '<code><pre>').replace(/<\/code><\/pre><br>/g, '</code></pre>');
2016-12-30 02:57:17 +00:00
if (me && me.data && me.data.nya) {
text = nyaize(text);
2016-12-30 02:57:17 +00:00
}
return text;
2016-12-28 22:49:51 +00:00
}