Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(content-blog): links in feed should be absolute #9151

Merged
merged 20 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -325,29 +325,32 @@ describe('blog plugin', () => {
it('builds simple website blog with localized dates', async () => {
const siteDir = path.join(__dirname, '__fixtures__', 'website');
const blogPostsFrench = await getBlogPosts(siteDir, {}, getI18n('fr'));
expect(blogPostsFrench).toHaveLength(9);
expect(blogPostsFrench).toHaveLength(10);
expect(blogPostsFrench[0]!.metadata.formattedDate).toMatchInlineSnapshot(
`"6 mars 2021"`,
`"23 juillet 2023"`,
);
expect(blogPostsFrench[1]!.metadata.formattedDate).toMatchInlineSnapshot(
`"5 mars 2021"`,
`"6 mars 2021"`,
);
expect(blogPostsFrench[2]!.metadata.formattedDate).toMatchInlineSnapshot(
`"16 août 2020"`,
`"5 mars 2021"`,
);
expect(blogPostsFrench[3]!.metadata.formattedDate).toMatchInlineSnapshot(
`"15 août 2020"`,
`"16 août 2020"`,
);
expect(blogPostsFrench[4]!.metadata.formattedDate).toMatchInlineSnapshot(
`"27 février 2020"`,
`"15 août 2020"`,
);
expect(blogPostsFrench[5]!.metadata.formattedDate).toMatchInlineSnapshot(
`"27 février 2020"`,
);
expect(blogPostsFrench[6]!.metadata.formattedDate).toMatchInlineSnapshot(
`"2 janvier 2019"`,
`"27 février 2020"`,
);
expect(blogPostsFrench[7]!.metadata.formattedDate).toMatchInlineSnapshot(
`"2 janvier 2019"`,
);
expect(blogPostsFrench[8]!.metadata.formattedDate).toMatchInlineSnapshot(
`"1 janvier 2019"`,
);
});
Expand Down Expand Up @@ -377,7 +380,7 @@ describe('blog plugin', () => {
expect(blogPost.metadata.editUrl).toEqual(hardcodedEditUrl);
});

expect(editUrlFunction).toHaveBeenCalledTimes(9);
expect(editUrlFunction).toHaveBeenCalledTimes(10);

expect(editUrlFunction).toHaveBeenCalledWith({
blogDirPath: 'blog',
Expand Down
29 changes: 28 additions & 1 deletion packages/docusaurus-plugin-content-blog/src/feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async function generateBlogFeed({
return feed;
}

async function defaultCreateFeedItems({
export async function defaultCreateFeedItems({
blogPosts,
siteConfig,
outDir,
Expand Down Expand Up @@ -106,6 +106,33 @@ async function defaultCreateFeedItems({
const $ = cheerioLoad(content);

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

$(`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));
}
} else if (elm.tagName === 'img') {
const {src, srcset} = elm.attribs;
if (src) {
elm.attribs.src = String(new URL(src, link));
}
if (srcset) {
elm.attribs.srcset = srcset
.split(',')
.map((s) => {
const [imageURL, ...descriptors] = s.trim().split(/\s+/);
const newImageURL = new URL(imageURL ?? '', link).href;
return [newImageURL, ...descriptors].join(' ');
})
.join(', ');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks a bit unsafe/risky, maybe introduce a dedicated lib to manipulate srcset reliably instead? see https://www.npmjs.com/package/srcset

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I'll look into it and revise my code

Copy link
Contributor Author

@VinceCYLiao VinceCYLiao Aug 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done and since latest version of srcset is pure ESM, so I have to use the previous version.

}
}
},
);

const feedItem: BlogFeedItem = {
title: metadataTitle,
id: link,
Expand Down
1 change: 1 addition & 0 deletions project-words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ jodyheavener
joshcena
jscodeshift
jssdk
juillet
kaszubowski
katex
kato
Expand Down
9 changes: 9 additions & 0 deletions website/_dogfooding/_blog tests/2023-07-19-a.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: 'Test if href in feed resolved correctly'
---

[absolute full url](https://github.com/facebook/docusaurus)

[absolute url with implicit domain name](/tests/blog/2023/07/19/b)

[relative url](2023-07-19-b.mdx)
1 change: 1 addition & 0 deletions website/_dogfooding/_blog tests/2023-07-19-b.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Test Relative Path
Loading