diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2017-02-11 02:32:00 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2017-02-11 02:32:00 +0900 |
| commit | 1b2d0806511613077e36a707f24cae215bfce03f (patch) | |
| tree | 1fd3acd227546e5faa7b8ed805e68178bd1e5903 /src/common/text/elements | |
| parent | Remove variables tracking system to more simply implementation (diff) | |
| download | sharkey-1b2d0806511613077e36a707f24cae215bfce03f.tar.gz sharkey-1b2d0806511613077e36a707f24cae215bfce03f.tar.bz2 sharkey-1b2d0806511613077e36a707f24cae215bfce03f.zip | |
Refactoring :sparkles:
Diffstat (limited to 'src/common/text/elements')
| -rw-r--r-- | src/common/text/elements/bold.js | 21 | ||||
| -rw-r--r-- | src/common/text/elements/code.js | 23 | ||||
| -rw-r--r-- | src/common/text/elements/hashtag.js | 32 | ||||
| -rw-r--r-- | src/common/text/elements/mention.js | 21 | ||||
| -rw-r--r-- | src/common/text/elements/url.js | 19 |
5 files changed, 50 insertions, 66 deletions
diff --git a/src/common/text/elements/bold.js b/src/common/text/elements/bold.js index 41a01399dd..ce25764457 100644 --- a/src/common/text/elements/bold.js +++ b/src/common/text/elements/bold.js @@ -2,16 +2,13 @@ * Bold */ -const regexp = /\*\*(.+?)\*\*/; - -module.exports = { - test: x => new RegExp('^' + regexp.source).test(x), - parse: text => { - const bold = text.match(new RegExp('^' + regexp.source))[0]; - return { - type: 'bold', - content: bold, - bold: bold.substr(2, bold.length - 4) - }; - } +module.exports = text => { + const match = text.match(/^\*\*(.+?)\*\*/); + if (!match) return null; + const bold = match[0]; + return { + type: 'bold', + content: bold, + bold: bold.substr(2, bold.length - 4) + }; }; diff --git a/src/common/text/elements/code.js b/src/common/text/elements/code.js index 0761a4c960..902504c9ec 100644 --- a/src/common/text/elements/code.js +++ b/src/common/text/elements/code.js @@ -2,19 +2,16 @@ * 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(), - codeHtml: genHtml(code.substr(3, code.length - 6).trim()) - }; - } +module.exports = text => { + const match = text.match(/^```([\s\S]+?)```/); + if (!match) return null; + const code = match[0]; + return { + type: 'code', + content: code, + code: code.substr(3, code.length - 6).trim(), + codeHtml: genHtml(code.substr(3, code.length - 6).trim()) + }; }; function escape(text) { diff --git a/src/common/text/elements/hashtag.js b/src/common/text/elements/hashtag.js index f04b782007..048dbd8929 100644 --- a/src/common/text/elements/hashtag.js +++ b/src/common/text/elements/hashtag.js @@ -2,22 +2,18 @@ * Hashtag */ -module.exports = { - test: (x, i) => - /^\s#[^\s]+/.test(x) || (i == 0 && /^#[^\s]+/.test(x)) - , - parse: text => { - const isHead = text[0] == '#'; - const hashtag = text.match(/^\s?#[^\s]+/)[0]; - const res = !isHead ? [{ - type: 'text', - content: text[0] - }] : []; - res.push({ - type: 'hashtag', - content: isHead ? hashtag : hashtag.substr(1), - hashtag: isHead ? hashtag.substr(1) : hashtag.substr(2) - }); - return res; - } +module.exports = (text, i) => { + if (!(/^\s#[^\s]+/.test(text) || (i == 0 && /^#[^\s]+/.test(text)))) return null; + const isHead = text[0] == '#'; + const hashtag = text.match(/^\s?#[^\s]+/)[0]; + const res = !isHead ? [{ + type: 'text', + content: text[0] + }] : []; + res.push({ + type: 'hashtag', + content: isHead ? hashtag : hashtag.substr(1), + hashtag: isHead ? hashtag.substr(1) : hashtag.substr(2) + }); + return res; }; diff --git a/src/common/text/elements/mention.js b/src/common/text/elements/mention.js index b58786fd1e..e0fac4dd76 100644 --- a/src/common/text/elements/mention.js +++ b/src/common/text/elements/mention.js @@ -2,16 +2,13 @@ * Mention */ -const regexp = /@[a-zA-Z0-9\-]+/; - -module.exports = { - test: x => new RegExp('^' + regexp.source).test(x), - parse: text => { - const mention = text.match(new RegExp('^' + regexp.source))[0]; - return { - type: 'mention', - content: mention, - username: mention.substr(1) - }; - } +module.exports = text => { + const match = text.match(/^@[a-zA-Z0-9\-]+/); + if (!match) return null; + const mention = match[0]; + return { + type: 'mention', + content: mention, + username: mention.substr(1) + }; }; diff --git a/src/common/text/elements/url.js b/src/common/text/elements/url.js index d02aef0800..f350b707ac 100644 --- a/src/common/text/elements/url.js +++ b/src/common/text/elements/url.js @@ -2,15 +2,12 @@ * URL */ -const regexp = /https?:\/\/[\w\/:%#@\$&\?!\(\)\[\]~\.=\+\-]+/; - -module.exports = { - test: x => new RegExp('^' + regexp.source).test(x), - parse: text => { - const link = text.match(new RegExp('^' + regexp.source))[0]; - return { - type: 'link', - content: link - }; - } +module.exports = text => { + const match = text.match(/^https?:\/\/[\w\/:%#@\$&\?!\(\)\[\]~\.=\+\-]+/); + if (!match) return null; + const link = match[0]; + return { + type: 'link', + content: link + }; }; |