Assets
Sometimes you want to link to static assets directly from Markdown files, and it is convenient to co-locate the asset next to the Markdown file using it.
We have setup Webpack loaders to handle most common file types, so that when you import a file, you get its url, and the asset is automatically copied to the output folder.
Let's imagine the following file structure:
# Your doc/website/docs/myFeature.mdx
# Some assets you want to use/website/docs/assets/docusaurus-asset-example-banner.png/website/docs/assets/docusaurus-asset-example-pdf.pdf
#
ImagesYou can display images in three different ways: Markdown syntax, JSX require or ES imports syntax.
Display images using simple Markdown syntax:
![Example banner](./assets/docusaurus-asset-example-banner.png)
Display images using inline CommonJS require
in JSX image tag:
<img src={require('./assets/docusaurus-asset-example-banner.png').default} alt="Example banner"/>
Display images using ES import
syntax and JSX image tag:
import myImageUrl from './assets/docusaurus-asset-example-banner.png';
<img src={myImageUrl} alt="Example banner" />
This results in displaying the image:
note
If you are using @docusaurus/plugin-ideal-image, you need to use the dedicated image component, as documented.
#
FilesIn the same way, you can link to existing assets by requiring them and using the returned url in videos, links etc.
# My Markdown page
<a target="_blank" href={require('./assets/docusaurus-asset-example-pdf.pdf').default}> Download this PDF</a>
or
[Download this PDF using Markdown](./assets/docusaurus-asset-example-pdf.pdf)
Download this PDF using Markdown
#
Inline SVGsDocusaurus supports inlining SVGs out of the box.
import DocusaurusSvg from './docusaurus.svg';
<DocusaurusSvg />;
This can be useful, if you want to alter the part of the SVG image via CSS. For example, you can change one of the SVG colors based on the current theme.
import DocusaurusSvg from './docusaurus.svg';
<DocusaurusSvg className="themedDocusaurus" />;
html[data-theme='light'] .themedDocusaurus [fill='#FFFF50'] { fill: greenyellow;}
html[data-theme='dark'] .themedDocusaurus [fill='#FFFF50'] { fill: seagreen;}
#
Themed ImagesDocusaurus supports themed images: the ThemedImage
component (included in the classic/bootstrap themes) allows you to switch the image source based on the current theme.
import ThemedImage from '@theme/ThemedImage';
<ThemedImage alt="Docusaurus themed image" sources={{ light: useBaseUrl('/img/docusaurus_light.svg'), dark: useBaseUrl('/img/docusaurus_dark.svg'), }}/>;