Markdown parser, done right. 100% CommonMark support, extensions, syntax plugins & high speed
https://markdown-it.github.io/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
1.6 KiB
78 lines
1.6 KiB
10 years ago
|
// Parse link destination
|
||
|
//
|
||
|
|
||
1 year ago
|
import { unescapeAll } from '../common/utils.mjs';
|
||
10 years ago
|
|
||
1 year ago
|
export default function parseLinkDestination(str, start, max) {
|
||
10 years ago
|
var code, level,
|
||
3 years ago
|
pos = start,
|
||
10 years ago
|
result = {
|
||
|
ok: false,
|
||
|
pos: 0,
|
||
|
lines: 0,
|
||
|
str: ''
|
||
|
};
|
||
|
|
||
|
if (str.charCodeAt(pos) === 0x3C /* < */) {
|
||
10 years ago
|
pos++;
|
||
|
while (pos < max) {
|
||
10 years ago
|
code = str.charCodeAt(pos);
|
||
6 years ago
|
if (code === 0x0A /* \n */) { return result; }
|
||
4 years ago
|
if (code === 0x3C /* < */) { return result; }
|
||
10 years ago
|
if (code === 0x3E /* > */) {
|
||
10 years ago
|
result.pos = pos + 1;
|
||
10 years ago
|
result.str = unescapeAll(str.slice(start + 1, pos));
|
||
10 years ago
|
result.ok = true;
|
||
|
return result;
|
||
10 years ago
|
}
|
||
|
if (code === 0x5C /* \ */ && pos + 1 < max) {
|
||
|
pos += 2;
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
pos++;
|
||
|
}
|
||
|
|
||
|
// no closing '>'
|
||
10 years ago
|
return result;
|
||
10 years ago
|
}
|
||
|
|
||
|
// this should be ... } else { ... branch
|
||
|
|
||
|
level = 0;
|
||
|
while (pos < max) {
|
||
10 years ago
|
code = str.charCodeAt(pos);
|
||
10 years ago
|
|
||
|
if (code === 0x20) { break; }
|
||
|
|
||
|
// ascii control characters
|
||
|
if (code < 0x20 || code === 0x7F) { break; }
|
||
|
|
||
|
if (code === 0x5C /* \ */ && pos + 1 < max) {
|
||
4 years ago
|
if (str.charCodeAt(pos + 1) === 0x20) { break; }
|
||
10 years ago
|
pos += 2;
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
if (code === 0x28 /* ( */) {
|
||
|
level++;
|
||
4 years ago
|
if (level > 32) { return result; }
|
||
10 years ago
|
}
|
||
|
|
||
|
if (code === 0x29 /* ) */) {
|
||
7 years ago
|
if (level === 0) { break; }
|
||
10 years ago
|
level--;
|
||
|
}
|
||
|
|
||
|
pos++;
|
||
|
}
|
||
|
|
||
10 years ago
|
if (start === pos) { return result; }
|
||
7 years ago
|
if (level !== 0) { return result; }
|
||
10 years ago
|
|
||
10 years ago
|
result.str = unescapeAll(str.slice(start, pos));
|
||
10 years ago
|
result.pos = pos;
|
||
|
result.ok = true;
|
||
|
return result;
|
||
1 year ago
|
}
|