feat: update implementation of RSS feeds
Adds a new filter `convertRelativeLinks` that converts any relative link to an absolute path Adds an XML transform to minify the final output for smaller filesizes Updates some data in meta Adds a new macro for generating an RSS feed
This commit is contained in:
parent
46ed7a29be
commit
e6cfa88f61
14 changed files with 1508 additions and 271 deletions
|
@ -4,6 +4,8 @@ import advancedFormat from "dayjs/plugin/advancedFormat.js";
|
|||
|
||||
import pluralizeBase from "pluralize";
|
||||
|
||||
import { JSDOM } from "jsdom";
|
||||
|
||||
export const keys = Object.keys;
|
||||
export const values = Object.values;
|
||||
export const entries = Object.entries;
|
||||
|
@ -12,6 +14,7 @@ dayjs.extend(utc);
|
|||
dayjs.extend(advancedFormat);
|
||||
|
||||
export const formatDate = (date, format) => dayjs.utc(date).format(format);
|
||||
export const formatAsUTCString = (date) => new Date(date).toUTCString();
|
||||
|
||||
export const organizeByDate = (collection) => {
|
||||
const collectionByDate = {};
|
||||
|
@ -101,3 +104,29 @@ export const isOld = (dateArg) => {
|
|||
|
||||
return diffInYears >= 2;
|
||||
};
|
||||
|
||||
// From coryd.dev
|
||||
// https://www.coryd.dev/posts/2025/generating-absolute-urls-in-my-rss-feeds/
|
||||
export const convertRelativeLinks = (htmlContent, url) => {
|
||||
if (!htmlContent || !url) return htmlContent;
|
||||
|
||||
const dom = new JSDOM(htmlContent);
|
||||
const document = dom.window.document;
|
||||
|
||||
document.querySelectorAll("a[href]").forEach((link) => {
|
||||
let href = link.getAttribute("href");
|
||||
|
||||
if (href.startsWith("#")) {
|
||||
link.remove();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!href.startsWith("http://") && !href.startsWith("https://"))
|
||||
link.setAttribute(
|
||||
"href",
|
||||
`${url.replace(/\/$/, "")}/${href.replace(/^\/+/, "")}`,
|
||||
);
|
||||
});
|
||||
|
||||
return document.body.innerHTML;
|
||||
};
|
||||
|
|
12
config/transforms/xml-config.js
Normal file
12
config/transforms/xml-config.js
Normal file
|
@ -0,0 +1,12 @@
|
|||
import minifyXML from "minify-xml";
|
||||
|
||||
export default function (eleventyConfig) {
|
||||
eleventyConfig.addTransform("xml-minify", (content, path) => {
|
||||
if (path && path.endsWith(".xml")) {
|
||||
return minifyXML(content, {
|
||||
shortenNamespaces: false,
|
||||
});
|
||||
}
|
||||
return content;
|
||||
});
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue