项目作者: MoOx

项目描述 :
Transform markdown content as a JSON for easy rendering for front-end with React, React Native & similar
高级语言: JavaScript
项目地址: git://github.com/MoOx/markdown-to-json.git
创建时间: 2019-06-07T20:27:48Z
项目社区:https://github.com/MoOx/markdown-to-json

开源协议:MIT License

下载


@moox/markdown-to-json

Build Status
@moox/markdown-to-json"">Version

Transform markdown content as a JSON

This package is a minimal markdown preprocessor to make it easy to render
markdown in a JS environement like React, React Native etc.

It is meant to be used before runtime:

  1. You transform your markdown files as JSON
  2. You consume the JSON files from the JS without any runtime transformation
    required

Installation

  1. npm install @moox/markdown-to-json

or

  1. yarn add @moox/markdown-to-json

Usage

CLI

  1. npx markdown-to-json "docs/**/*.md" [optional output-folder]

or

  1. yarn markdown-to-json "docs/**/*.md" [optional output-folder]

⚠️ Be sure to put globs between quotes.

Node.js

  1. const mdjs = require("@moox/markdown-to-json");
  2. const output = mdjs.markdownAsJsTree("# markdown string");

By default, it handles:

  • front-matter (via gray-matter)
  • auto slug for headings (with anchors)
  • code highlight (via highlight.js)
  • table of contents (via remark-toc)

The idea is to get a markdown like this

  1. ---
  2. test: a
  3. test2: b
  4. ---
  5. ## Test
  6. [link](href)
  7. ```js
  8. console.log(window);
  9. ```

like

  1. {
  2. "test": "a",
  3. "test2": "b",
  4. "headings": [
  5. {
  6. "id": "test",
  7. "level": 2,
  8. "text": "Test"
  9. }
  10. ],
  11. "body": {
  12. "tag": "div",
  13. "children": [
  14. {
  15. "tag": "h2",
  16. "props": {
  17. "id": "test"
  18. },
  19. "children": [
  20. //...
  21. ]
  22. }
  23. ]
  24. }
  25. }

Options

In addition to the markdown string, 2 arguments are accepted that are functions
that should returns an array of plugin with there options:

The first example is equivalent to

  1. const mdjs = require("@moox/markdown-to-json");
  2. const output = mdjs.markdownAsJsTree(
  3. "# markdown string",
  4. mdjs.defaultRemarkPlugins
  5. mdjs.defaultRehypePlugins
  6. );

By default sending arguments will override default plugins. You
can get the default one by doing something like this

  1. const mdjs = require("@moox/markdown-to-json");
  2. const output = mdjs.markdownAsJsTree(
  3. "# markdown string",
  4. () => ([
  5. [require("remark-whatever"), {optionForWhatever: true}],
  6. ...mdjs.defaultRemarkPlugins()
  7. ]),
  8. () => ([
  9. [require("rehype-hispterpackage"), {/* no options */}}],
  10. [require("rehype-anotherhispterpackage"), {powerUserOption: "super argument"}}],
  11. ...mdjs.defaultRehypePlugins()
  12. ]);
  13. );

Thanks unified to make this possible!

Check out input &
output to get an idea of what to expect
from this package.


LICENSE