Docusaurus: How can I display a Markdown file inline inside a Markdown page?

In my Docusaurus website, I want to have a Markdown file stored in one place and displayed in six places. How do I do this (a) in Docusaurus, (b) in HTML (c) a better way?
My attempt:
In srccomponentsMyFile.js:

    import React from 'react';
    import Content from '../../docs/myFile.md';

    const MyFile = () => <Content />;

    export default MyFile;

In docsmyFile.md:

Hello world

in docsanotherFile.md:

    import MyFile from './myFile.md';
    <MyFile />

Result: ‘unexpected error’ during Vercel deploy

New contributor

Neil Sandy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

You can import Markdown content using .mdx components. There are other ways to do this, but this method is also referenced by the folks at Docusaurus.

You will need to use .mdx files for this.

Here’s a simple example of how to do this.


Example:

Let’s say you have an “export” file, a file that will not be published to your Docusaurus site but will have its contents reused elsewhere.

NOTE: All files created will go into your /docs folder.

  1. Let’s create this file and call it: _markdown-partial-example.mdx. The _ filename prefix makes it so that it will not create a doc page for this file. It will not be published.

  2. Now, let’s create a file that will contain the _markdown-partial-example.mdx‘s imported contents. Let’s call that file: someOtherDoc.mdx

  3. Populate our reusable content file, _markdown-partial-example.mdx:

// ._markdown-partial-example.mdx
------------------------------------------------------------------------------

<span>Hello {props.name}</span>

This is text some content from `_markdown-partial-example.mdx`.

  1. Now, populate the “import” file, someOtherDoc.mdx:
// someOtherDoc.mdx
------------------------------------------------------------------------------

import PartialExample from './_markdown-partial-example.mdx';

<PartialExample name="User" />

Result

You Docusaurus site will create a page for the someOtherDoc.mdx file and use the contents of imported from _markdown-partial-example.mdx.

This method is also described in Docusaurus’s docs.

This answer was given to me by a colleague.
The main points are:

  1. This works for .mdx files. It may also work for .md files, since I believe Docusaurus treats them both the same, but it is neater and clearer to stick to .mdx.

  2. Prepend an underscore character to make Docusaurus ignore a file. For example, _partial.mdx.

  3. In the file you want to use (renderable.mdx for example), code like this:

    above
    “`mdx-code-block
    import SomeText from “./_partial.mdx”

    <SomeText />
    ```
    below
    

The contents of the file _partial.mdx will be displayed between ‘above’ and ‘below’.

Because of the underscore character, the partial file will not be displayed on its own; it will only be displayed when it is included in another file, as shown.

New contributor

Neil Sandy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật