mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-10 23:02:56 +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);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue