From b35361a6af678a58b8bd59235ed627272fda9fbc Mon Sep 17 00:00:00 2001 From: Tony Brix Date: Wed, 17 Jul 2019 21:59:20 -0500 Subject: [PATCH] baseUrl can be relative --- lib/marked.js | 11 ++++++-- test/specs/new/relative_base_urls.html | 35 ++++++++++++++++++++++++++ test/specs/new/relative_base_urls.md | 30 ++++++++++++++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 test/specs/new/relative_base_urls.html create mode 100644 test/specs/new/relative_base_urls.md diff --git a/lib/marked.js b/lib/marked.js index 196b2b9259..49a4153a4b 100644 --- a/lib/marked.js +++ b/lib/marked.js @@ -1426,11 +1426,18 @@ function resolveUrl(base, href) { } } base = baseUrls[' ' + base]; + var relativeBase = base.indexOf(':') === -1; if (href.slice(0, 2) === '//') { - return base.replace(/:[\s\S]*/, ':') + href; + if (relativeBase) { + return href; + } + return base.replace(/^([^:]+:)[\s\S]*$/, '$1') + href; } else if (href.charAt(0) === '/') { - return base.replace(/(:\/*[^/]*)[\s\S]*/, '$1') + href; + if (relativeBase) { + return href; + } + return base.replace(/^([^:]+:\/*[^/]*)[\s\S]*$/, '$1') + href; } else { return base + href; } diff --git a/test/specs/new/relative_base_urls.html b/test/specs/new/relative_base_urls.html new file mode 100644 index 0000000000..6f94843f56 --- /dev/null +++ b/test/specs/new/relative_base_urls.html @@ -0,0 +1,35 @@ +

Absolutization of RFC 3986 URIs

+ +

Absolute URI

+ +

section 4.3

+ +

Network-path reference

+ +

section 4.2

+ +

Absolute path

+ +

section 4.2

+ +

Relative path

+ +

section 4.2

+ +

Dot-relative path

+ +

section 3.3

+ +

section 3.3

+ +

Same-document query

+ +

section 4.4

+ +

Same-document fragment

+ +

section 4.4

+ +

Empty

+ +

section 4.2

diff --git a/test/specs/new/relative_base_urls.md b/test/specs/new/relative_base_urls.md new file mode 100644 index 0000000000..1f59f1feb5 --- /dev/null +++ b/test/specs/new/relative_base_urls.md @@ -0,0 +1,30 @@ +--- +baseUrl: "/base/" +--- +# Absolutization of RFC 3986 URIs + +## Absolute URI +[![section 4.3](http://example.com/logo)](http://example.com/) + +## Network-path reference +[![section 4.2](//example.com/logo)](//example.com/) + +## Absolute path +[![section 4.2](/path/to/img)](/path/to/content) + +## Relative path +[![section 4.2](img)](content) + +## Dot-relative path +[![section 3.3](./img)](./content) + +[![section 3.3](../img)](../content) + +## Same-document query +[![section 4.4](?type=image)](?) + +## Same-document fragment +[![section 4.4](#img)](#) + +## Empty +[section 4.2]()