facebook / docusaurus

Easy to maintain open source documentation websites.
https://docusaurus.io
MIT License
56.68k stars 8.52k forks source link

Templating page elements for commonly re-used elements #3518

Closed Southclaws closed 4 years ago

Southclaws commented 4 years ago

🚀 Feature

In our wiki, we have many pages with the same warnings on.

:::warning
This function was added in version 1.2.3 and will not work in previous versions.
:::

For example.

We would like to template this to make it easier to add (and maybe update in future) just like MediaWiki.

Have you read the Contributing Guidelines on issues?

Yes.

Motivation

To save time.

Pitch

It's a neat way to save time when using the same constructs everywhere.

We tried to do it with MDX and building a component but couldn't seem to figure out how to export an admonition.

slorber commented 4 years ago

Hi,

Is this what you are looking for?

https://v2.docusaurus.io/docs/markdown-features/#calloutsadmonitions

We have "caution", not "warning" (I often do the mistake btw, don't really like "caution")

Southclaws commented 4 years ago

No I mean I want to take one of these and template it. We have the same text in 100 pages, authors either copy paste or write again (which leads to slightly different admonitions). Templates would allow us to write one commonly used admonition (or any other type of markup) and place it everywhere (presumably using MDX).

MediaWiki has this feature, it's called templates too https://www.mediawiki.org/wiki/Help:Templates

I know you can use .mdx as the extension and import React components. But it seems using admonitions and other docusaurus things doesn't work.

slorber commented 4 years ago

Hi,

You could write a Remark plugin to transform the input MDX to whatever you want.

For example:

# markdown title

markdown text

{{versionWarning}}

Would become

# markdown title

markdown text

:::warning

This function was added in version 1.2.3 and will not work in previous versions.

:::

Or

# markdown title

markdown text

import VersionWarning from '@site/src/VersionWarning'

<VersionWarning/>

But at the end of the day, your MD document ends up being transpiled to JSX, so you could as well write it in the 3rd form directly.

Note: you can import one MDX from the other too, see for example https://v2.docusaurus.io/examples/markdownPageExample#import-mdx-and-md-files (but it must be in the same "scope" as the original md docs currently, so you should rather put the 2 files in ./docs folder)

slorber commented 4 years ago

See also a simple remark plugin to do that for you:

https://github.com/angeloashmore/gatsby-remark-find-replace

(looks to me it can be used without Gatsby)

Southclaws commented 4 years ago

Oh, that cross-mdx import is probably what I want, thanks!

slorber commented 4 years ago

please let me know if this works for you, and if we can close this issue ;)

Note, it's not very well documented but you can also import one MDX file from another (there may be some rough edges in our integration but it should work)

Southclaws commented 4 years ago

It looks like it works well! Thanks!