diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2017-11-13 18:05:35 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2017-11-13 18:05:35 +0900 |
| commit | bc9a8283c66d7588f931d4b802f7ab1fa7aa3226 (patch) | |
| tree | cac529d136737c95b1656564756da8c4ae84df32 /src/web/app/common/scripts/text-compiler.ts | |
| parent | :v: (diff) | |
| download | sharkey-bc9a8283c66d7588f931d4b802f7ab1fa7aa3226.tar.gz sharkey-bc9a8283c66d7588f931d4b802f7ab1fa7aa3226.tar.bz2 sharkey-bc9a8283c66d7588f931d4b802f7ab1fa7aa3226.zip | |
なんかもうめっちゃ変えた
Diffstat (limited to 'src/web/app/common/scripts/text-compiler.ts')
| -rw-r--r-- | src/web/app/common/scripts/text-compiler.ts | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/web/app/common/scripts/text-compiler.ts b/src/web/app/common/scripts/text-compiler.ts new file mode 100644 index 0000000000..8c65d6a068 --- /dev/null +++ b/src/web/app/common/scripts/text-compiler.ts @@ -0,0 +1,47 @@ +import * as riot from 'riot'; +import * as pictograph from 'pictograph'; +import CONFIG from './config'; + +const escape = text => + text + .replace(/>/g, '>') + .replace(/</g, '<'); + +export default (tokens, shouldBreak) => { + if (shouldBreak == null) { + shouldBreak = true; + } + + const me = (riot as any).mixin('i').me; + + let text = tokens.map(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 'url': + return `<mk-url href="${escape(token.content)}" target="_blank"></mk-url>`; + case 'link': + return `<a class="link" href="${escape(token.url)}" target="_blank" title="${escape(token.url)}">${escape(token.title)}</a>`; + case 'mention': + 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>`; + case 'hashtag': // TODO + return `<a>${escape(token.content)}</a>`; + case 'code': + return `<pre><code>${token.html}</code></pre>`; + case 'inline-code': + return `<code>${token.html}</code>`; + case 'emoji': + return pictograph.dic[token.emoji] || token.content; + } + }).join(''); + + // Remove needless whitespaces + text = text + .replace(/ <code>/g, '<code>').replace(/<\/code> /g, '</code>') + .replace(/<br><code><pre>/g, '<code><pre>').replace(/<\/code><\/pre><br>/g, '</code></pre>'); + + return text; +}; |