GithubHelp home page GithubHelp logo

docusaurus-plugin-includes's Introduction

docusaurus-plugin-includes

Plugin for Docusaurus to include the content of markdown in other markdown files.

Other than including files using MDX transclusion, this plugin ensures that the content of the included markdown file also appears in the table of contents on the right side of the docusaurus page.

The plugin is intended to have shared content (e.g. chapters), that appears in multiple places of the same documentation, as well as shared content that is used in documentation of multiple websites.

In order that docusaurus can archive all used markdown files into versioned_docs folder when creating a new version, the included files also have to be located inside of the docs folder. So we need to copy the files shared with other products from outside into a subfolder of docs before we can use them. The plugin can also do this copy job for configured folders.

After build, the shared folders also appear in the build folder. The plugin can remove post build specific folders from build folder.

Because documentation blocks shared across multiple websites often contain project specific words like the project name, the plugin can also replace placeholders with project specific replacements specified in the docusaurus configuration file.

The plugin also supports placeholders in remarkable-embed style {@myPlugin: slug} syntax, allowing you to embed rich content in your documents with your own JavaScript plugin function.

Usage

Compile, copy, install as local plugin

yarn add docusaurus-plugin-includes

or

npm install docusaurus-plugin-includes --save

Then adapt your docusaurus.config.js with the following block:

plugins: [
    [
      'docusaurus-plugin-includes',
      {
        sharedFolders: [
          { source: '../../_shared', target: '../docs/shared'},
        ],

        postBuildDeletedFolders: ['shared'],

        replacements: [
          { key: '{ProductName}', value: 'My long product name for XYZ' },
          { key: '{ShortName}', value: 'XYZ' },
        ],

        embeds: [
          {
            key: 'myAwesomePlugin',
            embedFunction: function(code) { 
              return `...`;
            }
          }
        ],
        injectedHtmlTags: {
          preBodyTags: [`<link rel="stylesheet" href="https://cdn.example.com/style.css" type="text/css">`]
        }
      },
    ],
  ],

Include markdown files in other markdown files

Add the following at the position where another file should be included:

{@include: pathRelativeToDocsFolder/markdownfile.md}

A real world sample is {@include: shared/finesse_compatibility.md}. The path is relative from the main docs folder. In the sample above, the included file is located in a subfolder shared in the main docs folder.

The included markdown files must be plain markdown files without docusaurus headers with tags like idand title.

Included files are allowed to again include other files. Make sure to avoid endless include loops.

Copy shared folder(s) from outside into docs folder

The shared files must also be located in the main docs folder to make sure they are also copied automatically from docusaurus into the versioned_docs folder when creating a version. Markdown files shared with other external product documentations must therefore somehow be copied from outside into a subfolder of the main docs folder.

The plugin adds 2 command line commands to automate copying these folder(s).

  • includes:copySharedFolders: Copy the configured shared folders
  • includes:cleanCopySharedFolders: Delete existing target folders first, copySharedFolders

The folders to copy can be configured in plugins configuration in docusaurus.config.js file.

  sharedFolders: [
    { source: '../../_shared', target: '../docs/shared'},
  ]

Source and target path are defined relative to the website folder where also the file docusaurus.config.js is located.

Delete shared folders post build from build output directory

After docusaurus build, the shared folders also exist in the final build output directory, but we don't want that,

The plugin can also delete configured folders from all version subfolders of docusaurus build output. Configure here the subfolder names (in docs folder of build output) that have to be deleted:

  postBuildDeletedFolders: ['shared']

Replace placeholders

Because we often need the product name or the CRM name in the documentation, we need the possibility to add placeholders in the shared markdown files that will be replaced with the value for the current product.

The plugin also allows such placeholder replacements configured in docusaurus.config.js file.

  replacements: [
    { key: '{ProductName}', value: 'My Awesome Project' },
    { key: '{SolutionName}', value: 'My Awesome Solution' },
  ]

Replace remarkable-embed style placeholders

The plugin also supports placeholders in remarkable-embed style {@myPlugin: slug} syntax, allowing you to embed rich content in your documents with your own JavaScript plugin function.

You can configure such placeholder replacements in docusaurus.config.js file.

  embeds: [
    {
      key: 'myAwesomePlugin',
      embedFunction: function(code) { 
        return `...`;
      }
    }
  ]

The following sample configuration adds plugin code to embed video files from assets folder with syntax {@video: filename} and youtube videos with syntax {@youtube: videocode}.

  embeds: [
    {
      key: 'video',
      embedFunction: function(code) {
        return `<video width="785" height="588" controls loop controlsList="nodownload">
                  <source type="video/mp4" src={require('./assets/${code}').default}></source>
                </video>`;
      }
    },
    {
      key: 'youtube',
      embedFunction: function(code) {
        return '<iframe width="785" height="440" type="text/html" frameborder="0" src="https://www.youtube.com/embed/' + code + '"></iframe>'
      }
    }
  ]

Inject HTML Tags

Inject head and/or body HTML tags to Docusaurus generated HTML.

The following 3 identifiers can be used and they are all optional:

  • headTags
  • preBodyTags
  • postBodyTags

See https://docusaurus.io/docs/lifecycle-apis#injecthtmltags for more information.

In the following example a CSS file gets loaded in the head tag and a h1 tag gets added to the body:

  injectedHtmlTags: {
    headTags: [`<link rel="stylesheet" href="https://cdn.example.com/style.css" type="text/css">`],
    preBodyTags: [`<h1>Test</h1>`]
  }

docusaurus-plugin-includes's People

Contributors

matthiasbauerbs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

