Refactoring ✨
This commit is contained in:
parent
c20cd97be2
commit
1b2d080651
|
@ -2,16 +2,13 @@
|
||||||
* Bold
|
* Bold
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const regexp = /\*\*(.+?)\*\*/;
|
module.exports = text => {
|
||||||
|
const match = text.match(/^\*\*(.+?)\*\*/);
|
||||||
module.exports = {
|
if (!match) return null;
|
||||||
test: x => new RegExp('^' + regexp.source).test(x),
|
const bold = match[0];
|
||||||
parse: text => {
|
|
||||||
const bold = text.match(new RegExp('^' + regexp.source))[0];
|
|
||||||
return {
|
return {
|
||||||
type: 'bold',
|
type: 'bold',
|
||||||
content: bold,
|
content: bold,
|
||||||
bold: bold.substr(2, bold.length - 4)
|
bold: bold.substr(2, bold.length - 4)
|
||||||
};
|
};
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -2,19 +2,16 @@
|
||||||
* Code
|
* Code
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const regexp = /```([\s\S]+?)```/;
|
module.exports = text => {
|
||||||
|
const match = text.match(/^```([\s\S]+?)```/);
|
||||||
module.exports = {
|
if (!match) return null;
|
||||||
test: x => new RegExp('^' + regexp.source).test(x),
|
const code = match[0];
|
||||||
parse: text => {
|
|
||||||
const code = text.match(new RegExp('^' + regexp.source))[0];
|
|
||||||
return {
|
return {
|
||||||
type: 'code',
|
type: 'code',
|
||||||
content: code,
|
content: code,
|
||||||
code: code.substr(3, code.length - 6).trim(),
|
code: code.substr(3, code.length - 6).trim(),
|
||||||
codeHtml: genHtml(code.substr(3, code.length - 6).trim())
|
codeHtml: genHtml(code.substr(3, code.length - 6).trim())
|
||||||
};
|
};
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function escape(text) {
|
function escape(text) {
|
||||||
|
|
|
@ -2,11 +2,8 @@
|
||||||
* Hashtag
|
* Hashtag
|
||||||
*/
|
*/
|
||||||
|
|
||||||
module.exports = {
|
module.exports = (text, i) => {
|
||||||
test: (x, i) =>
|
if (!(/^\s#[^\s]+/.test(text) || (i == 0 && /^#[^\s]+/.test(text)))) return null;
|
||||||
/^\s#[^\s]+/.test(x) || (i == 0 && /^#[^\s]+/.test(x))
|
|
||||||
,
|
|
||||||
parse: text => {
|
|
||||||
const isHead = text[0] == '#';
|
const isHead = text[0] == '#';
|
||||||
const hashtag = text.match(/^\s?#[^\s]+/)[0];
|
const hashtag = text.match(/^\s?#[^\s]+/)[0];
|
||||||
const res = !isHead ? [{
|
const res = !isHead ? [{
|
||||||
|
@ -19,5 +16,4 @@ module.exports = {
|
||||||
hashtag: isHead ? hashtag.substr(1) : hashtag.substr(2)
|
hashtag: isHead ? hashtag.substr(1) : hashtag.substr(2)
|
||||||
});
|
});
|
||||||
return res;
|
return res;
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -2,16 +2,13 @@
|
||||||
* Mention
|
* Mention
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const regexp = /@[a-zA-Z0-9\-]+/;
|
module.exports = text => {
|
||||||
|
const match = text.match(/^@[a-zA-Z0-9\-]+/);
|
||||||
module.exports = {
|
if (!match) return null;
|
||||||
test: x => new RegExp('^' + regexp.source).test(x),
|
const mention = match[0];
|
||||||
parse: text => {
|
|
||||||
const mention = text.match(new RegExp('^' + regexp.source))[0];
|
|
||||||
return {
|
return {
|
||||||
type: 'mention',
|
type: 'mention',
|
||||||
content: mention,
|
content: mention,
|
||||||
username: mention.substr(1)
|
username: mention.substr(1)
|
||||||
};
|
};
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -2,15 +2,12 @@
|
||||||
* URL
|
* URL
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const regexp = /https?:\/\/[\w\/:%#@\$&\?!\(\)\[\]~\.=\+\-]+/;
|
module.exports = text => {
|
||||||
|
const match = text.match(/^https?:\/\/[\w\/:%#@\$&\?!\(\)\[\]~\.=\+\-]+/);
|
||||||
module.exports = {
|
if (!match) return null;
|
||||||
test: x => new RegExp('^' + regexp.source).test(x),
|
const link = match[0];
|
||||||
parse: text => {
|
|
||||||
const link = text.match(new RegExp('^' + regexp.source))[0];
|
|
||||||
return {
|
return {
|
||||||
type: 'link',
|
type: 'link',
|
||||||
content: link
|
content: link
|
||||||
};
|
};
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -30,8 +30,8 @@ function analyze(source) {
|
||||||
// パース
|
// パース
|
||||||
while (source != '') {
|
while (source != '') {
|
||||||
const parsed = elements.some(el => {
|
const parsed = elements.some(el => {
|
||||||
if (el.test(source, i)) {
|
let tokens = el(source, i);
|
||||||
let tokens = el.parse(source);
|
if (tokens) {
|
||||||
if (!Array.isArray(tokens)) {
|
if (!Array.isArray(tokens)) {
|
||||||
tokens = [tokens];
|
tokens = [tokens];
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue