Skip to content

Commit cf92230

Browse files
committed
Add support for <base> element
Closes GH-30.
1 parent 8429b2d commit cf92230

File tree

10 files changed

+77
-3
lines changed

10 files changed

+77
-3
lines changed

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ var handlers = require('./lib/handlers')
1010
function toMDAST(tree, options) {
1111
var settings = options || {}
1212

13+
h.baseFound = false
14+
h.frozenBaseURL = null
15+
1316
h.handlers = xtend(handlers, settings.handlers || {})
1417
h.augment = augment
1518
h.document = settings.document

lib/handlers/base.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict'
2+
3+
module.exports = base
4+
5+
function base(h, node) {
6+
if (!h.baseFound) {
7+
h.frozenBaseURL = node.properties.href || null
8+
h.baseFound = true
9+
}
10+
}

lib/handlers/image.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
module.exports = image
44

5+
var resolve = require('../util/resolve')
6+
57
function image(h, node) {
68
var props = {
7-
url: node.properties.src || '',
9+
url: resolve(h, node.properties.src),
810
title: node.properties.title || null,
911
alt: node.properties.alt || null
1012
}

lib/handlers/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ exports.command = ignore
1919
exports.content = ignore
2020
exports.datalist = ignore
2121
exports.dialog = ignore
22-
exports.head = ignore
2322
exports.input = ignore
2423
exports.keygen = ignore
2524
exports.link = ignore
@@ -28,6 +27,7 @@ exports.math = ignore
2827
exports.menu = ignore
2928
exports.menuitem = ignore
3029
exports.meta = ignore
30+
exports.noscript = ignore
3131
exports.optgroup = ignore
3232
exports.option = ignore
3333
exports.script = ignore
@@ -79,6 +79,7 @@ exports.main = wrapped
7979
exports.nav = wrapped
8080
exports.section = wrapped
8181

82+
exports.base = require('./base')
8283
exports.ol = exports.ul = exports.dir = require('./list')
8384
exports.table = require('./table')
8485
exports.tr = require('./table-row')

lib/handlers/link.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
module.exports = link
44

55
var all = require('../all')
6+
var resolve = require('../util/resolve')
67

78
function link(h, node) {
89
var props = {
9-
url: node.properties.href || '',
10+
url: resolve(h, node.properties.href),
1011
title: node.properties.title || null
1112
}
1213

lib/util/resolve.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict'
2+
3+
module.exports = resolve
4+
5+
function resolve(h, url) {
6+
if (url === null || url === undefined) {
7+
return ''
8+
}
9+
10+
return h.frozenBaseURL ? String(new URL(url, h.frozenBaseURL)) : url
11+
}

test/fixtures/base-invalid/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<html>
2+
<head>
3+
<title>Document title</title>
4+
<base target="_blank">
5+
<base href="http://some-other-website.com">
6+
</head>
7+
<body>
8+
<p><a href="http://some-other-url.com/relative.html">example</a></p>
9+
<p><a href="relative.html">example</a></p>
10+
<p><a href="./relative.html">example</a></p>
11+
<p><a href="../relative.html">example</a></p>
12+
<p><a>example</a></p>
13+
</body>
14+
</html>

test/fixtures/base-invalid/index.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[example](http://some-other-url.com/relative.html)
2+
3+
[example](relative.html)
4+
5+
[example](./relative.html)
6+
7+
[example](../relative.html)
8+
9+
[example](<>)

test/fixtures/base/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<html>
2+
<head>
3+
<title>Document title</title>
4+
<base href="http://example.com">
5+
<base href="http://some-other-website.com">
6+
</head>
7+
<body>
8+
<p><a href="http://some-other-url.com/relative.html">example</a></p>
9+
<p><a href="relative.html">example</a></p>
10+
<p><a href="./relative.html">example</a></p>
11+
<p><a href="../relative.html">example</a></p>
12+
<p><a>example</a></p>
13+
</body>
14+
</html>

test/fixtures/base/index.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[example](http://some-other-url.com/relative.html)
2+
3+
[example](http://example.com/relative.html)
4+
5+
[example](http://example.com/relative.html)
6+
7+
[example](http://example.com/relative.html)
8+
9+
[example](<>)

0 commit comments

Comments
 (0)