docusaurus-plugin-includes's Issues

Plugin not working in combination with plugin-content-docs?

Hi,
great plugin, but somehow I can't get it to work in combination with the content-docs plugin.
Our repo has a default docs directory and on build it fetches docs from other repos (which get added to the external directory).
This plugin only works for the documentation passed to the docs object in the classic preset but not with the docs in the content-docs plugin. They should be the same, right? Or did I misunderstand something?

In this config the include plugin works for the default docs:

  presets: [
    [
      '@docusaurus/preset-classic',
      {
        docs: {
          id: "docs",
          sidebarPath: require.resolve('./sidebars.js'),
        },
        theme: {
          customCss: require.resolve('./src/css/custom.css'),
        },
      },
    ]
  ],
  plugins: [
    [
      '@docusaurus/plugin-content-docs',
      {
        id: 'doc2',
        path: 'external/docs2/docs',
        routeBasePath: 'docs2',
        sidebarPath: require.resolve('./external/doc2/sidebars.js'),
      }
    ],
  ]

But if I simply replace the docs it works for the docs2:

  presets: [
    [
      '@docusaurus/preset-classic',
      {
        docs: {
           id: 'doc2',
           path: 'external/docs2/docs',
           routeBasePath: 'docs2',
           sidebarPath: require.resolve('./external/doc2/sidebars.js'),
        },
        theme: {
          customCss: require.resolve('./src/css/custom.css'),
        },
      },
    ]
  ],
  plugins: [
    [
      '@docusaurus/plugin-content-docs',
      {
        id: "docs",
        sidebarPath: require.resolve('./sidebars.js'),
      }
    ],
  ]

Thanks for your help in advance

Documents without frontmatter do not render

In upgrading from 1.13 to 1.14 markdown pages no longer have any content in them unless the page has frontmatter metadata. This appears to come from the change introduced here: https://github.com/simologos/docusaurus-plugin-includes/blob/master/src/includesLoader.ts#L27-L31

If I understand the comment correctly, it looks like you are trying to prevent "included" markdown files from being picked up as documents, but in my case the existing documents are still found but just don't have content in them.

It seems that another approach is needed, but in the mean time could you add a note to the README that clearly states that documentation pages must have front matter for the plugin to work?

Question: Unable to get the plugin to work, missing steps?

My team would love to use this plugin, but we have yet to get it to work with Docusaurus. We tried it with versions from the dependency version (.72) through to Beta. We have it added via yarn, in package.json, configured using your code/instructions from the readme, and building without errors. But all {@include filelocation/filename.md} and variable entries are generating as plain text.

Files are not being embedded, placeholders are not replacing.

Is there a specific import line to include in the markdown files? Additional configurations to take? Do you have an example demo working with it somewhere? Thanks!

import mdx function doesn't work

Hello,

I am using this plugin just fine. thank you very much.

However, when using this plugin, the native mdx file import function in docusaurus does not work.

import Test from './test.mdx'

<Test />

Code like above is not rendering in html. I need to use that code instead of include. Required when passing a variable.

import Test from './test.mdx'

<Test name='ABCD' />

is it possible to modify?

Cant be used with React version >17.0.1

Please check and update peerDependencies for React. It cant be used with the recent React version 18.2.0 anymore.

  "peerDependencies": {
    "react": "^16.8.4 || ^17.0.0",
    "react-dom": "^16.8.4 || ^17.0.0"

It breaks:

npm install
npm ERR! code ERESOLVE
npm ERR! ERESOLVE could not resolve
npm ERR!
npm ERR! While resolving: [email protected]
npm ERR! Found: [email protected]
npm ERR! node_modules/react
npm ERR!   react@"^18.2.0" from the root project
npm ERR!   peerOptional react@">= 16.8.0 < 19.0.0" from @docsearch/[email protected]
npm ERR!   node_modules/@docsearch/react
npm ERR!     @docsearch/react@"^3.5.2" from @docusaurus/[email protected]
npm ERR!     node_modules/@docusaurus/theme-search-algolia
npm ERR!       @docusaurus/theme-search-algolia@"3.2.1" from @docusaurus/[email protected]
npm ERR!       node_modules/@docusaurus/preset-classic
npm ERR!         @docusaurus/preset-classic@"3.2.1" from the root project
npm ERR!   26 more (@docusaurus/core, @docusaurus/mdx-loader, ...)
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! peer react@"^16.8.4 || ^17.0.0" from [email protected]
npm ERR! node_modules/docusaurus-plugin-includes
npm ERR!   docusaurus-plugin-includes@"^1.1.5" from the root project
npm ERR!
npm ERR! Conflicting peer dependency: [email protected]
npm ERR! node_modules/react
npm ERR!   peer react@"^16.8.4 || ^17.0.0" from [email protected]
npm ERR!   node_modules/docusaurus-plugin-includes
npm ERR!     docusaurus-plugin-includes@"^1.1.5" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.

Thanks!

IMG files in shared .md files have broken image refs.

Hi there,

I have a shared image in that static/img/ directory that I want to use across a shared document.

It works on preview in VS Code, but upon build the file isn't found.

For example, I have a shared/info-tab.md with an image ref. That md file is included in the file, but I get the following return:

Cause: Image docs/data-syncs/static/img/info-tab.png used in docs/data-syncs/data-sync-sources/dynamodb.mdx not found.
Details:
Error: Image docs/data-syncs/static/img/info-tab.png used in docs/data-syncs/data-sync-sources/dynamodb.mdx not found.
    at async Promise.all (index 0)

Any ideas on how to recitfy this? Am I implementing it wrong?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.