forked from FoundKeyGang/FoundKey
Refactoring ✨
This commit is contained in:
parent
c20cd97be2
commit
1b2d080651
6 changed files with 52 additions and 68 deletions
|
@ -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 => {
|
return {
|
||||||
const bold = text.match(new RegExp('^' + regexp.source))[0];
|
type: 'bold',
|
||||||
return {
|
content: bold,
|
||||||
type: 'bold',
|
bold: bold.substr(2, bold.length - 4)
|
||||||
content: bold,
|
};
|
||||||
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 => {
|
return {
|
||||||
const code = text.match(new RegExp('^' + regexp.source))[0];
|
type: 'code',
|
||||||
return {
|
content: code,
|
||||||
type: 'code',
|
code: code.substr(3, code.length - 6).trim(),
|
||||||
content: code,
|
codeHtml: genHtml(code.substr(3, code.length - 6).trim())
|
||||||
code: code.substr(3, code.length - 6).trim(),
|
};
|
||||||
codeHtml: genHtml(code.substr(3, code.length - 6).trim())
|
|
||||||
};
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function escape(text) {
|
function escape(text) {
|
||||||
|
|
|
@ -2,22 +2,18 @@
|
||||||
* 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))
|
const isHead = text[0] == '#';
|
||||||
,
|
const hashtag = text.match(/^\s?#[^\s]+/)[0];
|
||||||
parse: text => {
|
const res = !isHead ? [{
|
||||||
const isHead = text[0] == '#';
|
type: 'text',
|
||||||
const hashtag = text.match(/^\s?#[^\s]+/)[0];
|
content: text[0]
|
||||||
const res = !isHead ? [{
|
}] : [];
|
||||||
type: 'text',
|
res.push({
|
||||||
content: text[0]
|
type: 'hashtag',
|
||||||
}] : [];
|
content: isHead ? hashtag : hashtag.substr(1),
|
||||||
res.push({
|
hashtag: isHead ? hashtag.substr(1) : hashtag.substr(2)
|
||||||
type: 'hashtag',
|
});
|
||||||
content: isHead ? hashtag : hashtag.substr(1),
|
return res;
|
||||||
hashtag: isHead ? hashtag.substr(1) : hashtag.substr(2)
|
|
||||||
});
|
|
||||||
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 => {
|
return {
|
||||||
const mention = text.match(new RegExp('^' + regexp.source))[0];
|
type: 'mention',
|
||||||
return {
|
content: mention,
|
||||||
type: 'mention',
|
username: mention.substr(1)
|
||||||
content: mention,
|
};
|
||||||
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 => {
|
return {
|
||||||
const link = text.match(new RegExp('^' + regexp.source))[0];
|
type: 'link',
|
||||||
return {
|
content: link
|
||||||
type: '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 a new issue