generate pages from markdown files with dynamic routes, 0 effort, 0 boilerplate.

Overview


next-markdown

Markdown Pages for Next.js

Dynamic Routes. Blog Aware. Design Your Layout

Downloads Github Stars npm (tag)

Made for people

  • having a nextjs project
  • in ❤️ with markdown
  • who want to generate boring (but very necessary!) pages like /about, /terms, /blog or /whatever/other/route from markdown files with 0 effort (eg. about.md, whatever/other/route.md)
  • (optional) who want these .md files to be hosted on a separate git repo

Currently in use in

Twitter Follow

Get Started

In your nextjs project, run

npm install next-markdown

Add the following [...nextmd].jsx file in the pages/ folder

import NextMarkdown from "next-markdown";

const nextmd = NextMarkdown({ pathToContent: "./pages-markdown" });

export const getStaticPaths = nextmd.getStaticPaths;
export const getStaticProps = nextmd.getStaticProps;

export default function MarkdownPage({ frontMatter, html, files }) {
  return <div dangerouslySetInnerHTML={{ __html: html }} /> 👈 design your own layout 🧑‍🎨
}

Usage 👋

At the root of your project create a folder pages-markdown/, add the following hello.md file

# Hello World

This is **awesome**

That's it. Open http://localhost:3000/hello page and see the magic.

Enjoy.

nextmd demo

Features 🚀

Dynamic Routes for Markdown Files

Just like nextjs does with pages/.

next-markdown generates paths based on the path of your markdown files.

For example, the following project structure will result into creating the following pages:

pages/
├ index.jsx    ......... ➡️ /
├ caveat.jsx   ......... ➡️ /caveat
├ [...nextmd].jsx

pages-markdown/
├ about.md     ......... ➡️ /about
├ caveat.md    ......... ➡️ ❌ because `pages/caveat.jsx` takes precedence over [...nextmd] cf. https://nextjs.org/docs/routing/dynamic-routes#caveats
├ hello/
  ├ index.md   ......... ➡️ /hello
  ├ world.md   ......... ➡️ /hello/world
  ├ jurassic/
    ├ park.md  ......... ➡️ /hello/jurassic/park
├ blog/
  ├ index.md   ......... ➡️ /blog with `props.files` all the files within its directory
  ├ hello.md   ......... ➡️ /blog/hello
  ├ world.md   ......... ➡️ /blog/world
├ docs/
  ├ index.md   ......... ➡️ /docs with `props.files` all the files within its directory
  ├ get-started.md   ... ➡️ /docs/get-started
  ├ features.md   ...... ➡️ /docs/features
  ├ contribute.md   .... ➡️ /docs/contribute

See the example.

Blog Aware

next-markdown is blog-aware, docs-aware, etc.:

  • list all files
  • easy to calculate the estimated reading time
  • etc.

See the example.

Table of Contents

For each page you'll receive the Table of Contents based on headings in your markdown.

See the example.

MDX Support

You can mix .md and .mdx files.

See the example.

Configure custom remark and rehype plugins

next-markdown comes with some default remark and rehype plugins to ensure its basic functionality.

In some cases you might want to specify additional plugins to enrich your page with extra features.

You can pass custom remark and rehype plugins via the next-markdown initializer config:

import NextMarkdown from "next-markdown";

const nextmd = NextMarkdown({
  ...,
  remarkPlugins: [],
  rehypePlugins: [],
});

See the example.

Host Your .md Files in Another Repo

For many good reasons you may want to host your content in another GIT repo.

See the example.

Caveat

By default, next-markdown ignores README.md files and files whose name starts with an underscore (eg. _draft.md).

This can be overriden by implementing the include function in the config object.

Examples 🖥

Feel free to browse the examples to see next-markdown in action.

Contributing 🏗️

Thanks for your interest in next-markdown! You are very welcome to contribute. If you are proposing a new feature, please open an issue to make sure it is inline with the project goals.

1. Fork this repository to your own GitHub account and clone it to your local device

git clone https://github.com/your-name/next-markdown.git
cd next-markdown

2. Install the dependencies and run dev script

npm install
npm run dev

terminal 1

3. Open another terminal, pick an example in the examples/ folder, install dependencies and run dev

cd examples/blogging # or dynamic-routes, or remote-content
npm install
npm run dev

terminal 2

4. Start coding

  • edit files in src/, save: http://localhost:3000 gets updated automatically (aka hot-reloading)
  • add tests in src/__tests__/. Run tests with npm test command.

browser

5. Submitting a PR

Before you make your pull request, make sure to run:

  • npm test to make sure nothing is broken
  • npm run format to make sure the code looks consistent
  • npm run lint to make sure there is no problem in the code

Contributors 🙏

