[MFM] Improve URL parsing

Fix #3368
This commit is contained in:
syuilo 2018-11-22 05:02:38 +09:00
parent 67df681a48
commit 8cbb961493
No known key found for this signature in database
GPG key ID: BDC4C49D06AB9D69
2 changed files with 46 additions and 10 deletions

View file

@ -245,11 +245,25 @@ const mfm = P.createLanguage({
const match = text.match(/^https?:\/\/[\w\/:%#@\$&\?!\(\)\[\]~\.,=\+\-]+/);
if (!match) return P.makeFailure(i, 'not a url');
let url = match[0];
const before = input[i - 1];
let pendingBracket = 0;
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(')') && before == '(') url = url.substr(0, url.lastIndexOf(')'));
if (url.endsWith(']') && before == '[') url = url.substr(0, url.lastIndexOf(']'));
return P.makeSuccess(i + url.length, url);
})
.map(x => makeNode('url', { url: x })),