Skip to content

Next.js component to fetch and render Markdown and MDX from a different repository or a folder

License

Notifications You must be signed in to change notification settings

zentered/next-product-docs

 
 

Repository files navigation

test status test status Semantic Release bagde

Next.js Component for Product Docs

This component helps you to render your product documentation (/docs) on your product website. Check out these docs to see a full impression what this component does.

Typically the website and product (docs) are in separate repositories. This library offers three key functions:

  1. staticPaths returns all available paths for static site generation in next.js
  2. pageProps returns the required content such as sidebar routes, Table of Contents and Markdown
  3. Documentation is a JSX function that contains the render function

You can read more about the whys and hows of this component in the Zentered Blog

Prerequisites

The main purpose of this component is to fetch and render Markdown from a different repo. In this folder you need to create a manifest.json file which contains the link structure for the documentation you want to show. This allows you to control the sidebar levels and titles for links.

Sample:

/docs
/docs/README.md
...

Manifest:

{
  "routes": [
    {
      "heading": true,
      "title": "Next Docs Documentation",
      "routes": [
        {
          "title": "Introduction",
          "path": "/docs/README.md"
        }
      ]
    }
  ]
}

You can find complete example here or check out the Example Documentation Manifest.

Installation & Usage

In your Next.js website repo, run:

npm install @zentered/next-product-docs

The location of your product docs can be configured through JSX properties. If you want to access docs on a private repository, you'll need to provide a GITHUB_TOKEN environment variable.

There are two possible ways to retrieve docs:

Documentation in the same repository as the website

In this example, the documentation content is stored in content/docs/:

const defaults = {
  docsFolder: 'docs',
  rootPath: 'content'
}

Documentation in a remote repository

Another example where documentation content is stored at @zentered/next-product-docs-example/content/docs/:

  org: 'zentered,
  repo: 'next-product-docs-example',
  tag: 'main',
  docsFolder: 'docs',
  rootPath: 'content'

Additional options

  skipPathPrefix: false,

Can be used if the docs are not located in the root of the repository.

  useMDX: true,

Limit the level of headings for the Table of Contents

  tocMaxDepth: 3,

Switch from Markdown to MDX.

  trailingSlash: false, // add a trailing slash to all pages
  assetsDestination: null, // use a CDN for assets
  debug: process.env.DEBUG === true

Full Example "[[...slug.jsx]]

import Head from 'next/head'
import { pageProps, staticPaths } from 'next-product-docs/serialize'
import Documentation from 'next-product-docs'

const docsOptions = {
  docsFolder: 'docs',
  rootPath: 'content'
}

export default function Docs({ title, source }) {
  return (
    <main>
      <Documentation source={source} />
    </main>
  )
}

export async function getStaticPaths() {
  const paths = await staticPaths(docsOptions)
  return { paths, fallback: false }
}

export async function getStaticProps(ctx) {
  const props = await pageProps(ctx, docsOptions)
  return { props }
}

Run Locally

Setup

    # build dist folder
    npm build

    # create system-wide link to project folder
    npm link

    # in the website/next project folder
    npm link @zentered/next-product-docs ../next-product-docs

    # for continous monitoring in next-product-docs
    npm run watch

Additional Components

For convenience we're providing two additional components that help you get started with a sidebar and table of contents. They contain styling classes, so you should customize them as you

You can modify the page [[slug]].jsx and pass on sidebarRoutes and tocHeadings, which contain the (nested) routes for the sidebar and toc. We're using react-scroll to highlight the current section of the page in the table of contents. In the sidebar you can easily integrate search for example with Algolia React InstantSearch.

Code Import

You can import code from external files:

This only works when working with a local folder, not by fetching the docs from a remote repository. Check out remark-code-import for further information.

Contributing

Considering contributing to next-product-docs? We'd love to work with you!

To start a local development environment, have a look at our example repo on how to link/unlink the component in a Next.js project.

You can also ping us on Twitter @zenteredco. The only workaround we have at the moment is forking the repository, publish new package versions to GitHub and install them in the Next.js project where we use the component.

Please adhere to the code of conduct.

Acknowledgements & Thanks

About

Next.js component to fetch and render Markdown and MDX from a different repository or a folder

Topics

Resources

License

Stars

Watchers

Forks

Languages

  • JavaScript 99.6%
  • Shell 0.4%