Comments
  • feat: allow building docs style pages

    feat: allow building docs style pages

    Purpose

    Allow developers to build such a page with next-markdown:

    image

    cf. https://chakra-ui.com/guides/integrations/with-storybook

    TODO

    List all files (done #5)

    List every files when rendering index.md. Props should look like:

    {
      ...
      html: string,
      nextmd: [...],
      files: [
        {
           ...
           html: string,
           nextmd: [..., ...],
        },
        ...
      ]
    }
    

    List previous / next files (won't do, not necessary)

    This can be actually achieved with files[n-1] and files[n+1].

    Table of Contents (done #8)

    List markdown content headings + add anchors

    Provide an example

    Please provide an example in examples/.

    Discussions

    [x] Maybe we should rename posts attribute. It is misleading since it's not asked to be a "blog post" (as we can see, it can also achieve documentation purpose) => done, posts has been renamed to files, cf. #5

    enhancement wip 
    opened by frouo 5
  • Setup the project so it's easy to develop and test this nextjs library

    Setup the project so it's easy to develop and test this nextjs library

    Please, help.

    Problem

    Right now I have no proper solution:

    • I tried to use npm link but, for every changes in next-markdown/src/index.ts, I need to execute cd examples/blogging && rm -rf node_modules/next-markdown && npm link. Very painful.
    • copy/paste next-markdown/src/index.ts and use it locally in an example, so when I am done, I copy/paste back to next-markdown/src. Very painful and ugly...
    • I cannot write tests. I tried exporting a next-markdown function (eg. export getNextmdFromFilePath = (..)=>{..}) but import { getNextmdFromFilePath } from '../'; in file __tests__/index.test.ts failed with error Module '"../index"' has no exported member 'getNextmdFromFilePath'

    Wanted

    I don't have the time (and knowledge yet) to setup properly the project this way:

    • ability to write tests...
    • live test: everytime next-markdown/src/index.ts changes, tests are ran
    • dev mode with lie reloading: having a project where everytime next-markdown/src/index.ts changes, the project updates
    • code splitting. For now if I create for example an utility file next-markdown/src/utility.ts the published npm library does not work (module not found)

    Thank you

    Any help is much than appreciated 🙏

    help wanted 
    opened by frouo 3
  • load page error

    load page error

    hello:1 GET http://localhost:3000/hello 500 (Internal Server Error) index.js?20a9:314 Uncaught at Object.next-markdown (file:///Users/kr.mao/Workspace/codesdancing/.next/server/pages/[...nextmd].js:32:1) at webpack_require (file:///Users/kr.mao/Workspace/codesdancing/.next/server/webpack-runtime.js:33:42) at eval (webpack-internal:///./src/pages/[...nextmd].jsx:8:71) at Function.webpack_require.a (file:///Users/kr.mao/Workspace/codesdancing/.next/server/webpack-runtime.js:106:13) at eval (webpack-internal:///./src/pages/[...nextmd].jsx:1:21) at Object../src/pages/[...nextmd].jsx (file:///Users/kr.mao/Workspace/codesdancing/.next/server/pages/[...nextmd].js:22:1) at webpack_require (file:///Users/kr.mao/Workspace/codesdancing/.next/server/webpack-runtime.js:33:42) at webpack_exec (file:///Users/kr.mao/Workspace/codesdancing/.next/server/pages/[...nextmd].js:52:39) at (file:///Users/kr.mao/Workspace/codesdancing/.next/server/pages/[...nextmd].js:53:28) at Object. (file:///Users/kr.mao/Workspace/codesdancing/.next/server/pages/[...nextmd].js:56:3) getNodeError @ nodeStackFrames.js?aca3:40 eval @ index.js?20a9:313 setTimeout(异步) _callee$ @ index.js?20a9:301 tryCatch @ runtime.js?96cf:63 invoke @ runtime.js?96cf:294 eval @ runtime.js?96cf:119 asyncGeneratorStep @ index.js?20a9:28 _next @ index.js?20a9:46 Promise.then(异步) asyncGeneratorStep @ index.js?20a9:37 _next @ index.js?20a9:46 eval @ index.js?20a9:51 eval @ index.js?20a9:43 _initNext @ index.js?20a9:363 initNext @ index.js?20a9:366 eval @ next-dev.js?3346:38 ./node_modules/next/dist/client/next-dev.js @ main.js?ts=1646807645629:600 options.factory @ webpack.js?ts=1646807645629:685 webpack_require @ webpack.js?ts=1646807645629:37 webpack_exec @ main.js?ts=1646807645629:1399 (匿名) @ main.js?ts=1646807645629:1400 webpackJsonpCallback @ webpack.js?ts=1646807645629:1268 (匿名) @ main.js?ts=1646807645629:9

    opened by krmao 2
  • feat: optional catch all routes

    feat: optional catch all routes

    Context

    At the time of writing, next-markdown support nextjs "catch all routes", meaning you need to add a [...nextmd].js in your pages and create an index.js for the home page.

    Purpose

    We want to create a website (including the home page!) with markdown. To allow creating home page we need to use "optional catch all routes".

    Todo

    next-markdown should work with [[...nextmd]].js

    opened by frouo 1
  • Better faster strong project setup

    Better faster strong project setup

    Closes #6

    Work in Progress:

    • [x] lib is bundled using parcel
    • [x] lib type: module
    • [x] examples uses the local lib
    • [x] examples hot-reload when lib code base is edited
    • [x] npm install [email protected] works
    • [x] unable to run tests yet :(
    help wanted wip 
    opened by frouo 1
  • [FIX] - Library ESModule build

    [FIX] - Library ESModule build

    Typescript supports .js imports for .ts files. see why here : https://github.com/microsoft/TypeScript/issues/16577#issuecomment-754941937

    Either that or :

    • use a bundler (IMO overkill for the size of next-markdown)
    • use a post-processor typescript plugin to append .js file extensions to relative imports after compilation
    opened by edvincandon 1
  • feat: enhance blog-aware rules to handle more use cases (blog, docs...)

    feat: enhance blog-aware rules to handle more use cases (blog, docs...)

    Purpose

    Allow to customize the YYYY-MM-DD rule for given folder.

    Thus allowing specified folder to have custom rule to sort content:

    pages-mardown/
    ├ docs/
       ├ feat-A-doc1.md
       ├ feat-A-doc2.md
       ├ feat-B-doc1.md
       ├ feat-B-doc2.md
       ├ feat-B-doc3.md
    ``
    
    ## how to?
    
    Please take example to the function `include` in the NextMarkdownConfig type, where `include` has a default behavior that can be overridden.
    enhancement 
    opened by frouo 1
  • Allow building documentation

    Allow building documentation

    Closes #4 Closes #15 (keep next-markdown simple, getStaticPropsForNextmd can do the job)

    This might be the most simple way to test this PR in your project, run npm install "https://github.com/frouo/next-markdown.git#feature/documentation"

    opened by frouo 0
  • feat (advanced): allow to flatten some directories for docs / blog classification purpose

    feat (advanced): allow to flatten some directories for docs / blog classification purpose

    Purpose

    In some circumstances, you likely want to use subfolders to classify your markdown files. For example when writing docs.

    So you want a way to bypass the default next-markdown dynamic routes.

    Considered solution

    ✋ default dynamic routes MUST remain because it covers 99% of use cases. We all love how nextjs works with /pages. . Eg: path/to/projects.md will match /path/to/project.

    We could have a .nextmd.config.json file at the root of a given folder to bypass how next-markdown will consider this folder's subfolders.

    # .nextmd.config.json
    
    {
      "flatten": ["examples", "features"]
    }
    

    image

    Discussion

    Tell me what you think in comment. Thanks

    enhancement help wanted question 
    opened by frouo 0
  • [FIX] - Build output

    [FIX] - Build output

    tsc build output to es2020 modules does not append .js extensions to dist files. Either use a builder to alleviate this or force single file output.

    node ESModule resolution requires .js extensions in imports !

    opened by edvincandon 0
  • feat (advanced): split next-markdown into smaller modules

    feat (advanced): split next-markdown into smaller modules

    Split the lib in several parts

    • npm install @nextmarkdown/core : contains everything but the markdownToHtml logic (commonjs lib)
    • npm install @nextmarkdown/markdown-to-html : will contain all the logic to convert markdown to html with remark / rehype plugins dependencies (esm module lib because rehype-stringify is ESM only).
    • npm install @nextmarkdown/mdx : will contain the next-mdx-remote logic.

    Finally npm install next-markdown would install, by default, both @nextmarkdown/core and @nextmarkdown/markdown-to-html so it is really easy to get started with next-mardown + covers 80% of the usage cases.

    enhancement help wanted 
    opened by frouo 1
Owner
François Rouault
iOS expert, Nextjs + Tailwindcss lover, ex-Android, pixel perfect
François Rouault
This package enables you to define your routes using the flat-routes convention.

Remix Flat Routes This package enables you to define your routes using the flat-routes convention. This is based on the gist by Ryan Florence ?? Insta

Kiliman 180 Jan 3, 2023
Download Notion pages as markdown and image files, preserving hierarchy and enabling workflow properties. Works with Docusaurus.

notion-pull notion-pull lets you use Notion as your editor for markdown-based static site generators like Docusaurus. Using Notion instead of raw mark

SIL LSDev 46 Jan 7, 2023
Markdown Transformer. Transform markdown files to different formats

Mdtx Inspired by generative programming and weed :). So I was learning Elm language at home usually in the evening and now I am missing all this gener

Aexol 13 Jan 2, 2023
Generate translated routes for your qwik project.

qwik-translate-routes Generate translated routes for your qwik project. Installation // npm npm install -D qwik-translate-routes // yarn yarn add -D q

Alexandre Fernandez 5 Dec 28, 2022
ToolJet an open-source low-code framework to build and deploy internal tools quickly without much effort from the engineering teams

ToolJet is an open-source low-code framework to build and deploy internal tools quickly without much effort from the engineering teams. You can connect to your data sources, such as databases (like PostgreSQL, MongoDB, Elasticsearch, etc), API endpoints (ToolJet supports importing OpenAPI spec & OAuth2 authorization), and external services (like Stripe, Slack, Google Sheets, Airtable) and use our pre-built UI widgets to build internal tools.

ToolJet 15.6k Jan 3, 2023
Low cost, low effort P2P WebRTC serverless signalling using Cloudflare Workers

P2PCF P2PCF enables free (or cheap) serverless WebRTC signalling using a Cloudflare worker and a Cloudflare R2 bucket. The API is inspired by P2PT, bu

Greg Fodor 560 Jan 8, 2023
The Main Purpose The main purpose of creating an anaonline information system, as an effort responsive to the management of the data of the Members of the Persis Youth based on information technology systems

landing-page-pp landing-page-pp.vercel.app #The Main Purpose The main purpose of creating an anaonline information system, as an effort responsive to

Hilman Firdaus 6 Oct 21, 2022
Dynamic-web-development - Dynamic web development used CSS and HTML

Dynamic-web-development ASSISNMENT I just used CSS and HTML to make a mobile int

null 1 Feb 8, 2022
dynamic-component-app is an angular application for dynamic component template creation

MyApp This project was generated with Angular CLI version 14.1.0. Development server Run ng serve for a dev server. Navigate to http://localhost:4200/

Aniket Muruskar 7 Aug 26, 2022
🌸 A cli can automatically generate files from Excel files.

unxlsx A cli can automatically generate files from Excel files. Why We often need to export some information from XLSX to generate our files, such as

Frozen FIsh 24 Aug 22, 2022
A Cloudflare Workers service that fetches and renders Notion pages as HTML, Markdown, or JSON.

notion-fetch A Cloudflare Workers service that fetches and renders Notion pages as HTML, Markdown, or JSON. Powered by Durable Objects and R2. Usage P

Heyang Zhou 7 Jan 6, 2023
A plugin for the Obsidian markdown note application, adding functionality to render markdown documents with multiple columns of text.

Multi-Column Markdown Take your boring markdown document and add some columns to it! With Multi Column Markdown rather than limiting your document lay

Cameron Robinson 91 Jan 2, 2023
A markdown-it plugin that process images through the eleventy-img plugin. Can be used in any projects that uses markdown-it.

markdown-it-eleventy-img A markdown-it plugin that process images through the eleventy-img plugin. Can be used in any projects that use markdown-it. F

null 25 Dec 20, 2022
An obsidian plugin for uploading local images embedded in markdown to remote store and export markdown for publishing to static site.

Obsidian Publish This plugin cloud upload all local images embedded in markdown to specified remote image store (support imgur only, currently) and ex

Addo.Zhang 7 Dec 13, 2022
Dynamic form elements generate with jQuery

Demo Advance Form Demo. Basic Form Demo. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https:/

Rajneesh Gautam 5 Dec 13, 2022
Save time by managing bills & expenses, invoicing & easy reconciliation all in one app. Generate clear dynamic statements and get your reports, the way you like them

expense-manager-app (Opensource Expense Tracking App built with React) ?? Save time by managing bills & expenses, invoicing & easy reconciliation all

Muhammad Awais 3 Oct 12, 2022
Create beautiful and simple HTML pages from your Readme.md files

?? Oranda Create beautiful and simple HTML pages from your Readme.md files ?? No config ??‍?? Code Highlighting ?? Emoji Support ✨ Creates Static file

axo 16 Oct 13, 2022
Render (GitHub Flavoured with syntax highlighting) Markdown, and generate CSS for each of GitHub’s themes.

render-gfm Render (GitHub Flavoured with syntax highlighting) Markdown, and generate CSS for each of GitHub’s themes. GitHub Repository npm Package Do

Shaun Bharat 12 Oct 10, 2022
A plugin for generate markdown table quickly like Typora.

Obsidian Table Generator A plugin for generate markdown table quickly like Typora. Features You can use obsidian-table-generator to generate markdown

Boninall 58 Dec 30, 2022