2017-11-23 05:43:00 +09:00
|
|
|
declare const _URL_: string;
|
|
|
|
|
2017-03-18 20:05:11 +09:00
|
|
|
import * as riot from 'riot';
|
2017-11-13 18:05:35 +09:00
|
|
|
import * as pictograph from 'pictograph';
|
2016-12-30 11:57:17 +09:00
|
|
|
|
2017-03-01 14:19:59 +09:00
|
|
|
const escape = text =>
|
|
|
|
text
|
2017-02-09 01:07:06 +09:00
|
|
|
.replace(/>/g, '>')
|
|
|
|
.replace(/</g, '<');
|
|
|
|
|
2017-03-18 20:05:11 +09:00
|
|
|
export default (tokens, shouldBreak) => {
|
2016-12-31 18:41:52 +09:00
|
|
|
if (shouldBreak == null) {
|
|
|
|
shouldBreak = true;
|
2016-12-29 07:49:51 +09:00
|
|
|
}
|
2016-12-30 11:57:17 +09:00
|
|
|
|
2017-11-13 18:05:35 +09:00
|
|
|
const me = (riot as any).mixin('i').me;
|
2016-12-30 11:57:17 +09:00
|
|
|
|
2017-03-01 14:19:59 +09:00
|
|
|
let text = tokens.map(token => {
|
2016-12-29 07:49:51 +09:00
|
|
|
switch (token.type) {
|
|
|
|
case 'text':
|
2017-02-09 01:07:06 +09:00
|
|
|
return escape(token.content)
|
|
|
|
.replace(/(\r\n|\n|\r)/g, shouldBreak ? '<br>' : ' ');
|
2016-12-29 07:49:51 +09:00
|
|
|
case 'bold':
|
2017-03-18 20:05:11 +09:00
|
|
|
return `<strong>${escape(token.bold)}</strong>`;
|
2017-03-18 01:16:32 +09:00
|
|
|
case 'url':
|
2017-06-14 01:24:11 +09:00
|
|
|
return `<mk-url href="${escape(token.content)}" target="_blank"></mk-url>`;
|
2017-03-18 01:16:32 +09:00
|
|
|
case 'link':
|
2017-03-18 20:05:11 +09:00
|
|
|
return `<a class="link" href="${escape(token.url)}" target="_blank" title="${escape(token.url)}">${escape(token.title)}</a>`;
|
2016-12-29 07:49:51 +09:00
|
|
|
case 'mention':
|
2017-11-23 05:43:00 +09:00
|
|
|
return `<a href="${_URL_ + '/' + escape(token.username)}" target="_blank" data-user-preview="${token.content}" ${me && me.username == token.username ? 'data-is-me' : ''}>${token.content}</a>`;
|
2016-12-29 07:49:51 +09:00
|
|
|
case 'hashtag': // TODO
|
2017-03-18 20:05:11 +09:00
|
|
|
return `<a>${escape(token.content)}</a>`;
|
2017-02-09 01:07:06 +09:00
|
|
|
case 'code':
|
2017-03-18 20:05:11 +09:00
|
|
|
return `<pre><code>${token.html}</code></pre>`;
|
2017-02-11 23:41:57 +09:00
|
|
|
case 'inline-code':
|
2017-03-18 20:05:11 +09:00
|
|
|
return `<code>${token.html}</code>`;
|
2017-03-01 05:33:27 +09:00
|
|
|
case 'emoji':
|
2017-05-31 16:19:30 +09:00
|
|
|
return pictograph.dic[token.emoji] || token.content;
|
2016-12-29 07:49:51 +09:00
|
|
|
}
|
|
|
|
}).join('');
|
2016-12-30 11:57:17 +09:00
|
|
|
|
2017-03-01 14:22:56 +09:00
|
|
|
// Remove needless whitespaces
|
2017-03-01 14:22:29 +09: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-09 01:07:06 +09:00
|
|
|
|
2016-12-30 11:57:17 +09:00
|
|
|
return text;
|
2017-03-01 14:19:59 +09:00
|
|
|
};
|