Refactoring

This commit is contained in:
syuilo 2018-11-25 13:19:33 +09:00
parent 0367c37b0a
commit 0e49c11a4c
No known key found for this signature in database
GPG Key ID: BDC4C49D06AB9D69
1 changed files with 22 additions and 34 deletions

View File

@ -29,6 +29,26 @@ function makeNodeWithChildren(name: string, children: Node[], props?: any): Node
return _makeNode(name, children, props); return _makeNode(name, children, props);
} }
function getTrailingPosition(x: string): number {
let pendingBracket = 0;
const end = x.split('').findIndex(char => {
if (char == ')') {
if (pendingBracket > 0) {
pendingBracket--;
return false;
} else {
return true;
}
} else if (char == '(') {
pendingBracket++;
return false;
} else {
return false;
}
});
return end > 0 ? end : x.length;
}
const newline = P((input, i) => { const newline = P((input, i) => {
if (i == 0 || input[i] == '\n' || input[i - 1] == '\n') { if (i == 0 || input[i] == '\n' || input[i - 1] == '\n') {
return P.makeSuccess(i, null); return P.makeSuccess(i, null);
@ -113,23 +133,7 @@ const mfm = P.createLanguage({
const match = text.match(/^#([^\s\.,!\?#]+)/i); const match = text.match(/^#([^\s\.,!\?#]+)/i);
if (!match) return P.makeFailure(i, 'not a hashtag'); if (!match) return P.makeFailure(i, 'not a hashtag');
let hashtag = match[1]; let hashtag = match[1];
let pendingBracket = 0; hashtag = hashtag.substr(0, getTrailingPosition(hashtag));
const end = hashtag.split('').findIndex(char => {
if (char == ')') {
if (pendingBracket > 0) {
pendingBracket--;
return false;
} else {
return true;
}
} else if (char == '(') {
pendingBracket++;
return false;
} else {
return false;
}
});
if (end > 0) hashtag = hashtag.substr(0, end);
if (hashtag.match(/^[0-9]+$/)) return P.makeFailure(i, 'not a hashtag'); if (hashtag.match(/^[0-9]+$/)) return P.makeFailure(i, 'not a hashtag');
if (!['\n', ' ', '(', null, undefined].includes(input[i - 1])) return P.makeFailure(i, 'require space before "#"'); if (!['\n', ' ', '(', null, undefined].includes(input[i - 1])) return P.makeFailure(i, 'require space before "#"');
return P.makeSuccess(i + ('#' + hashtag).length, makeNode('hashtag', { hashtag: hashtag })); return P.makeSuccess(i + ('#' + hashtag).length, makeNode('hashtag', { hashtag: hashtag }));
@ -264,23 +268,7 @@ const mfm = P.createLanguage({
const match = text.match(/^https?:\/\/[\w\/:%#@\$&\?!\(\)\[\]~\.,=\+\-]+/); const match = text.match(/^https?:\/\/[\w\/:%#@\$&\?!\(\)\[\]~\.,=\+\-]+/);
if (!match) return P.makeFailure(i, 'not a url'); if (!match) return P.makeFailure(i, 'not a url');
let url = match[0]; let url = match[0];
let pendingBracket = 0; url = url.substr(0, getTrailingPosition(url));
const end = url.split('').findIndex(char => {
if (char == ')') {
if (pendingBracket > 0) {
pendingBracket--;
return false;
} else {
return true;
}
} else if (char == '(') {
pendingBracket++;
return false;
} else {
return false;
}
});
if (end > 0) url = url.substr(0, end);
if (url.endsWith('.')) url = url.substr(0, url.lastIndexOf('.')); if (url.endsWith('.')) url = url.substr(0, url.lastIndexOf('.'));
if (url.endsWith(',')) url = url.substr(0, url.lastIndexOf(',')); if (url.endsWith(',')) url = url.substr(0, url.lastIndexOf(','));
return P.makeSuccess(i + url.length, url); return P.makeSuccess(i + url.length, url);