mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-05 04:12:53 +02:00
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:
parent
2528226eeb
commit
ab1dada1aa
8 changed files with 33 additions and 1 deletions
|
@ -35,3 +35,7 @@
|
|||
[](/staticAssetImage.png)
|
||||
|
||||
[<span style={{color: "red"}}>Stylized link to asset file</span>](./asset.pdf)
|
||||
|
||||
[json](./data.json)
|
||||
|
||||
[static json](/staticjson.json)
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
{"hello": "world"}
|
|
@ -0,0 +1 @@
|
|||
{"static": ["json", 1]}
|
|
@ -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>
|
||||
"
|
||||
`;
|
||||
|
|
|
@ -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);
|
||||
|
|
8
website/_dogfooding/_pages tests/_script.js
Normal file
8
website/_dogfooding/_pages tests/_script.js
Normal 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;
|
3
website/_dogfooding/_pages tests/data.json
Normal file
3
website/_dogfooding/_pages tests/data.json
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"hello": "world"
|
||||
}
|
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue