update hyperdown

This commit is contained in:
joyqi
2021-06-22 17:29:01 +08:00
parent 70068ed5a1
commit 98bf0a3b6c
3 changed files with 35 additions and 22 deletions
File diff suppressed because one or more lines are too long
+14 -8
View File
@@ -323,12 +323,12 @@
return _this.makeHolder(prefix + escaped);
};
})(this));
text = text.replace(/<(https?:\/\/.+)>/ig, (function(_this) {
text = text.replace(/<(https?:\/\/.+|(?:mailto:)?[_a-z0-9-\.\+]+@[_\w-]+\.[a-z]{2,})>/ig, (function(_this) {
return function() {
var link, matches, url;
matches = 1 <= arguments.length ? slice.call(arguments, 0) : [];
url = _this.cleanUrl(matches[1]);
link = _this.call('parseLink', matches[1]);
link = _this.call('parseLink', url);
return _this.makeHolder("<a href=\"" + url + "\">" + link + "</a>");
};
})(this));
@@ -404,14 +404,14 @@
};
})(this));
text = this.parseInlineCallback(text);
text = text.replace(/<([_a-z0-9-\.\+]+@[^@]+\.[a-z]{2,})>/ig, '<a href="mailto:$1">$1</a>');
if (enableAutoLink) {
text = text.replace(/(^|[^\"])(https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&\/=]*))($|[^\"])/g, (function(_this) {
text = text.replace(/(^|[^\"])(https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&\/=]*)|(?:mailto:)?[_a-z0-9-\.\+]+@[_\w-]+\.[a-z]{2,})($|[^\"])/g, (function(_this) {
return function() {
var link, matches;
var link, matches, url;
matches = 1 <= arguments.length ? slice.call(arguments, 0) : [];
link = _this.call('parseLink', matches[2]);
return matches[1] + "<a href=\"" + matches[2] + "\">" + link + "</a>" + matches[5];
url = _this.cleanUrl(matches[2]);
link = _this.call('parseLink', url);
return matches[1] + "<a href=\"" + link + "\">" + matches[2] + "</a>" + matches[5];
};
})(this));
}
@@ -1138,8 +1138,14 @@
};
Parser.prototype.cleanUrl = function(url) {
var matches;
url = url.replace(/["'<>\s]/g, '');
if ((url.match(/^\w+:/i)) && !(url.match(/^https?:/i))) {
if (!!(matches = url.match(/^(mailto:)?[_a-z0-9-\.\+]+@[_\w-]+\.[a-z]{2,}$/i))) {
if (matches[1] == null) {
url = 'mailto:' + url;
}
}
if ((url.match(/^\w+:/i)) && !(url.match(/^(https?|mailto):/i))) {
return '#';
}
return url;
+20 -13
View File
@@ -385,8 +385,8 @@ class HyperDown
"/(^|[^\\\])(`+)(.+?)\\2/",
function ($matches) use ($self) {
return $matches[1] . $self->makeHolder(
'<code>' . htmlspecialchars($matches[3]) . '</code>'
);
'<code>' . htmlspecialchars($matches[3]) . '</code>'
);
},
$text
);
@@ -396,8 +396,8 @@ class HyperDown
"/(^|[^\\\])(\\$+)(.+?)\\2/",
function ($matches) use ($self) {
return $matches[1] . $self->makeHolder(
$matches[2] . htmlspecialchars($matches[3]) . $matches[2]
);
$matches[2] . htmlspecialchars($matches[3]) . $matches[2]
);
},
$text
);
@@ -416,10 +416,10 @@ class HyperDown
// link
$text = preg_replace_callback(
"/<(https?:\/\/.+)>/i",
"/<(https?:\/\/.+|(?:mailto:)?[_a-z0-9-\.\+]+@[_\w-]+\.[a-z]{2,})>/i",
function ($matches) use ($self) {
$url = $self->cleanUrl($matches[1]);
$link = $self->call('parseLink', $matches[1]);
$link = $self->call('parseLink', $url);
return $self->makeHolder(
"<a href=\"{$url}\">{$link}</a>"
@@ -433,8 +433,8 @@ class HyperDown
"/<(\/?)([a-z0-9-]+)(\s+[^>]*)?>/i",
function ($matches) use ($self, $whiteList) {
if ($self->_html || false !== stripos(
'|' . $self->_commonWhiteList . '|' . $whiteList . '|', '|' . $matches[2] . '|'
)) {
'|' . $self->_commonWhiteList . '|' . $whiteList . '|', '|' . $matches[2] . '|'
)) {
return $self->makeHolder($matches[0]);
} else {
return $self->makeHolder(htmlspecialchars($matches[0]));
@@ -537,10 +537,11 @@ class HyperDown
// autolink url
if ($enableAutoLink) {
$text = preg_replace_callback(
"/(^|[^\"])(https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&\/=]*))($|[^\"])/",
"/(^|[^\"])(https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&\/=]*)|(?:mailto:)?[_a-z0-9-\.\+]+@[_\w-]+\.[a-z]{2,})($|[^\"])/",
function ($matches) use ($self) {
$link = $self->call('parseLink', $matches[2]);
return "{$matches[1]}<a href=\"{$matches[2]}\">{$link}</a>{$matches[5]}";
$url = $self->cleanUrl($matches[2]);
$link = $self->call('parseLink', $url);
return "{$matches[1]}<a href=\"{$link}\">{$matches[2]}</a>{$matches[5]}";
},
$text
);
@@ -1087,7 +1088,7 @@ class HyperDown
private function parseBlockMh($block, $key, $line, &$state, $lines)
{
if (preg_match("/^\s*((=|-){2,})\s*$/", $line, $matches)
&& ($block && $block[0] == "normal" && !preg_match("/^\s*$/", $lines[$block[2]]))) { // check if last line isn't empty
&& ($block && $block[0] == "normal" && !preg_match("/^\s*$/", $lines[$block[2]]))) { // check if last line isn't empty
if ($this->isBlock('normal')) {
$this->backBlock(1, 'mh', $matches[1][0] == '=' ? 1 : 2)
->setBlock($key)
@@ -1629,7 +1630,13 @@ class HyperDown
{
$url = preg_replace("/[\"'<>\s]/", '', $url);
if (preg_match("/^\w+:/i", $url) && !preg_match("/^https?:/i", $url)) {
if (preg_match("/^(mailto:)?[_a-z0-9-\.\+]+@[_\w-]+\.[a-z]{2,}$/i", $url, $matches)) {
if (empty($matches[1])) {
$url = 'mailto:' . $url;
}
}
if (preg_match("/^\w+:/i", $url) && !preg_match("/^(https?|mailto):/i", $url)) {
return '#';
}