1
1
mirror of https://github.com/kokonect-link/cherrypick synced 2024-11-01 15:45:58 +09:00

Refactoring

This commit is contained in:
syuilo 2017-02-11 02:32:00 +09:00
parent c20cd97be2
commit 1b2d080651
6 changed files with 52 additions and 68 deletions

View File

@ -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)
};
};

View File

@ -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) {

View File

@ -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;
};

View File

@ -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)
};
};

View File

@ -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
};
};

View File

@ -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];
}