Fixes #6
This commit is contained in:
5
_src/AbsoluteUrl.js
Normal file
5
_src/AbsoluteUrl.js
Normal file
@ -0,0 +1,5 @@
|
||||
const { URL } = require("url");
|
||||
|
||||
module.exports = function(url, base) {
|
||||
return (new URL(url, base)).toString()
|
||||
};
|
20
_src/HtmlToAbsoluteUrls.js
Normal file
20
_src/HtmlToAbsoluteUrls.js
Normal file
@ -0,0 +1,20 @@
|
||||
const posthtml = require('posthtml');
|
||||
const urls = require('posthtml-urls')
|
||||
const absoluteUrl = require("./AbsoluteUrl");
|
||||
|
||||
module.exports = function(htmlContent, base) {
|
||||
let options = {
|
||||
eachURL: function(url, attr, element) {
|
||||
// #anchor in-page
|
||||
if( url.trim().indexOf("#") === 0 ) {
|
||||
return url;
|
||||
}
|
||||
|
||||
return absoluteUrl(url, base);
|
||||
}
|
||||
};
|
||||
|
||||
let modifier = posthtml().use(urls(options));
|
||||
|
||||
return modifier.process(htmlContent);
|
||||
};
|
9
_src/test/HtmlToAbsoluteUrlsTest.js
Normal file
9
_src/test/HtmlToAbsoluteUrlsTest.js
Normal file
@ -0,0 +1,9 @@
|
||||
import test from "ava";
|
||||
import htmlToAbsUrls from "../HtmlToAbsoluteUrls.js";
|
||||
|
||||
test("Changes a link href", async t => {
|
||||
t.is((await htmlToAbsUrls(`<a href="#testanchor">Hello</a>`, "http://example.com/")).html, `<a href="#testanchor">Hello</a>`);
|
||||
t.is((await htmlToAbsUrls(`<a href="/test.html">Hello</a>`, "http://example.com/")).html, `<a href="http://example.com/test.html">Hello</a>`);
|
||||
t.is((await htmlToAbsUrls(`<img src="/test.png">`, "http://example.com/")).html, `<img src="http://example.com/test.png">`);
|
||||
t.is((await htmlToAbsUrls(`<a href="http://someotherdomain/">Hello</a>`, "http://example.com/")).html, `<a href="http://someotherdomain/">Hello</a>`);
|
||||
});
|
Reference in New Issue
Block a user