From 119dcbaf5acee34f2d989b1b8bded9304909891a Mon Sep 17 00:00:00 2001 From: Zach Leatherman Date: Sat, 27 Jan 2018 21:08:43 -0600 Subject: [PATCH] Fixes #6 --- .eleventy.js | 18 ++++++++++++++++++ _src/AbsoluteUrl.js | 5 +++++ _src/HtmlToAbsoluteUrls.js | 20 ++++++++++++++++++++ _src/test/HtmlToAbsoluteUrlsTest.js | 9 +++++++++ feed/feed.njk | 6 +++--- package.json | 17 +++++++++++++++-- 6 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 _src/AbsoluteUrl.js create mode 100644 _src/HtmlToAbsoluteUrls.js create mode 100644 _src/test/HtmlToAbsoluteUrlsTest.js diff --git a/.eleventy.js b/.eleventy.js index 4a41844..0c4fd68 100644 --- a/.eleventy.js +++ b/.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); diff --git a/_src/AbsoluteUrl.js b/_src/AbsoluteUrl.js new file mode 100644 index 0000000..f85fcc0 --- /dev/null +++ b/_src/AbsoluteUrl.js @@ -0,0 +1,5 @@ +const { URL } = require("url"); + +module.exports = function(url, base) { + return (new URL(url, base)).toString() +}; \ No newline at end of file diff --git a/_src/HtmlToAbsoluteUrls.js b/_src/HtmlToAbsoluteUrls.js new file mode 100644 index 0000000..d71717e --- /dev/null +++ b/_src/HtmlToAbsoluteUrls.js @@ -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); +}; diff --git a/_src/test/HtmlToAbsoluteUrlsTest.js b/_src/test/HtmlToAbsoluteUrlsTest.js new file mode 100644 index 0000000..e039e4e --- /dev/null +++ b/_src/test/HtmlToAbsoluteUrlsTest.js @@ -0,0 +1,9 @@ +import test from "ava"; +import htmlToAbsUrls from "../HtmlToAbsoluteUrls.js"; + +test("Changes a link href", async t => { + t.is((await htmlToAbsUrls(`Hello`, "http://example.com/")).html, `Hello`); + t.is((await htmlToAbsUrls(`Hello`, "http://example.com/")).html, `Hello`); + t.is((await htmlToAbsUrls(``, "http://example.com/")).html, ``); + t.is((await htmlToAbsUrls(`Hello`, "http://example.com/")).html, `Hello`); +}); diff --git a/feed/feed.njk b/feed/feed.njk index 2754226..86ee46a 100644 --- a/feed/feed.njk +++ b/feed/feed.njk @@ -16,10 +16,10 @@ permalink: feed/feed.xml {% for post in collections.posts %} {{ post.data.title }} - + {{ post.date | rssDate }} - {{ metadata.url }}{{ post.url }} - {{ post.templateContent }} + {{ post.url | absoluteUrl(metadata.url) }} + {{ post.templateContent | htmlToAbsoluteUrls(metadata.url) }} {% endfor %} \ No newline at end of file diff --git a/package.json b/package.json index 5e1adec..b588033 100644 --- a/package.json +++ b/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}" + ] } }