Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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.

Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,29 @@ describe('transformLinks plugin', () => {
expect(result).toMatchInlineSnapshot(`"[file](dir/file.zip)"`);
});

it('does not transform existing dotted directory links to asset requires', async () => {
const result = await processContent(
`[directory](../dotted-directory.whatever)`,
);
expect(result).toMatchInlineSnapshot(
`"[directory](../dotted-directory.whatever)"`,
);
});

it('does not transform absolute dotted directory links to asset requires', async () => {
const result = await processContent(
`[directory](/static-dotted-directory.test)`,
);
expect(result).toMatchInlineSnapshot(
`"[directory](/static-dotted-directory.test)"`,
);
});

describe('onBrokenMarkdownLinks', () => {
const fixtures = {
urlEmpty: `[empty]()`,
fileDoesNotExistSiteAlias: `[file](@site/file.zip)`,
directoryWithDotSiteAlias: `[dir](@site/dotted-directory.whatever)`,
};

describe('throws', () => {
Expand All @@ -103,6 +122,15 @@ describe('transformLinks plugin', () => {
To ignore this error, use the \`siteConfig.markdown.hooks.onBrokenMarkdownLinks\` option, or apply the \`pathname://\` protocol to the broken link URLs."
`);
});

it('if site alias points to a directory with a dot', async () => {
await expect(processContent(fixtures.directoryWithDotSiteAlias)).rejects
.toThrowErrorMatchingInlineSnapshot(`
"Markdown link with URL \`@site/dotted-directory.whatever\` in source file "packages/docusaurus-mdx-loader/src/remark/transformLinks/__tests__/__fixtures__/docs/myFile.mdx" (1:1) couldn't be resolved.
Make sure it references a local Markdown file that exists within the current plugin.
To ignore this error, use the \`siteConfig.markdown.hooks.onBrokenMarkdownLinks\` option, or apply the \`pathname://\` protocol to the broken link URLs."
`);
});
});

describe('warns', () => {
Expand Down Expand Up @@ -141,6 +169,22 @@ describe('transformLinks plugin', () => {
]
`);
});

it('if site alias points to a directory with a dot', async () => {
const result = await processWarn(fixtures.directoryWithDotSiteAlias);
expect(result).toMatchInlineSnapshot(
`"[dir](@site/dotted-directory.whatever)"`,
);
expect(warnMock).toHaveBeenCalledTimes(1);
expect(warnMock.mock.calls).toMatchInlineSnapshot(`
[
[
"[WARNING] Markdown link with URL \`@site/dotted-directory.whatever\` in source file "packages/docusaurus-mdx-loader/src/remark/transformLinks/__tests__/__fixtures__/docs/myFile.mdx" (1:1) couldn't be resolved.
Make sure it references a local Markdown file that exists within the current plugin.",
],
]
`);
});
});

describe('function form', () => {
Expand Down Expand Up @@ -269,6 +313,60 @@ describe('transformLinks plugin', () => {
]
`);
});

it('if site alias points to a directory with a dot', async () => {
const result = await processWarn(fixtures.directoryWithDotSiteAlias);
expect(result).toMatchInlineSnapshot(
`"[dir](/404 "fixed link title")"`,
);
expect(logMock).toHaveBeenCalledTimes(1);
expect(logMock.mock.calls).toMatchInlineSnapshot(`
[
[
"onBrokenMarkdownLinks called with",
{
"node": {
"children": [
{
"position": {
"end": {
"column": 5,
"line": 1,
"offset": 4,
},
"start": {
"column": 2,
"line": 1,
"offset": 1,
},
},
"type": "text",
"value": "dir",
},
],
"position": {
"end": {
"column": 39,
"line": 1,
"offset": 38,
},
"start": {
"column": 1,
"line": 1,
"offset": 0,
},
},
"title": "fixed link title",
"type": "link",
"url": "/404",
},
"sourceFilePath": "packages/docusaurus-mdx-loader/src/remark/transformLinks/__tests__/__fixtures__/docs/myFile.mdx",
"url": "@site/dotted-directory.whatever",
},
],
]
`);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -171,26 +171,38 @@ async function toAssetRequireNode(
});
}

/**
* Checks if the given file path exists and is a file.
* Returns true if it exists, false otherwise.
*/
async function isExistingFile(filePath: string): Promise<boolean> {
try {
return (await fs.stat(filePath)).isFile();
} catch {
return false;
}
}

async function getLocalFileAbsolutePath(
assetPath: string,
{siteDir, filePath, staticDirs}: Context,
) {
if (assetPath.startsWith('@site/')) {
const assetFilePath = path.join(siteDir, assetPath.replace('@site/', ''));
if (await fs.pathExists(assetFilePath)) {
if (await isExistingFile(assetFilePath)) {
return assetFilePath;
}
} else if (path.isAbsolute(assetPath)) {
const assetFilePath = await findAsyncSequential(
staticDirs.map((dir) => path.join(dir, assetPath)),
fs.pathExists,
isExistingFile,
);
if (assetFilePath) {
return assetFilePath;
}
} else {
const assetFilePath = path.join(path.dirname(filePath), assetPath);
if (await fs.pathExists(assetFilePath)) {
if (await isExistingFile(assetFilePath)) {
return assetFilePath;
}
}
Expand Down