Fixes #6
This commit is contained in:
parent
963b5d46e6
commit
119dcbaf5a
18
.eleventy.js
18
.eleventy.js
@ -1,4 +1,7 @@
|
||||
const { DateTime } = require("luxon");
|
||||
const metadata = require("./_data/metadata.json");
|
||||
const absoluteUrl = require("./_src/absoluteUrl");
|
||||
const HtmlToAbsoluteUrls = require("./_src/HtmlToAbsoluteUrls");
|
||||
const highlighters = require("./_src/eleventy-liquidjs-tag-highlight");
|
||||
|
||||
function dateToISO(dateObj) {
|
||||
@ -24,6 +27,21 @@ module.exports = function(eleventyConfig) {
|
||||
return DateTime.fromJSDate(dateObj).toFormat("dd LLL yyyy");
|
||||
});
|
||||
|
||||
eleventyConfig.addNunjucksFilter("absoluteUrl", function(href, base) {
|
||||
return absoluteUrl(href, base);
|
||||
});
|
||||
|
||||
eleventyConfig.addNunjucksFilter("htmlToAbsoluteUrls", function(htmlContent, base, callback) {
|
||||
if(!htmlContent) {
|
||||
callback(null, "");
|
||||
return;
|
||||
}
|
||||
|
||||
HtmlToAbsoluteUrls(htmlContent, base, callback).then(result => {
|
||||
callback(null, result.html);
|
||||
});
|
||||
}, true);
|
||||
|
||||
// compatibility with existing {% highlight js %} and others
|
||||
eleventyConfig.addLiquidTag("highlight", highlighters.prismjs);
|
||||
|
||||
|
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>`);
|
||||
});
|
@ -16,10 +16,10 @@ permalink: feed/feed.xml
|
||||
{% for post in collections.posts %}
|
||||
<entry>
|
||||
<title>{{ post.data.title }}</title>
|
||||
<link href="{{ metadata.url }}{{ post.url }}"/>
|
||||
<link href="{{ post.url | absoluteUrl(metadata.url) }}"/>
|
||||
<updated>{{ post.date | rssDate }}</updated>
|
||||
<id>{{ metadata.url }}{{ post.url }}</id>
|
||||
<content type="html">{{ post.templateContent }}</content>
|
||||
<id>{{ post.url | absoluteUrl(metadata.url) }}</id>
|
||||
<content type="html">{{ post.templateContent | htmlToAbsoluteUrls(metadata.url) }}</content>
|
||||
</entry>
|
||||
{% endfor %}
|
||||
</feed>
|
17
package.json
17
package.json
@ -21,9 +21,22 @@
|
||||
"url": "https://github.com/11ty/eleventy-base-blog/issues"
|
||||
},
|
||||
"homepage": "https://github.com/11ty/eleventy-base-blog#readme",
|
||||
"devDependencies": {
|
||||
"@11ty/eleventy": ">=0.2.11",
|
||||
"dependencies": {
|
||||
"@11ty/eleventy": "0.2.12",
|
||||
"luxon": "^0.3.1",
|
||||
"posthtml": "^0.11.2",
|
||||
"posthtml-urls": "^1.0.0",
|
||||
"prismjs": "^1.10.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"ava": "^0.25.0"
|
||||
},
|
||||
"ava": {
|
||||
"files": [
|
||||
"_src/test/*.js"
|
||||
],
|
||||
"source": [
|
||||
"_src/**/*.{js,jsx}"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user