This commit is contained in:
Zach Leatherman 2018-01-27 21:08:43 -06:00
parent 963b5d46e6
commit 119dcbaf5a
6 changed files with 70 additions and 5 deletions

View File

@ -1,4 +1,7 @@
const { DateTime } = require("luxon"); 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"); const highlighters = require("./_src/eleventy-liquidjs-tag-highlight");
function dateToISO(dateObj) { function dateToISO(dateObj) {
@ -24,6 +27,21 @@ module.exports = function(eleventyConfig) {
return DateTime.fromJSDate(dateObj).toFormat("dd LLL yyyy"); 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 // compatibility with existing {% highlight js %} and others
eleventyConfig.addLiquidTag("highlight", highlighters.prismjs); eleventyConfig.addLiquidTag("highlight", highlighters.prismjs);

5
_src/AbsoluteUrl.js Normal file
View File

@ -0,0 +1,5 @@
const { URL } = require("url");
module.exports = function(url, base) {
return (new URL(url, base)).toString()
};

View 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);
};

View 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>`);
});

View File

@ -16,10 +16,10 @@ permalink: feed/feed.xml
{% for post in collections.posts %} {% for post in collections.posts %}
<entry> <entry>
<title>{{ post.data.title }}</title> <title>{{ post.data.title }}</title>
<link href="{{ metadata.url }}{{ post.url }}"/> <link href="{{ post.url | absoluteUrl(metadata.url) }}"/>
<updated>{{ post.date | rssDate }}</updated> <updated>{{ post.date | rssDate }}</updated>
<id>{{ metadata.url }}{{ post.url }}</id> <id>{{ post.url | absoluteUrl(metadata.url) }}</id>
<content type="html">{{ post.templateContent }}</content> <content type="html">{{ post.templateContent | htmlToAbsoluteUrls(metadata.url) }}</content>
</entry> </entry>
{% endfor %} {% endfor %}
</feed> </feed>

View File

@ -21,9 +21,22 @@
"url": "https://github.com/11ty/eleventy-base-blog/issues" "url": "https://github.com/11ty/eleventy-base-blog/issues"
}, },
"homepage": "https://github.com/11ty/eleventy-base-blog#readme", "homepage": "https://github.com/11ty/eleventy-base-blog#readme",
"devDependencies": { "dependencies": {
"@11ty/eleventy": ">=0.2.11", "@11ty/eleventy": "0.2.12",
"luxon": "^0.3.1", "luxon": "^0.3.1",
"posthtml": "^0.11.2",
"posthtml-urls": "^1.0.0",
"prismjs": "^1.10.0" "prismjs": "^1.10.0"
},
"devDependencies": {
"ava": "^0.25.0"
},
"ava": {
"files": [
"_src/test/*.js"
],
"source": [
"_src/**/*.{js,jsx}"
]
} }
} }