Skip to content

Commit

Permalink
relative baseUrl (#1526)
Browse files Browse the repository at this point in the history
relative baseUrl
  • Loading branch information
UziTech committed Jul 19, 2019
2 parents f35c3e6 + d620f3c commit aff0aa4
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/marked.js
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
@@ -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
@@ -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]()

1 comment on commit aff0aa4

@vercel
Copy link

@vercel vercel bot commented on aff0aa4 Jul 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.