fix: allow links to JSON in .md files to be transformed as asset links (#4827)

* fix: fix links to JSON from .md files
closes #3561
It seems to be a common problem that many people are having see:
https://stackoverflow.com/questions/65307533/link-to-static-json-file

Co-authored-by: Anthony McCaigue <anthony@nquiringminds.com>
Co-authored-by: Alois Klink <alois@nquiringminds.com>

* Add dogfooding examples

* actually fix

* oops

Co-authored-by: Alois Klink <alois@nquiringminds.com>
Co-authored-by: Josh-Cena <sidachen2003@gmail.com>
This commit is contained in:
Anthony McCaigue 2022-01-23 03:46:27 +00:00 committed by GitHub
parent 2528226eeb
commit ab1dada1aa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 33 additions and 1 deletions

View file

@ -35,3 +35,7 @@
[![Clickable Docusaurus logo](./static/staticAssetImage.png)](/staticAssetImage.png)
[<span style={{color: "red"}}>Stylized link to asset file</span>](./asset.pdf)
[json](./data.json)
[static json](/staticjson.json)

View file

@ -47,5 +47,9 @@ exports[`transformAsset plugin transform md links to <a /> 1`] = `
<a target=\\"_blank\\" href={require('![CWD]/node_modules/file-loader/dist/cjs.js?name=assets/files/[name]-[hash].[ext]!./static/staticAssetImage.png').default}><img alt={\\"Clickable Docusaurus logo\\"} src={require(\\"![CWD]/node_modules/url-loader/dist/cjs.js?limit=10000&name=assets/images/[name]-[hash].[ext]&fallback=[CWD]/node_modules/file-loader/dist/cjs.js!./static/staticAssetImage.png\\").default} width=\\"200\\" height=\\"200\\" /></a>
<a target=\\"_blank\\" href={require('![CWD]/node_modules/file-loader/dist/cjs.js?name=assets/files/[name]-[hash].[ext]!./asset.pdf').default}><span style={{color: \\"red\\"}}>Stylized link to asset file</span></a>
<a target=\\"_blank\\" href={require('./data.raw!=![CWD]/node_modules/file-loader/dist/cjs.js?name=assets/files/[name]-[hash].[ext]!./data.json').default}>json</a>
<a target=\\"_blank\\" href={require('./static/staticjson.raw!=![CWD]/node_modules/file-loader/dist/cjs.js?name=assets/files/[name]-[hash].[ext]!./static/staticjson.json').default}>static json</a>
"
`;

View file

@ -47,7 +47,12 @@ function toAssetRequireNode(node: Link, assetPath: string, filePath: string) {
const hash = parsedUrl.hash ?? '';
const search = parsedUrl.search ?? '';
const href = `require('${inlineMarkdownLinkFileLoader}${
const href = `require('${
// A hack to stop Webpack from using its built-in loader to parse JSON
path.extname(relativeAssetPath) === '.json'
? `${relativeAssetPath.replace('.json', '.raw')}!=`
: ''
}${inlineMarkdownLinkFileLoader}${
escapePath(relativeAssetPath) + search
}').default${hash ? ` + '${hash}'` : ''}`;
const children = stringifyContent(node);

View file

@ -0,0 +1,8 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
export default 1;

View file

@ -0,0 +1,3 @@
{
"hello": "world"
}

View file

@ -25,3 +25,9 @@ See [#3309](https://github.com/facebook/docusaurus/issues/3309)
- [pathname://../dogfooding/javadoc](pathname://../dogfooding/javadoc)
- [pathname://../dogfooding/javadoc/index.html](pathname://../dogfooding/javadoc/index.html)
## Linking to JSON
- [./script.js](./_script.js)
- [./data.json](./data.json)