From a68efc1e5e1f14afe899f04ee75f76493da15baa Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 9 Feb 2017 01:07:06 +0900 Subject: [PATCH] nanka iroiro --- src/common/text/elements/code.js | 17 ++++++++++ src/common/text/index.js | 3 +- src/web/app/common/scripts/text-compiler.js | 35 +++++++++++---------- src/web/app/desktop/tags/timeline-post.tag | 17 ++++++++-- 4 files changed, 52 insertions(+), 20 deletions(-) create mode 100644 src/common/text/elements/code.js diff --git a/src/common/text/elements/code.js b/src/common/text/elements/code.js new file mode 100644 index 0000000000..d046716ed6 --- /dev/null +++ b/src/common/text/elements/code.js @@ -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() + }; + } +}; diff --git a/src/common/text/index.js b/src/common/text/index.js index 973e7c5236..636f0c4681 100644 --- a/src/common/text/index.js +++ b/src/common/text/index.js @@ -6,7 +6,8 @@ const elements = [ require('./elements/bold'), require('./elements/url'), require('./elements/mention'), - require('./elements/hashtag') + require('./elements/hashtag'), + require('./elements/code') ]; function analyze(source) { diff --git a/src/web/app/common/scripts/text-compiler.js b/src/web/app/common/scripts/text-compiler.js index 8ea2361b87..ab322764f3 100644 --- a/src/web/app/common/scripts/text-compiler.js +++ b/src/web/app/common/scripts/text-compiler.js @@ -1,12 +1,18 @@ const riot = require('riot'); const nyaize = require('nyaize').default; -module.exports = function(tokens, shouldBreak, escape) { +const escape = function(text) { + return text + .replace(/>/g, '>') + .replace(//g, '>') - .replace(/' : ' '); - } else { - return token.content - .replace(/(\r\n|\n|\r)/g, shouldBreak ? '
' : ' '); - } + return escape(token.content) + .replace(/(\r\n|\n|\r)/g, shouldBreak ? '
' : ' '); case 'bold': - return '' + token.bold + ''; + return '' + escape(token.bold) + ''; case 'link': - return ''; + return ''; case 'mention': - return '' + token.content + ''; + return '' + token.content + ''; case 'hashtag': // TODO - return '' + token.content + ''; + return '' + escape(token.content) + ''; + case 'code': + return '
' + escape(token.code) + '
'; } }).join(''); + text = text.replace(/
/g, '
').replace(/<\/code><\/pre>
/g, '
'); + if (me && me.data && me.data.nya) { text = nyaize(text); } diff --git a/src/web/app/desktop/tags/timeline-post.tag b/src/web/app/desktop/tags/timeline-post.tag index 0105617e0d..7d6f537c8c 100644 --- a/src/web/app/desktop/tags/timeline-post.tag +++ b/src/web/app/desktop/tags/timeline-post.tag @@ -30,11 +30,11 @@
-
+
- +

DUMMY

RP:
@@ -228,6 +228,17 @@ font-style oblique 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 > img display block @@ -304,7 +315,7 @@ then @analyze @p._highlight else @analyze @p.text - @refs.text.innerHTML = if @p._highlight? + @refs.text.innerHTML = @refs.text.innerHTML.replace \

DUMMY

if @p._highlight? then @compile tokens, true, false else @compile tokens