Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/marked.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
35 changes: 35 additions & 0 deletions test/specs/new/relative_base_urls.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<h1 id="absolutization-of-rfc-3986-uris">Absolutization of RFC 3986 URIs</h1>

<h2 id="absolute-uri">Absolute URI</h2>

<p><a href="http://example.com/"><img src="http://example.com/logo" alt="section 4.3"></a></p>

<h2 id="network-path-reference">Network-path reference</h2>

<p><a href="//example.com/"><img src="//example.com/logo" alt="section 4.2"></a></p>

<h2 id="absolute-path">Absolute path</h2>

<p><a href="/path/to/content"><img src="/path/to/img" alt="section 4.2"></a></p>

<h2 id="relative-path">Relative path</h2>

<p><a href="/base/content"><img src="/base/img" alt="section 4.2"></a></p>

<h2 id="dot-relative-path">Dot-relative path</h2>

<p><a href="/base/./content"><img src="/base/./img" alt="section 3.3"></a></p>

<p><a href="/base/../content"><img src="/base/../img" alt="section 3.3"></a></p>

<h2 id="same-document-query">Same-document query</h2>

<p><a href="?"><img src="?type=image" alt="section 4.4"></a></p>

<h2 id="same-document-fragment">Same-document fragment</h2>

<p><a href="#"><img src="#img" alt="section 4.4"></a></p>

<h2 id="empty">Empty</h2>

<p><a href="">section 4.2</a></p>
30 changes: 30 additions & 0 deletions test/specs/new/relative_base_urls.md
Original file line number Diff line number Diff line change
@@ -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]()