Git Product home page Git Product logo

next-markdown's Introduction


next-markdown

Markdown Pages for Next.js

Dynamic Routes • Blog Aware • Design Your Layout

npm (tag) Downloads GitHub stars

Twitter Follow

Made for people

  • having a nextjs project
  • in ❤️ with markdown
  • who want to generate boring (but very necessary!) pages like /about, /terms, /blog, /docs or /whatever/other/route from markdown files with 0 effort

Used by

  • lembot.com - all pages except the home page are generated from markdown hosted in a separate public github repo.
  • snappify.io (blog, docs) - a powerful design tool to create and manage beautiful images of your code.
  • frouo.com - a dev blog
  • reach us on twitter @nextmarkdown to add your website here (or personal: @frouo)

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, subPaths }) {
  return <div dangerouslySetInnerHTML={{ __html: html }} /> 👈 design your own layout 🧑‍🎨
}

Usage 👋

At the root of your project create the 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

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

Just like nextjs does with pages/.

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` is already defined 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
  ├ hello.md   ......... ➡️ /blog/hello
  ├ world.md   ......... ➡️ /blog/world
├ docs/
  ├ index.md   ......... ➡️ /docs
  ├ get-started.md   ... ➡️ /docs/get-started
  ├ features.md   ...... ➡️ /docs/features
  ├ contribute.md   .... ➡️ /docs/contribute

See the example.

Blog Aware (example)

next-markdown is blog-aware:

  • list all the posts
  • write draft or unpublish a post by simply prefixing the file name with an underscore (eg. _hello.md will redirect to 404)
  • reading time
  • etc.

Documentation (example)

next-markdown lets you build a documentation:

  • sidebar
  • previous / next
  • organize your docs by folders
  • etc.

Table of Contents (example)

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

Personal Blog (example)

Use next-markdown to browse and parse your markdown files so you can build your personal blog in seconds.

MDX Support (example)

There is nothing to setup on your side, MDX support comes for free.

You can mix .md and .mdx files.

Configure custom remark and rehype plugins (example)

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: [],
});

Host Your .md Files in Another Repo (example)

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

Examples 🖥

More examples here ↗️.

Feel free to browse them 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/blog # 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 🙏

next-markdown's People

Contributors

dsumer avatar edvincandon avatar frouo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

next-markdown's Issues

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

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

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.

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 🙏

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

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

feat: support mdx

Support MDX files

For example, support such tree:

pages-mardown/
├ about.mdx
├ this/is/an/example.mdx

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.

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.