mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-24 22:46:57 +02:00
fix: preserve hash in asset link (#5690)
This commit is contained in:
parent
b40e45943e
commit
67403b3b65
3 changed files with 16 additions and 4 deletions
|
@ -14,7 +14,9 @@ exports[`transformAsset plugin transform md links to <a /> 1`] = `
|
|||
|
||||
<a target=\\"_blank\\" href={require('!file-loader?name=assets/files/[name]-[hash].[ext]!./asset.pdf').default}>asset</a>
|
||||
|
||||
<a target=\\"_blank\\" href={require('!file-loader?name=assets/files/[name]-[hash].[ext]!./asset.pdf').default}title=\\"Title\\">asset</a>
|
||||
<a target=\\"_blank\\" href={require('!file-loader?name=assets/files/[name]-[hash].[ext]!./asset.pdf').default + '#page=2'}>asset with hash</a>
|
||||
|
||||
<a target=\\"_blank\\" href={require('!file-loader?name=assets/files/[name]-[hash].[ext]!./asset.pdf').default} title=\\"Title\\">asset</a>
|
||||
|
||||
## Heading
|
||||
|
||||
|
@ -32,6 +34,8 @@ exports[`transformAsset plugin transform md links to <a /> 1`] = `
|
|||
|
||||
<a target=\\"_blank\\" href={require('!file-loader?name=assets/files/[name]-[hash].[ext]!./static/staticAsset.pdf').default}>@site/static/staticAsset.pdf</a>
|
||||
|
||||
<a target=\\"_blank\\" href={require('!file-loader?name=assets/files/[name]-[hash].[ext]!./static/staticAsset.pdf').default + '#page=2'} title=\\"Title\\">@site/static/staticAsset.pdf</a>
|
||||
|
||||
<a target=\\"_blank\\" href={require('!file-loader?name=assets/files/[name]-[hash].[ext]!./static/staticAsset.pdf').default}>Just staticAsset.pdf</a>, and <a target=\\"_blank\\" href={require('!file-loader?name=assets/files/[name]-[hash].[ext]!./static/staticAsset.pdf').default}><strong>awesome</strong> staticAsset 2.pdf 'It is really "AWESOME"'</a>, but also <a target=\\"_blank\\" href={require('!file-loader?name=assets/files/[name]-[hash].[ext]!./static/staticAsset.pdf').default}>coded <code>staticAsset 3.pdf</code></a>
|
||||
|
||||
<a target=\\"_blank\\" href={require('!file-loader?name=assets/files/[name]-[hash].[ext]!./static/staticAssetImage.png').default}><img alt={\\"Clickable Docusaurus logo\\"} src={require(\\"!url-loader?limit=10000&name=assets/images/[name]-[hash].[ext]&fallback=file-loader!./static/staticAssetImage.png\\").default} /></a>
|
||||
|
|
|
@ -4,6 +4,8 @@
|
|||
|
||||
[asset](./asset.pdf)
|
||||
|
||||
[asset with hash](./asset.pdf#page=2)
|
||||
|
||||
[asset](asset.pdf 'Title')
|
||||
|
||||
## Heading
|
||||
|
@ -22,6 +24,8 @@
|
|||
|
||||
[@site/static/staticAsset.pdf](@site/static/staticAsset.pdf)
|
||||
|
||||
[@site/static/staticAsset.pdf](@site/static/staticAsset.pdf#page=2 'Title')
|
||||
|
||||
[Just staticAsset.pdf](/staticAsset.pdf), and [**awesome** staticAsset 2.pdf 'It is really "AWESOME"'](/staticAsset.pdf), but also [coded `staticAsset 3.pdf`](/staticAsset.pdf)
|
||||
|
||||
[](/staticAssetImage.png)
|
||||
|
|
|
@ -23,6 +23,7 @@ import type {Link, Literal} from 'mdast';
|
|||
const {
|
||||
loaders: {inlineMarkdownLinkFileLoader},
|
||||
} = getFileLoaderUtils();
|
||||
const hashRegex = /#.*$/;
|
||||
|
||||
interface PluginOptions {
|
||||
filePath: string;
|
||||
|
@ -56,6 +57,9 @@ function toAssetRequireNode({
|
|||
let relativeRequireAssetPath = posixPath(
|
||||
path.relative(path.dirname(filePath), requireAssetPath),
|
||||
);
|
||||
const hash = hashRegex.test(node.url)
|
||||
? node.url.substr(node.url.indexOf('#'))
|
||||
: '';
|
||||
|
||||
// nodejs does not like require("assets/file.pdf")
|
||||
relativeRequireAssetPath = relativeRequireAssetPath.startsWith('.')
|
||||
|
@ -64,9 +68,9 @@ function toAssetRequireNode({
|
|||
|
||||
const href = `require('${inlineMarkdownLinkFileLoader}${escapePath(
|
||||
relativeRequireAssetPath,
|
||||
)}').default`;
|
||||
)}').default${hash ? ` + '${hash}'` : ''}`;
|
||||
const children = stringifyContent(node);
|
||||
const title = node.title ? `title="${escapeHtml(node.title)}"` : '';
|
||||
const title = node.title ? ` title="${escapeHtml(node.title)}"` : '';
|
||||
|
||||
(node as unknown as Literal).type = 'jsx';
|
||||
(
|
||||
|
@ -82,7 +86,7 @@ async function convertToAssetLinkIfNeeded({
|
|||
staticDir,
|
||||
filePath,
|
||||
}: {node: Link} & PluginOptions) {
|
||||
const assetPath = node.url;
|
||||
const assetPath = node.url.replace(hashRegex, '');
|
||||
|
||||
const hasSiteAlias = assetPath.startsWith('@site/');
|
||||
const hasAssetLikeExtension =
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue