diff --git a/src/common/text/elements/bold.js b/src/common/text/elements/bold.js index 41a01399d..ce2576445 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 0761a4c96..902504c9e 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 f04b78200..048dbd892 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 b58786fd1..e0fac4dd7 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 d02aef080..f350b707a 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 + }; }; diff --git a/src/common/text/index.js b/src/common/text/index.js index 636f0c468..d0ef8674b 100644 --- a/src/common/text/index.js +++ b/src/common/text/index.js @@ -30,8 +30,8 @@ function analyze(source) { // パース while (source != '') { const parsed = elements.some(el => { - if (el.test(source, i)) { - let tokens = el.parse(source); + let tokens = el(source, i); + if (tokens) { if (!Array.isArray(tokens)) { tokens = [tokens]; }