Skip to content

Commit aff0aa4

Browse files
authored
relative baseUrl (#1526)
relative baseUrl
2 parents f35c3e6 + d620f3c commit aff0aa4

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

lib/marked.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -1426,11 +1426,18 @@ function resolveUrl(base, href) {
14261426
}
14271427
}
14281428
base = baseUrls[' ' + base];
1429+
var relativeBase = base.indexOf(':') === -1;
14291430

14301431
if (href.slice(0, 2) === '//') {
1431-
return base.replace(/:[\s\S]*/, ':') + href;
1432+
if (relativeBase) {
1433+
return href;
1434+
}
1435+
return base.replace(/^([^:]+:)[\s\S]*$/, '$1') + href;
14321436
} else if (href.charAt(0) === '/') {
1433-
return base.replace(/(:\/*[^/]*)[\s\S]*/, '$1') + href;
1437+
if (relativeBase) {
1438+
return href;
1439+
}
1440+
return base.replace(/^([^:]+:\/*[^/]*)[\s\S]*$/, '$1') + href;
14341441
} else {
14351442
return base + href;
14361443
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<h1 id="absolutization-of-rfc-3986-uris">Absolutization of RFC 3986 URIs</h1>
2+
3+
<h2 id="absolute-uri">Absolute URI</h2>
4+
5+
<p><a href="http://example.com/"><img src="http://example.com/logo" alt="section 4.3"></a></p>
6+
7+
<h2 id="network-path-reference">Network-path reference</h2>
8+
9+
<p><a href="//example.com/"><img src="//example.com/logo" alt="section 4.2"></a></p>
10+
11+
<h2 id="absolute-path">Absolute path</h2>
12+
13+
<p><a href="/path/to/content"><img src="/path/to/img" alt="section 4.2"></a></p>
14+
15+
<h2 id="relative-path">Relative path</h2>
16+
17+
<p><a href="/base/content"><img src="/base/img" alt="section 4.2"></a></p>
18+
19+
<h2 id="dot-relative-path">Dot-relative path</h2>
20+
21+
<p><a href="/base/./content"><img src="/base/./img" alt="section 3.3"></a></p>
22+
23+
<p><a href="/base/../content"><img src="/base/../img" alt="section 3.3"></a></p>
24+
25+
<h2 id="same-document-query">Same-document query</h2>
26+
27+
<p><a href="?"><img src="?type=image" alt="section 4.4"></a></p>
28+
29+
<h2 id="same-document-fragment">Same-document fragment</h2>
30+
31+
<p><a href="#"><img src="#img" alt="section 4.4"></a></p>
32+
33+
<h2 id="empty">Empty</h2>
34+
35+
<p><a href="">section 4.2</a></p>

test/specs/new/relative_base_urls.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
baseUrl: "/base/"
3+
---
4+
# Absolutization of RFC 3986 URIs
5+
6+
## Absolute URI
7+
[![section 4.3](http://example.com/logo)](http://example.com/)
8+
9+
## Network-path reference
10+
[![section 4.2](//example.com/logo)](//example.com/)
11+
12+
## Absolute path
13+
[![section 4.2](/path/to/img)](/path/to/content)
14+
15+
## Relative path
16+
[![section 4.2](img)](content)
17+
18+
## Dot-relative path
19+
[![section 3.3](./img)](./content)
20+
21+
[![section 3.3](../img)](../content)
22+
23+
## Same-document query
24+
[![section 4.4](?type=image)](?)
25+
26+
## Same-document fragment
27+
[![section 4.4](#img)](#)
28+
29+
## Empty
30+
[section 4.2]()

0 commit comments

Comments
 (0)