Skip to content

Commit

Permalink
Relative baseUrl, marked#1526
Browse files Browse the repository at this point in the history
  • Loading branch information
yahtnif committed Nov 16, 2019
1 parent 655de2e commit b799c51
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
2 changes: 1 addition & 1 deletion __tests__/base/heading_id
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ headerId: true



<h2 id="heading-with-a-link"><code>Heading</code> with a <a href="http://example.com">link</a></h2>
<h2 id="heading-with-a-link"><code>Heading</code> with a <a href="http://example.com">link</a></h2>
52 changes: 52 additions & 0 deletions __tests__/option/baseUrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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]()



<h1>Absolutization of RFC 3986 URIs</h1>
<h2>Absolute URI</h2>
<p><a href="http://example.com/"><img src="http://example.com/logo" alt="section 4.3"></a></p>
<h2>Network-path reference</h2>
<p><a href="//example.com/"><img src="//example.com/logo" alt="section 4.2"></a></p>
<h2>Absolute path</h2>
<p><a href="/path/to/content"><img src="/path/to/img" alt="section 4.2"></a></p>
<h2>Relative path</h2>
<p><a href="/base/content"><img src="/base/img" alt="section 4.2"></a></p>
<h2>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>Same-document query</h2>
<p><a href="?"><img src="?type=image" alt="section 4.4"></a></p>
<h2>Same-document fragment</h2>
<p><a href="#"><img src="#img" alt="section 4.4"></a></p>
<h2>Empty</h2>
<p><a href="">section 4.2</a></p>
11 changes: 9 additions & 2 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,18 @@ export function resolveUrl(base: string, href: string): string {
}

base = baseUrls[baseUrlsKey];
const 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

0 comments on commit b799c51

Please sign in to comment.