yumechi-no-kuni/src/web/app/common/scripts/text-compiler.js
2017-02-09 01:07:06 +09:00

45 lines
1.3 KiB
JavaScript

const riot = require('riot');
const nyaize = require('nyaize').default;
const escape = function(text) {
return text
.replace(/>/g, '>')
.replace(/</g, '&lt;');
};
module.exports = function(tokens, shouldBreak, shouldEscape) {
if (shouldBreak == null) {
shouldBreak = true;
}
if (shouldEscape != null) {
alert('do not use this option')
}
const me = riot.mixin('i').me;
let text = tokens.map(function(token) {
switch (token.type) {
case 'text':
return escape(token.content)
.replace(/(\r\n|\n|\r)/g, shouldBreak ? '<br>' : ' ');
case 'bold':
return '<strong>' + escape(token.bold) + '</strong>';
case 'link':
return '<mk-url href="' + escape(token.content) + '" target="_blank"></mk-url>';
case 'mention':
return '<a href="' + CONFIG.url + '/' + escape(token.username) + '" target="_blank" data-user-preview="' + token.content + '">' + token.content + '</a>';
case 'hashtag': // TODO
return '<a>' + escape(token.content) + '</a>';
case 'code':
return '<pre><code>' + escape(token.code) + '</code></pre>';
}
}).join('');
text = text.replace(/<br><code><pre>/g, '<code><pre>').replace(/<\/code><\/pre><br>/g, '</code></pre>');
if (me && me.data && me.data.nya) {
text = nyaize(text);
}
return text;
}