From b6079568c85401b9dc3dede6c980d6ef67882647 Mon Sep 17 00:00:00 2001
From: VinceCYLiao
Date: Wed, 19 Jul 2023 00:26:31 +0800
Subject: [PATCH 01/19] fix: #9136:fix(content-blog): links in feed should be
absolute
---
packages/docusaurus-plugin-content-blog/src/feed.ts | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/packages/docusaurus-plugin-content-blog/src/feed.ts b/packages/docusaurus-plugin-content-blog/src/feed.ts
index dc9a7b85f84c..9efc84e16ccf 100644
--- a/packages/docusaurus-plugin-content-blog/src/feed.ts
+++ b/packages/docusaurus-plugin-content-blog/src/feed.ts
@@ -106,6 +106,14 @@ async function defaultCreateFeedItems({
const $ = cheerioLoad(content);
const link = normalizeUrl([siteUrl, permalink]);
+
+ $(`div#${blogPostContainerID} a`).each((_, elm) => {
+ const {href} = elm.attribs;
+ if (href) {
+ elm.attribs.href = String(new URL(href, link));
+ }
+ });
+
const feedItem: BlogFeedItem = {
title: metadataTitle,
id: link,
From a339fc9f8142e9595eeeefd69f8f95798081dd22 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=94=BF=E5=AE=87=20=E5=BB=96?=
Date: Wed, 19 Jul 2023 16:15:33 +0800
Subject: [PATCH 02/19] feat: add test to check if links in feed are resolved
correctly (+2 squashed commits) Squashed commits: [2db488373] chore: add a
new file to test href resolving [6c18ceaa4] docs: added to test if href
resolved correctly in feed
---
.../website/blog/blog-with-links.md | 11 +
.../blog/blog-with-links/index.html | 21 ++
.../__tests__/__snapshots__/feed.test.ts.snap | 357 ++++++++++++++++--
.../src/__tests__/feed.test.ts | 130 ++++++-
.../src/feed.ts | 2 +-
.../_dogfooding/_blog tests/2023-07-19-a.mdx | 9 +
.../_dogfooding/_blog tests/2023-07-19-b.mdx | 1 +
7 files changed, 501 insertions(+), 30 deletions(-)
create mode 100644 packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/website/blog/blog-with-links.md
create mode 100644 packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/website/build-snap/blog/blog-with-links/index.html
create mode 100644 website/_dogfooding/_blog tests/2023-07-19-a.mdx
create mode 100644 website/_dogfooding/_blog tests/2023-07-19-b.mdx
diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/website/blog/blog-with-links.md b/packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/website/blog/blog-with-links.md
new file mode 100644
index 000000000000..651c4cbe5525
--- /dev/null
+++ b/packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/website/blog/blog-with-links.md
@@ -0,0 +1,11 @@
+---
+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)
+
+[anchor](#title)
diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/website/build-snap/blog/blog-with-links/index.html b/packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/website/build-snap/blog/blog-with-links/index.html
new file mode 100644
index 000000000000..3ba7d0584081
--- /dev/null
+++ b/packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/website/build-snap/blog/blog-with-links/index.html
@@ -0,0 +1,21 @@
+
+
+
+
+
+Test if href in feed resolved correctly | Docusaurus blog only!
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/__snapshots__/feed.test.ts.snap b/packages/docusaurus-plugin-content-blog/src/__tests__/__snapshots__/feed.test.ts.snap
index ae568922d131..1033b98702b6 100644
--- a/packages/docusaurus-plugin-content-blog/src/__tests__/__snapshots__/feed.test.ts.snap
+++ b/packages/docusaurus-plugin-content-blog/src/__tests__/__snapshots__/feed.test.ts.snap
@@ -1,17 +1,28 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
-exports[`atom filters to the first two entries 1`] = `
+exports[`Test defaultCreateFeedItems links in feeds are resolved correctly 1`] = `
[
"
https://docusaurus.io/myBaseUrl/blog
Hello Blog
- 2021-03-06T00:00:00.000Z
+ 2023-07-19T08:15:33.000Z
https://github.com/jpmonette/feed
Hello Blog
https://docusaurus.io/myBaseUrl/image/favicon.ico
Copyright
+
+
+ https://docusaurus.io/myBaseUrl/blog/blog-with-links
+
+ 2023-07-19T08:15:33.000Z
+
+ absolute full url
+absolute url with implicit domain name
+relative url
+anchor
]]>
+
https://docusaurus.io/myBaseUrl/blog/mdx-require-blog-post
@@ -26,7 +37,267 @@ exports[`atom filters to the first two entries 1`] = `
2021-03-05T00:00:00.000Z
- HTML Heading 1HTML Heading 2 HTML Paragraph
Import DOM
Heading 1 Heading 2 Heading 3 Heading 4 Heading 5 Normal Text Italics Text Bold Text
link
]]>
+ HTML Heading 1HTML Heading 2 HTML Paragraph
Import DOM
Heading 1 Heading 2 Heading 3 Heading 4 Heading 5 Normal Text Italics Text Bold Text
link
]]>
+
+
+
+ https://docusaurus.io/myBaseUrl/blog/hey/my super path/héllô
+
+ 2020-08-16T00:00:00.000Z
+
+ complex url slug]]>
+
+
+
+
+
+ https://docusaurus.io/myBaseUrl/blog/simple/slug
+
+ 2020-08-15T00:00:00.000Z
+
+ simple url slug]]>
+
+ Sébastien Lorber
+ https://sebastienlorber.com
+
+
+
+
+ https://docusaurus.io/myBaseUrl/blog/heading-as-title
+
+ 2019-01-02T00:00:00.000Z
+
+
+
+ https://docusaurus.io/myBaseUrl/blog/date-matter
+
+ 2019-01-01T00:00:00.000Z
+
+ date inside front matter]]>
+
+
+
+
+ https://docusaurus.io/myBaseUrl/blog/2018/12/14/Happy-First-Birthday-Slash
+
+ 2018-12-14T00:00:00.000Z
+
+ Happy birthday!]]>
+
+ Yangshun Tay (translated)
+
+
+ Sébastien Lorber (translated)
+ lorber.sebastien@gmail.com
+
+
+",
+ "
+
+
+ Hello Blog
+ https://docusaurus.io/myBaseUrl/blog
+ Hello Blog
+ Wed, 19 Jul 2023 08:15:33 GMT
+ https://validator.w3.org/feed/docs/rss2.html
+ https://github.com/jpmonette/feed
+ en
+ Copyright
+ -
+
+ https://docusaurus.io/myBaseUrl/blog/blog-with-links
+ https://docusaurus.io/myBaseUrl/blog/blog-with-links
+ Wed, 19 Jul 2023 08:15:33 GMT
+
+ absolute full url
+absolute url with implicit domain name
+relative url
+anchor
]]>
+
+ -
+
+ https://docusaurus.io/myBaseUrl/blog/mdx-require-blog-post
+ https://docusaurus.io/myBaseUrl/blog/mdx-require-blog-post
+ Sat, 06 Mar 2021 00:00:00 GMT
+
+ Test MDX with require calls ]]>
+
+ -
+
+ https://docusaurus.io/myBaseUrl/blog/mdx-blog-post
+ https://docusaurus.io/myBaseUrl/blog/mdx-blog-post
+ Fri, 05 Mar 2021 00:00:00 GMT
+
+ HTML Heading 1HTML Heading 2 HTML Paragraph
Import DOM
Heading 1 Heading 2 Heading 3 Heading 4 Heading 5 Normal Text Italics Text Bold Text
link
]]>
+
+ -
+
+ https://docusaurus.io/myBaseUrl/blog/hey/my super path/héllô
+ https://docusaurus.io/myBaseUrl/blog/hey/my super path/héllô
+ Sun, 16 Aug 2020 00:00:00 GMT
+
+ complex url slug]]>
+ date
+ complex
+
+ -
+
+ https://docusaurus.io/myBaseUrl/blog/simple/slug
+ https://docusaurus.io/myBaseUrl/blog/simple/slug
+ Sat, 15 Aug 2020 00:00:00 GMT
+
+ simple url slug]]>
+
+ -
+
+ https://docusaurus.io/myBaseUrl/blog/heading-as-title
+ https://docusaurus.io/myBaseUrl/blog/heading-as-title
+ Wed, 02 Jan 2019 00:00:00 GMT
+
+ -
+
+ https://docusaurus.io/myBaseUrl/blog/date-matter
+ https://docusaurus.io/myBaseUrl/blog/date-matter
+ Tue, 01 Jan 2019 00:00:00 GMT
+
+ date inside front matter]]>
+ date
+
+ -
+
+ https://docusaurus.io/myBaseUrl/blog/2018/12/14/Happy-First-Birthday-Slash
+ https://docusaurus.io/myBaseUrl/blog/2018/12/14/Happy-First-Birthday-Slash
+ Fri, 14 Dec 2018 00:00:00 GMT
+
+ Happy birthday!]]>
+ lorber.sebastien@gmail.com (Sébastien Lorber (translated))
+
+
+ ",
+ "{
+ "version": "https://jsonfeed.org/version/1",
+ "title": "Hello Blog",
+ "home_page_url": "https://docusaurus.io/myBaseUrl/blog",
+ "description": "Hello Blog",
+ "items": [
+ {
+ "id": "https://docusaurus.io/myBaseUrl/blog/blog-with-links",
+ "content_html": "absolute full url
/nabsolute url with implicit domain name
/nrelative url
/nanchor
",
+ "url": "https://docusaurus.io/myBaseUrl/blog/blog-with-links",
+ "title": "Test if href in feed resolved correctly",
+ "summary": "absolute full url",
+ "date_modified": "2023-07-19T08:15:33.000Z",
+ "tags": []
+ },
+ {
+ "id": "https://docusaurus.io/myBaseUrl/blog/mdx-require-blog-post",
+ "content_html": "Test MDX with require calls
",
+ "url": "https://docusaurus.io/myBaseUrl/blog/mdx-require-blog-post",
+ "title": "MDX Blog Sample with require calls",
+ "summary": "Test MDX with require calls",
+ "date_modified": "2021-03-06T00:00:00.000Z",
+ "tags": []
+ },
+ {
+ "id": "https://docusaurus.io/myBaseUrl/blog/mdx-blog-post",
+ "content_html": "HTML Heading 1 HTML Heading 2 HTML Paragraph
Import DOM
Heading 1 Heading 2 Heading 3 Heading 4 Heading 5 Normal Text Italics Text Bold Text
link
",
+ "url": "https://docusaurus.io/myBaseUrl/blog/mdx-blog-post",
+ "title": "Full Blog Sample",
+ "summary": "HTML Heading 1",
+ "date_modified": "2021-03-05T00:00:00.000Z",
+ "tags": []
+ },
+ {
+ "id": "https://docusaurus.io/myBaseUrl/blog/hey/my super path/héllô",
+ "content_html": "complex url slug
",
+ "url": "https://docusaurus.io/myBaseUrl/blog/hey/my super path/héllô",
+ "title": "Complex Slug",
+ "summary": "complex url slug",
+ "date_modified": "2020-08-16T00:00:00.000Z",
+ "tags": [
+ "date",
+ "complex"
+ ]
+ },
+ {
+ "id": "https://docusaurus.io/myBaseUrl/blog/simple/slug",
+ "content_html": "simple url slug
",
+ "url": "https://docusaurus.io/myBaseUrl/blog/simple/slug",
+ "title": "Simple Slug",
+ "summary": "simple url slug",
+ "date_modified": "2020-08-15T00:00:00.000Z",
+ "author": {
+ "name": "Sébastien Lorber",
+ "url": "https://sebastienlorber.com"
+ },
+ "tags": []
+ },
+ {
+ "id": "https://docusaurus.io/myBaseUrl/blog/heading-as-title",
+ "content_html": "",
+ "url": "https://docusaurus.io/myBaseUrl/blog/heading-as-title",
+ "title": "some heading",
+ "date_modified": "2019-01-02T00:00:00.000Z",
+ "tags": []
+ },
+ {
+ "id": "https://docusaurus.io/myBaseUrl/blog/date-matter",
+ "content_html": "date inside front matter
",
+ "url": "https://docusaurus.io/myBaseUrl/blog/date-matter",
+ "title": "date-matter",
+ "summary": "date inside front matter",
+ "date_modified": "2019-01-01T00:00:00.000Z",
+ "tags": [
+ "date"
+ ]
+ },
+ {
+ "id": "https://docusaurus.io/myBaseUrl/blog/2018/12/14/Happy-First-Birthday-Slash",
+ "content_html": "Happy birthday!
",
+ "url": "https://docusaurus.io/myBaseUrl/blog/2018/12/14/Happy-First-Birthday-Slash",
+ "title": "Happy 1st Birthday Slash! (translated)",
+ "summary": "Happy birthday! (translated)",
+ "date_modified": "2018-12-14T00:00:00.000Z",
+ "author": {
+ "name": "Yangshun Tay (translated)"
+ },
+ "tags": []
+ }
+ ]
+}",
+]
+`;
+
+exports[`atom filters to the first two entries 1`] = `
+[
+ "
+
+ https://docusaurus.io/myBaseUrl/blog
+ Hello Blog
+ 2023-07-19T08:15:33.000Z
+ https://github.com/jpmonette/feed
+
+ Hello Blog
+ https://docusaurus.io/myBaseUrl/image/favicon.ico
+ Copyright
+
+
+ https://docusaurus.io/myBaseUrl/blog/blog-with-links
+
+ 2023-07-19T08:15:33.000Z
+
+ absolute full url
+absolute url with implicit domain name
+relative url
+anchor
]]>
+
+
+
+ https://docusaurus.io/myBaseUrl/blog/mdx-require-blog-post
+
+ 2021-03-06T00:00:00.000Z
+
+ Test MDX with require calls ]]>
",
]
@@ -38,12 +309,23 @@ exports[`atom has feed item for each post 1`] = `
https://docusaurus.io/myBaseUrl/blog
Hello Blog
- 2021-03-06T00:00:00.000Z
+ 2023-07-19T08:15:33.000Z
https://github.com/jpmonette/feed
Hello Blog
https://docusaurus.io/myBaseUrl/image/favicon.ico
Copyright
+
+
+ https://docusaurus.io/myBaseUrl/blog/blog-with-links
+
+ 2023-07-19T08:15:33.000Z
+
+ absolute full url
+absolute url with implicit domain name
+relative url
+anchor
]]>
+
https://docusaurus.io/myBaseUrl/blog/mdx-require-blog-post
@@ -58,7 +340,7 @@ exports[`atom has feed item for each post 1`] = `
2021-03-05T00:00:00.000Z
- HTML Heading 1HTML Heading 2 HTML Paragraph
Import DOM
Heading 1 Heading 2 Heading 3 Heading 4 Heading 5 Normal Text Italics Text Bold Text
link
]]>
+ HTML Heading 1HTML Heading 2 HTML Paragraph
Import DOM
Heading 1 Heading 2 Heading 3 Heading 4 Heading 5 Normal Text Italics Text Bold Text
link
]]>
@@ -124,6 +406,15 @@ exports[`json filters to the first two entries 1`] = `
"home_page_url": "https://docusaurus.io/myBaseUrl/blog",
"description": "Hello Blog",
"items": [
+ {
+ "id": "https://docusaurus.io/myBaseUrl/blog/blog-with-links",
+ "content_html": "absolute full url
/nabsolute url with implicit domain name
/nrelative url
/nanchor
",
+ "url": "https://docusaurus.io/myBaseUrl/blog/blog-with-links",
+ "title": "Test if href in feed resolved correctly",
+ "summary": "absolute full url",
+ "date_modified": "2023-07-19T08:15:33.000Z",
+ "tags": []
+ },
{
"id": "https://docusaurus.io/myBaseUrl/blog/mdx-require-blog-post",
"content_html": "Test MDX with require calls
",
@@ -132,15 +423,6 @@ exports[`json filters to the first two entries 1`] = `
"summary": "Test MDX with require calls",
"date_modified": "2021-03-06T00:00:00.000Z",
"tags": []
- },
- {
- "id": "https://docusaurus.io/myBaseUrl/blog/mdx-blog-post",
- "content_html": "HTML Heading 1 HTML Heading 2 HTML Paragraph
Import DOM
Heading 1 Heading 2 Heading 3 Heading 4 Heading 5 Normal Text Italics Text Bold Text
link
",
- "url": "https://docusaurus.io/myBaseUrl/blog/mdx-blog-post",
- "title": "Full Blog Sample",
- "summary": "HTML Heading 1",
- "date_modified": "2021-03-05T00:00:00.000Z",
- "tags": []
}
]
}",
@@ -155,6 +437,15 @@ exports[`json has feed item for each post 1`] = `
"home_page_url": "https://docusaurus.io/myBaseUrl/blog",
"description": "Hello Blog",
"items": [
+ {
+ "id": "https://docusaurus.io/myBaseUrl/blog/blog-with-links",
+ "content_html": "absolute full url
/nabsolute url with implicit domain name
/nrelative url
/nanchor
",
+ "url": "https://docusaurus.io/myBaseUrl/blog/blog-with-links",
+ "title": "Test if href in feed resolved correctly",
+ "summary": "absolute full url",
+ "date_modified": "2023-07-19T08:15:33.000Z",
+ "tags": []
+ },
{
"id": "https://docusaurus.io/myBaseUrl/blog/mdx-require-blog-post",
"content_html": "Test MDX with require calls
",
@@ -166,7 +457,7 @@ exports[`json has feed item for each post 1`] = `
},
{
"id": "https://docusaurus.io/myBaseUrl/blog/mdx-blog-post",
- "content_html": "HTML Heading 1 HTML Heading 2 HTML Paragraph
Import DOM
Heading 1 Heading 2 Heading 3 Heading 4 Heading 5 Normal Text Italics Text Bold Text
link
",
+ "content_html": "HTML Heading 1 HTML Heading 2 HTML Paragraph
Import DOM
Heading 1 Heading 2 Heading 3 Heading 4 Heading 5 Normal Text Italics Text Bold Text
link
",
"url": "https://docusaurus.io/myBaseUrl/blog/mdx-blog-post",
"title": "Full Blog Sample",
"summary": "HTML Heading 1",
@@ -242,11 +533,22 @@ exports[`rss filters to the first two entries 1`] = `
Hello Blog
https://docusaurus.io/myBaseUrl/blog
Hello Blog
- Sat, 06 Mar 2021 00:00:00 GMT
+ Wed, 19 Jul 2023 08:15:33 GMT
https://validator.w3.org/feed/docs/rss2.html
https://github.com/jpmonette/feed
en
Copyright
+ -
+
+ https://docusaurus.io/myBaseUrl/blog/blog-with-links
+ https://docusaurus.io/myBaseUrl/blog/blog-with-links
+ Wed, 19 Jul 2023 08:15:33 GMT
+
+ absolute full url
+absolute url with implicit domain name
+relative url
+anchor
]]>
+
-
https://docusaurus.io/myBaseUrl/blog/mdx-require-blog-post
@@ -255,14 +557,6 @@ exports[`rss filters to the first two entries 1`] = `
Test MDX with require calls ]]>
- -
-
- https://docusaurus.io/myBaseUrl/blog/mdx-blog-post
- https://docusaurus.io/myBaseUrl/blog/mdx-blog-post
- Fri, 05 Mar 2021 00:00:00 GMT
-
- HTML Heading 1HTML Heading 2 HTML Paragraph
Import DOM
Heading 1 Heading 2 Heading 3 Heading 4 Heading 5 Normal Text Italics Text Bold Text
link
]]>
-
",
]
@@ -276,11 +570,22 @@ exports[`rss has feed item for each post 1`] = `
Hello Blog
https://docusaurus.io/myBaseUrl/blog
Hello Blog
- Sat, 06 Mar 2021 00:00:00 GMT
+ Wed, 19 Jul 2023 08:15:33 GMT
https://validator.w3.org/feed/docs/rss2.html
https://github.com/jpmonette/feed
en
Copyright
+ -
+
+ https://docusaurus.io/myBaseUrl/blog/blog-with-links
+ https://docusaurus.io/myBaseUrl/blog/blog-with-links
+ Wed, 19 Jul 2023 08:15:33 GMT
+
+ absolute full url
+absolute url with implicit domain name
+relative url
+anchor
]]>
+
-
https://docusaurus.io/myBaseUrl/blog/mdx-require-blog-post
@@ -295,7 +600,7 @@ exports[`rss has feed item for each post 1`] = `
https://docusaurus.io/myBaseUrl/blog/mdx-blog-post
Fri, 05 Mar 2021 00:00:00 GMT
- HTML Heading 1HTML Heading 2 HTML Paragraph
Import DOM
Heading 1 Heading 2 Heading 3 Heading 4 Heading 5 Normal Text Italics Text Bold Text
link
]]>
+ HTML Heading 1HTML Heading 2 HTML Paragraph
Import DOM
Heading 1 Heading 2 Heading 3 Heading 4 Heading 5 Normal Text Italics Text Bold Text
link
]]>
-
diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/feed.test.ts b/packages/docusaurus-plugin-content-blog/src/__tests__/feed.test.ts
index 6592c09d7aa5..d98961e3868e 100644
--- a/packages/docusaurus-plugin-content-blog/src/__tests__/feed.test.ts
+++ b/packages/docusaurus-plugin-content-blog/src/__tests__/feed.test.ts
@@ -8,12 +8,18 @@
import {jest} from '@jest/globals';
import path from 'path';
import fs from 'fs-extra';
+import {load as cheerioLoad} from 'cheerio';
+import {readOutputHTMLFile} from '@docusaurus/utils';
+import {blogPostContainerID} from '@docusaurus/utils-common';
import {DEFAULT_OPTIONS} from '../options';
import {generateBlogPosts} from '../blogUtils';
-import {createBlogFeedFiles} from '../feed';
-import type {LoadContext, I18n} from '@docusaurus/types';
+import {
+ createBlogFeedFiles,
+ defaultCreateFeedItems as feedDefaultCreateFeedItems,
+} from '../feed';
+import type {LoadContext, I18n, DocusaurusConfig} from '@docusaurus/types';
import type {BlogContentPaths} from '../types';
-import type {PluginOptions} from '@docusaurus/plugin-content-blog';
+import type {BlogPost, PluginOptions} from '@docusaurus/plugin-content-blog';
const DefaultI18N: I18n = {
currentLocale: 'en',
@@ -62,6 +68,32 @@ async function testGenerateFeeds(
});
}
+function isFullAbsolutePath(str: string) {
+ const domain = 'https://domain.com';
+ const {origin} = new URL(str, domain);
+ return origin !== domain;
+}
+
+async function generateLinksOfBlogPosts(outDir: string, blogPosts: BlogPost[]) {
+ const linksOfBlogPosts: {[postId: string]: string[]} = {};
+ const pathOfFile = path.join(outDir, 'blog');
+ const promises = blogPosts.map(async (post) => {
+ try {
+ const content = await readOutputHTMLFile(post.id, pathOfFile, true);
+ const $ = cheerioLoad(content);
+ const anchorElements = $(`div#${blogPostContainerID} a`);
+ if (anchorElements.length > 0) {
+ const href = anchorElements.map((_, elm) => elm.attribs.href).toArray();
+ linksOfBlogPosts[post.id] = href;
+ }
+ } catch {
+ // post is a draft
+ }
+ });
+ await Promise.all(promises);
+ return linksOfBlogPosts;
+}
+
describe.each(['atom', 'rss', 'json'])('%s', (feedType) => {
const fsMock = jest.spyOn(fs, 'outputFile').mockImplementation(() => {});
@@ -196,3 +228,95 @@ describe.each(['atom', 'rss', 'json'])('%s', (feedType) => {
fsMock.mockClear();
});
});
+
+describe('Test defaultCreateFeedItems', () => {
+ const fsMock = jest.spyOn(fs, 'outputFile').mockImplementation(() => {});
+ it('links in feeds are resolved correctly', async () => {
+ const siteDir = path.join(__dirname, '__fixtures__', 'website');
+ const outDir = path.join(siteDir, 'build-snap');
+ const siteConfig = {
+ title: 'Hello',
+ baseUrl: '/myBaseUrl/',
+ url: 'https://docusaurus.io',
+ favicon: 'image/favicon.ico',
+ } as DocusaurusConfig;
+
+ const context = {
+ siteDir,
+ siteConfig,
+ i18n: DefaultI18N,
+ outDir,
+ } as LoadContext;
+
+ const options = {
+ path: 'blog',
+ routeBasePath: 'blog',
+ tagsBasePath: 'tags',
+ authorsMapPath: 'authors.yml',
+ include: DEFAULT_OPTIONS.include,
+ exclude: DEFAULT_OPTIONS.exclude,
+ feedOptions: {
+ type: ['atom', 'rss', 'json'],
+ copyright: 'Copyright',
+ },
+ readingTime: ({content, defaultReadingTime}) =>
+ defaultReadingTime({content}),
+ truncateMarker: //,
+ } as PluginOptions;
+
+ const blogPosts = await generateBlogPosts(
+ getBlogContentPaths(siteDir),
+ context,
+ options,
+ );
+
+ await createBlogFeedFiles({
+ blogPosts,
+ options,
+ siteConfig: context.siteConfig,
+ outDir: context.outDir,
+ locale: 'en',
+ });
+
+ const originalLinksInBlogs: {[id: string]: Array} =
+ await generateLinksOfBlogPosts(outDir, blogPosts);
+
+ const blogPostsWithLinks = blogPosts.filter(
+ (post) => originalLinksInBlogs[post.id],
+ );
+
+ const feedsWithLinks = await feedDefaultCreateFeedItems({
+ blogPosts: blogPostsWithLinks,
+ siteConfig,
+ outDir,
+ });
+
+ feedsWithLinks.forEach((feed) => {
+ const $ = cheerioLoad(feed.content ?? '');
+ const linksInFeed = $('a')
+ .map((_, elm) => elm.attribs.href)
+ .toArray();
+ const idOfBlogPost = feed.id!.replace(
+ new URL(`${siteConfig.baseUrl}blog`, siteConfig.url).href,
+ '',
+ );
+ const originalLinksInBlog = originalLinksInBlogs[idOfBlogPost];
+ const {permalink = ''} =
+ blogPostsWithLinks.find((post) => post.id === idOfBlogPost)?.metadata ||
+ {};
+
+ originalLinksInBlog!.forEach((originalLinkInBlog, idx) => {
+ const linkToTest = isFullAbsolutePath(originalLinkInBlog)
+ ? originalLinkInBlog
+ : new URL(originalLinkInBlog, new URL(permalink, siteConfig.url))
+ .href;
+
+ expect(linkToTest).toEqual(linksInFeed[idx]);
+ });
+ });
+ expect(
+ fsMock.mock.calls.map((call) => call[1] as string),
+ ).toMatchSnapshot();
+ fsMock.mockClear();
+ });
+});
diff --git a/packages/docusaurus-plugin-content-blog/src/feed.ts b/packages/docusaurus-plugin-content-blog/src/feed.ts
index 9efc84e16ccf..df0da93f5ce5 100644
--- a/packages/docusaurus-plugin-content-blog/src/feed.ts
+++ b/packages/docusaurus-plugin-content-blog/src/feed.ts
@@ -70,7 +70,7 @@ async function generateBlogFeed({
return feed;
}
-async function defaultCreateFeedItems({
+export async function defaultCreateFeedItems({
blogPosts,
siteConfig,
outDir,
diff --git a/website/_dogfooding/_blog tests/2023-07-19-a.mdx b/website/_dogfooding/_blog tests/2023-07-19-a.mdx
new file mode 100644
index 000000000000..16cdfd88d675
--- /dev/null
+++ b/website/_dogfooding/_blog tests/2023-07-19-a.mdx
@@ -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)
diff --git a/website/_dogfooding/_blog tests/2023-07-19-b.mdx b/website/_dogfooding/_blog tests/2023-07-19-b.mdx
new file mode 100644
index 000000000000..d6a54897045a
--- /dev/null
+++ b/website/_dogfooding/_blog tests/2023-07-19-b.mdx
@@ -0,0 +1 @@
+# Test Relative Path
From e65995e2f4c63782300df50dae32516ddb9e5de1 Mon Sep 17 00:00:00 2001
From: VinceCYLiao
Date: Sun, 23 Jul 2023 15:47:07 +0800
Subject: [PATCH 03/19] feat: absolutize img URLs
---
.../src/feed.ts | 31 +++++++++++++++----
1 file changed, 25 insertions(+), 6 deletions(-)
diff --git a/packages/docusaurus-plugin-content-blog/src/feed.ts b/packages/docusaurus-plugin-content-blog/src/feed.ts
index df0da93f5ce5..06ef67cb3b04 100644
--- a/packages/docusaurus-plugin-content-blog/src/feed.ts
+++ b/packages/docusaurus-plugin-content-blog/src/feed.ts
@@ -107,12 +107,31 @@ export async function defaultCreateFeedItems({
const link = normalizeUrl([siteUrl, permalink]);
- $(`div#${blogPostContainerID} a`).each((_, elm) => {
- const {href} = elm.attribs;
- if (href) {
- elm.attribs.href = String(new URL(href, link));
- }
- });
+ $(`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(', ');
+ }
+ }
+ },
+ );
const feedItem: BlogFeedItem = {
title: metadataTitle,
From aaa869a514f1a30ae398c04b2ed86b9f226f8285 Mon Sep 17 00:00:00 2001
From: VinceCYLiao
Date: Sun, 23 Jul 2023 15:48:30 +0800
Subject: [PATCH 04/19] chore: remove test case added in previous commit
---
.../src/__tests__/feed.test.ts | 130 +-----------------
1 file changed, 3 insertions(+), 127 deletions(-)
diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/feed.test.ts b/packages/docusaurus-plugin-content-blog/src/__tests__/feed.test.ts
index d98961e3868e..6592c09d7aa5 100644
--- a/packages/docusaurus-plugin-content-blog/src/__tests__/feed.test.ts
+++ b/packages/docusaurus-plugin-content-blog/src/__tests__/feed.test.ts
@@ -8,18 +8,12 @@
import {jest} from '@jest/globals';
import path from 'path';
import fs from 'fs-extra';
-import {load as cheerioLoad} from 'cheerio';
-import {readOutputHTMLFile} from '@docusaurus/utils';
-import {blogPostContainerID} from '@docusaurus/utils-common';
import {DEFAULT_OPTIONS} from '../options';
import {generateBlogPosts} from '../blogUtils';
-import {
- createBlogFeedFiles,
- defaultCreateFeedItems as feedDefaultCreateFeedItems,
-} from '../feed';
-import type {LoadContext, I18n, DocusaurusConfig} from '@docusaurus/types';
+import {createBlogFeedFiles} from '../feed';
+import type {LoadContext, I18n} from '@docusaurus/types';
import type {BlogContentPaths} from '../types';
-import type {BlogPost, PluginOptions} from '@docusaurus/plugin-content-blog';
+import type {PluginOptions} from '@docusaurus/plugin-content-blog';
const DefaultI18N: I18n = {
currentLocale: 'en',
@@ -68,32 +62,6 @@ async function testGenerateFeeds(
});
}
-function isFullAbsolutePath(str: string) {
- const domain = 'https://domain.com';
- const {origin} = new URL(str, domain);
- return origin !== domain;
-}
-
-async function generateLinksOfBlogPosts(outDir: string, blogPosts: BlogPost[]) {
- const linksOfBlogPosts: {[postId: string]: string[]} = {};
- const pathOfFile = path.join(outDir, 'blog');
- const promises = blogPosts.map(async (post) => {
- try {
- const content = await readOutputHTMLFile(post.id, pathOfFile, true);
- const $ = cheerioLoad(content);
- const anchorElements = $(`div#${blogPostContainerID} a`);
- if (anchorElements.length > 0) {
- const href = anchorElements.map((_, elm) => elm.attribs.href).toArray();
- linksOfBlogPosts[post.id] = href;
- }
- } catch {
- // post is a draft
- }
- });
- await Promise.all(promises);
- return linksOfBlogPosts;
-}
-
describe.each(['atom', 'rss', 'json'])('%s', (feedType) => {
const fsMock = jest.spyOn(fs, 'outputFile').mockImplementation(() => {});
@@ -228,95 +196,3 @@ describe.each(['atom', 'rss', 'json'])('%s', (feedType) => {
fsMock.mockClear();
});
});
-
-describe('Test defaultCreateFeedItems', () => {
- const fsMock = jest.spyOn(fs, 'outputFile').mockImplementation(() => {});
- it('links in feeds are resolved correctly', async () => {
- const siteDir = path.join(__dirname, '__fixtures__', 'website');
- const outDir = path.join(siteDir, 'build-snap');
- const siteConfig = {
- title: 'Hello',
- baseUrl: '/myBaseUrl/',
- url: 'https://docusaurus.io',
- favicon: 'image/favicon.ico',
- } as DocusaurusConfig;
-
- const context = {
- siteDir,
- siteConfig,
- i18n: DefaultI18N,
- outDir,
- } as LoadContext;
-
- const options = {
- path: 'blog',
- routeBasePath: 'blog',
- tagsBasePath: 'tags',
- authorsMapPath: 'authors.yml',
- include: DEFAULT_OPTIONS.include,
- exclude: DEFAULT_OPTIONS.exclude,
- feedOptions: {
- type: ['atom', 'rss', 'json'],
- copyright: 'Copyright',
- },
- readingTime: ({content, defaultReadingTime}) =>
- defaultReadingTime({content}),
- truncateMarker: //,
- } as PluginOptions;
-
- const blogPosts = await generateBlogPosts(
- getBlogContentPaths(siteDir),
- context,
- options,
- );
-
- await createBlogFeedFiles({
- blogPosts,
- options,
- siteConfig: context.siteConfig,
- outDir: context.outDir,
- locale: 'en',
- });
-
- const originalLinksInBlogs: {[id: string]: Array} =
- await generateLinksOfBlogPosts(outDir, blogPosts);
-
- const blogPostsWithLinks = blogPosts.filter(
- (post) => originalLinksInBlogs[post.id],
- );
-
- const feedsWithLinks = await feedDefaultCreateFeedItems({
- blogPosts: blogPostsWithLinks,
- siteConfig,
- outDir,
- });
-
- feedsWithLinks.forEach((feed) => {
- const $ = cheerioLoad(feed.content ?? '');
- const linksInFeed = $('a')
- .map((_, elm) => elm.attribs.href)
- .toArray();
- const idOfBlogPost = feed.id!.replace(
- new URL(`${siteConfig.baseUrl}blog`, siteConfig.url).href,
- '',
- );
- const originalLinksInBlog = originalLinksInBlogs[idOfBlogPost];
- const {permalink = ''} =
- blogPostsWithLinks.find((post) => post.id === idOfBlogPost)?.metadata ||
- {};
-
- originalLinksInBlog!.forEach((originalLinkInBlog, idx) => {
- const linkToTest = isFullAbsolutePath(originalLinkInBlog)
- ? originalLinkInBlog
- : new URL(originalLinkInBlog, new URL(permalink, siteConfig.url))
- .href;
-
- expect(linkToTest).toEqual(linksInFeed[idx]);
- });
- });
- expect(
- fsMock.mock.calls.map((call) => call[1] as string),
- ).toMatchSnapshot();
- fsMock.mockClear();
- });
-});
From af9cbe28b8193e53e7c61410f40fc50921e23526 Mon Sep 17 00:00:00 2001
From: VinceCYLiao
Date: Sun, 23 Jul 2023 16:27:25 +0800
Subject: [PATCH 05/19] chore: update fixture
---
.../website/blog/blog-with-links.md | 11 ---------
.../website/blog/blog-with-links.mdx | 22 +++++++++++++++++
.../blog/blog-with-links/index.html | 24 +++++++++++++------
3 files changed, 39 insertions(+), 18 deletions(-)
delete mode 100644 packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/website/blog/blog-with-links.md
create mode 100644 packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/website/blog/blog-with-links.mdx
diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/website/blog/blog-with-links.md b/packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/website/blog/blog-with-links.md
deleted file mode 100644
index 651c4cbe5525..000000000000
--- a/packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/website/blog/blog-with-links.md
+++ /dev/null
@@ -1,11 +0,0 @@
----
-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)
-
-[anchor](#title)
diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/website/blog/blog-with-links.mdx b/packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/website/blog/blog-with-links.mdx
new file mode 100644
index 000000000000..5292e8b59a2d
--- /dev/null
+++ b/packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/website/blog/blog-with-links.mdx
@@ -0,0 +1,22 @@
+import dino from "../static/img/docusaurus.png";
+import useBaseUrl from '@docusaurus/useBaseUrl';
+
+---
+title: test links
+date: 2023-07-23
+---
+[absolute full url](https://github.com/facebook/docusaurus)
+
+[absolute url with implicit domain name](/blog/2017/12/14/introducing-docusaurus)
+
+[relative url](2017-12-14-introducing-docusaurus.mdx)
+
+[anchor](#title)
+
+![](/img/docusaurus.png)
+
+![](https://docusaurus.io/assets/images/slash-introducing-411a16dd05086935b8e9ddae38ae9b45.svg)
+
+
+
+
diff --git a/packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/website/build-snap/blog/blog-with-links/index.html b/packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/website/build-snap/blog/blog-with-links/index.html
index 3ba7d0584081..9d37b3731fd4 100644
--- a/packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/website/build-snap/blog/blog-with-links/index.html
+++ b/packages/docusaurus-plugin-content-blog/src/__tests__/__fixtures__/website/build-snap/blog/blog-with-links/index.html
@@ -3,19 +3,29 @@
-Test if href in feed resolved correctly | Docusaurus blog only!
+blog-with-links | Docusaurus blog only!
-
-
+
+
-
+