Skip to content

Commit

Permalink
fix edge case
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Oct 6, 2022
1 parent 0049e94 commit 260c6a3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.

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

18 changes: 17 additions & 1 deletion packages/docusaurus-utils/src/__tests__/emitUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,22 @@ describe('readOutputHTMLFile', () => {
).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?
it('throws if file does not exist', async () => {
await expect(
Expand All @@ -98,7 +114,7 @@ describe('readOutputHTMLFile', () => {
undefined,
).then(String),
).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."`,
);
});
});
Expand Down
26 changes: 14 additions & 12 deletions packages/docusaurus-utils/src/emitUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,22 @@ export async function readOutputHTMLFile(
trailingSlash: boolean | undefined,
): Promise<Buffer> {
const withTrailingSlashPath = path.join(outDir, permalink, 'index.html');
const withoutTrailingSlashPath = path.join(
outDir,
`${permalink.replace(/\/$/, '').replace(/.html$/, '')}.html`,
);
const HTMLPath = await findAsyncSequential(
[
trailingSlash !== false && withTrailingSlashPath,
trailingSlash !== true && withoutTrailingSlashPath,
].filter((p): p is string => Boolean(p)),
fs.pathExists,
);
const withoutTrailingSlashPath = (() => {
const basePath = path.join(outDir, permalink.replace(/\/$/, ''));
const htmlSuffix = /\.html?$/i.test(basePath) ? '' : '.html';
return `${basePath}${htmlSuffix}`;
})();

const possibleHtmlPaths = [
trailingSlash !== false && withTrailingSlashPath,
trailingSlash !== true && withoutTrailingSlashPath,
].filter((p): p is string => Boolean(p));

const HTMLPath = await findAsyncSequential(possibleHtmlPaths, fs.pathExists);

if (!HTMLPath) {
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);
Expand Down

0 comments on commit 260c6a3

Please sign in to comment.