nanka iroiro

This commit is contained in:
syuilo 2017-02-09 01:07:06 +09:00
parent 5ad6877090
commit a68efc1e5e
4 changed files with 52 additions and 20 deletions

View File

@ -0,0 +1,17 @@
/**
* Code
*/
const regexp = /```([\s\S]+?)```/;
module.exports = {
test: x => new RegExp('^' + regexp.source).test(x),
parse: text => {
const code = text.match(new RegExp('^' + regexp.source))[0];
return {
type: 'code',
content: code,
code: code.substr(3, code.length - 6).trim()
};
}
};

View File

@ -6,7 +6,8 @@ const elements = [
require('./elements/bold'), require('./elements/bold'),
require('./elements/url'), require('./elements/url'),
require('./elements/mention'), require('./elements/mention'),
require('./elements/hashtag') require('./elements/hashtag'),
require('./elements/code')
]; ];
function analyze(source) { function analyze(source) {

View File

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

View File

@ -30,11 +30,11 @@
</div> </div>
</header> </header>
<div class="body"> <div class="body">
<div class="text"> <div class="text" ref="text">
<a class="reply" if={ p.reply_to }> <a class="reply" if={ p.reply_to }>
<i class="fa fa-reply"></i> <i class="fa fa-reply"></i>
</a> </a>
<span ref="text"></span> <p>DUMMY</p>
<a class="quote" if={ p.repost != null }>RP:</a> <a class="quote" if={ p.repost != null }>RP:</a>
</div> </div>
<div class="media" if={ p.media }> <div class="media" if={ p.media }>
@ -228,6 +228,17 @@
font-style oblique font-style oblique
color #a0bf46 color #a0bf46
pre
padding 16px
overflow auto
font-size 80%
color #525252
background #f8f8f8
border-radius 2px
> code
font-family Consolas, 'Courier New', Courier, Monaco, monospace
> .media > .media
> img > img
display block display block
@ -304,7 +315,7 @@
then @analyze @p._highlight then @analyze @p._highlight
else @analyze @p.text else @analyze @p.text
@refs.text.innerHTML = if @p._highlight? @refs.text.innerHTML = @refs.text.innerHTML.replace \<p>DUMMY</p> if @p._highlight?
then @compile tokens, true, false then @compile tokens, true, false
else @compile tokens else @compile tokens