Skip to content

Commit

Permalink
simplify impl
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Aug 3, 2023
1 parent 27ad605 commit be9d4fe
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions packages/docusaurus-plugin-content-blog/src/feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,25 +111,30 @@ async function defaultCreateFeedItems({
);
const $ = cheerioLoad(content);

const link = normalizeUrl([siteUrl, permalink]);
const blogPostAbsoluteUrl = normalizeUrl([siteUrl, permalink]);

const toAbsoluteUrl = (src: string) =>
String(new URL(src, blogPostAbsoluteUrl));

// Make links and image urls absolute
// See https://github.com/facebook/docusaurus/issues/9136
$(`div#${blogPostContainerID} a, div#${blogPostContainerID} img`).each(
(_, elm) => {
if (elm.tagName === 'a') {
const {href} = elm.attribs;
if (href) {
elm.attribs.href = String(new URL(href, link));
elm.attribs.href = toAbsoluteUrl(href);
}
} else if (elm.tagName === 'img') {
const {src, srcset: srcsetAttr} = elm.attribs;
if (src) {
elm.attribs.src = String(new URL(src, link));
elm.attribs.src = toAbsoluteUrl(src);
}
if (srcsetAttr) {
elm.attribs.srcset = srcset.stringify(
srcset.parse(srcsetAttr).map((props) => ({
...props,
url: String(new URL(props.url, link)),
url: toAbsoluteUrl(props.url),
})),
);
}
Expand All @@ -139,8 +144,8 @@ async function defaultCreateFeedItems({

const feedItem: BlogFeedItem = {
title: metadataTitle,
id: link,
link,
id: blogPostAbsoluteUrl,
link: blogPostAbsoluteUrl,
date,
description,
// Atom feed demands the "term", while other feeds use "name"
Expand Down

0 comments on commit be9d4fe

Please sign in to comment.