mirror of
https://github.com/facebook/docusaurus.git
synced 2025-04-30 10:48:05 +02:00
fix(content-blog): make RSS feed generation work with slugs with .html extension (#8158)
Co-authored-by: sebastienlorber <lorber.sebastien@gmail.com>
This commit is contained in:
parent
c325f87fd8
commit
efbd0cbd40
5 changed files with 64 additions and 13 deletions
1
packages/docusaurus-utils/src/__tests__/__fixtures__/build-snap/htmlFile.html
generated
Normal file
1
packages/docusaurus-utils/src/__tests__/__fixtures__/build-snap/htmlFile.html
generated
Normal file
|
@ -0,0 +1 @@
|
||||||
|
htmlFile.html
|
|
@ -0,0 +1 @@
|
||||||
|
nestedHtmlFile.html
|
|
@ -73,6 +73,38 @@ describe('readOutputHTMLFile', () => {
|
||||||
).then(String),
|
).then(String),
|
||||||
).resolves.toBe('file\n');
|
).resolves.toBe('file\n');
|
||||||
});
|
});
|
||||||
|
it('reads file ending in .html', async () => {
|
||||||
|
await expect(
|
||||||
|
readOutputHTMLFile(
|
||||||
|
'/htmlFile.html',
|
||||||
|
path.join(__dirname, '__fixtures__/build-snap'),
|
||||||
|
false,
|
||||||
|
).then(String),
|
||||||
|
).resolves.toBe('htmlFile.html\n');
|
||||||
|
await expect(
|
||||||
|
readOutputHTMLFile(
|
||||||
|
'/htmlFile.html',
|
||||||
|
path.join(__dirname, '__fixtures__/build-snap'),
|
||||||
|
undefined,
|
||||||
|
).then(String),
|
||||||
|
).resolves.toBe('htmlFile.html\n');
|
||||||
|
});
|
||||||
|
it('reads file ending in .html in folder containing .html', async () => {
|
||||||
|
await expect(
|
||||||
|
readOutputHTMLFile(
|
||||||
|
'/weird.html.folder/nestedHtmlFile.html',
|
||||||
|
path.join(__dirname, '__fixtures__/build-snap'),
|
||||||
|
undefined,
|
||||||
|
).then(String),
|
||||||
|
).resolves.toBe('nestedHtmlFile.html\n');
|
||||||
|
await expect(
|
||||||
|
readOutputHTMLFile(
|
||||||
|
'/weird.html.folder/nestedHtmlFile.html',
|
||||||
|
path.join(__dirname, '__fixtures__/build-snap'),
|
||||||
|
undefined,
|
||||||
|
).then(String),
|
||||||
|
).resolves.toBe('nestedHtmlFile.html\n');
|
||||||
|
});
|
||||||
// Can it ever happen?
|
// Can it ever happen?
|
||||||
it('throws if file does not exist', async () => {
|
it('throws if file does not exist', async () => {
|
||||||
await expect(
|
await expect(
|
||||||
|
@ -82,7 +114,7 @@ describe('readOutputHTMLFile', () => {
|
||||||
undefined,
|
undefined,
|
||||||
).then(String),
|
).then(String),
|
||||||
).rejects.toThrowErrorMatchingInlineSnapshot(
|
).rejects.toThrowErrorMatchingInlineSnapshot(
|
||||||
`"Expected output HTML file to be found at <PROJECT_ROOT>/packages/docusaurus-utils/src/__tests__/__fixtures__/build-snap/nonExistent/index.html."`,
|
`"Expected output HTML file to be found at <PROJECT_ROOT>/packages/docusaurus-utils/src/__tests__/__fixtures__/build-snap/nonExistent/index.html for permalink /nonExistent."`,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -79,20 +79,22 @@ export async function readOutputHTMLFile(
|
||||||
trailingSlash: boolean | undefined,
|
trailingSlash: boolean | undefined,
|
||||||
): Promise<Buffer> {
|
): Promise<Buffer> {
|
||||||
const withTrailingSlashPath = path.join(outDir, permalink, 'index.html');
|
const withTrailingSlashPath = path.join(outDir, permalink, 'index.html');
|
||||||
const withoutTrailingSlashPath = path.join(
|
const withoutTrailingSlashPath = (() => {
|
||||||
outDir,
|
const basePath = path.join(outDir, permalink.replace(/\/$/, ''));
|
||||||
`${permalink.replace(/\/$/, '')}.html`,
|
const htmlSuffix = /\.html?$/i.test(basePath) ? '' : '.html';
|
||||||
);
|
return `${basePath}${htmlSuffix}`;
|
||||||
const HTMLPath = await findAsyncSequential(
|
})();
|
||||||
[
|
|
||||||
trailingSlash !== false && withTrailingSlashPath,
|
const possibleHtmlPaths = [
|
||||||
trailingSlash !== true && withoutTrailingSlashPath,
|
trailingSlash !== false && withTrailingSlashPath,
|
||||||
].filter((p): p is string => Boolean(p)),
|
trailingSlash !== true && withoutTrailingSlashPath,
|
||||||
fs.pathExists,
|
].filter((p): p is string => Boolean(p));
|
||||||
);
|
|
||||||
|
const HTMLPath = await findAsyncSequential(possibleHtmlPaths, fs.pathExists);
|
||||||
|
|
||||||
if (!HTMLPath) {
|
if (!HTMLPath) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Expected output HTML file to be found at ${withTrailingSlashPath}.`,
|
`Expected output HTML file to be found at ${withTrailingSlashPath} for permalink ${permalink}.`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return fs.readFile(HTMLPath);
|
return fs.readFile(HTMLPath);
|
||||||
|
|
15
website/_dogfooding/_blog tests/2022-10-02-html-slug.md
Normal file
15
website/_dogfooding/_blog tests/2022-10-02-html-slug.md
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
---
|
||||||
|
title: A post with html slug
|
||||||
|
tags: [paginated-tag]
|
||||||
|
slug: /x/y/z.html
|
||||||
|
---
|
||||||
|
|
||||||
|
# Hmmm!
|
||||||
|
|
||||||
|
This is a blog post with an html slug!
|
||||||
|
|
||||||
|
```mdx-code-block
|
||||||
|
import Partial from "./_partial.mdx"
|
||||||
|
|
||||||
|
<Partial />
|
||||||
|
```
|
Loading…
Add table
Reference in a new issue