UNPKG

16 kBMarkdownView Raw
1# Markdown Magic [![npm-version][npm-badge]][npm-link]
2
3✨ Add a little magic to your markdown ✨
4
5## About
6
7<img align="right" width="200" height="183" src="https://cloud.githubusercontent.com/assets/532272/21507867/3376e9fe-cc4a-11e6-9350-7ec4f680da36.gif">Markdown magic uses comment blocks in markdown files to automatically sync or transform its contents.
8
9- Automatically keep markdown files up to date from local or remote code sources
10- Transform markdown content with custom transform functions
11- Render markdown with any template engine
12- Automatically generate a table of contents
13- ... etc
14
15The comments markdown magic uses are hidden in markdown and when viewed as HTML.
16
17This `README.md` is generated with `markdown-magic` [view the raw file](https://raw.githubusercontent.com/DavidWells/markdown-magic/master/README.md) to see how.
18
19[Video demo](http://www.youtube.com/watch?v=4V2utrvxwJ8) • [Example Repo](https://github.com/DavidWells/repo-using-markdown-magic)
20
21## Table of Contents
22<!-- ⛔️ MD-MAGIC-EXAMPLE:START (TOC:collapse=true&collapseText=Click to expand) -->
23<details>
24<summary>Click to expand</summary>
25
26- [About](#about)
27- [Install](#install)
28- [Usage](#usage)
29 - [API](#api)
30 - [Configuration Options](#configuration-options)
31- [CLI Usage](#cli-usage)
32- [Transforms](#transforms)
33 - [> TOC](#-toc)
34 - [> CODE](#-code)
35 - [> FILE](#-file)
36 - [> REMOTE](#-remote)
37- [🔌 Markdown magic plugins](#-markdown-magic-plugins)
38- [Adding Custom Transforms](#adding-custom-transforms)
39- [Plugin Example](#plugin-example)
40- [Other usage examples](#other-usage-examples)
41- [Custom Transform Demo](#custom-transform-demo)
42- [Prior Art](#prior-art)
43- [License](#license)
44- [Usage examples](#usage-examples)
45
46</details>
47<!-- ⛔️ MD-MAGIC-EXAMPLE:END -->
48
49## Install
50
51```bash
52npm install markdown-magic --save-dev
53```
54
55## Usage
56<!-- ⛔️ MD-MAGIC-EXAMPLE:START (CODE:src=./examples/basic-usage.js) -->
57<!-- The below code snippet is automatically added from ./examples/basic-usage.js -->
58```js
59import path from 'path'
60import markdownMagic from 'markdown-magic'
61
62const markdownPath = path.join(__dirname, 'README.md')
63markdownMagic(markdownPath)
64```
65<!-- ⛔️ MD-MAGIC-EXAMPLE:END *-->
66
67
68<!-- ⛔️ MD-MAGIC-EXAMPLE:START (RENDERDOCS:path=./index.js)
69- Do not remove or modify this section -->
70### API
71```js
72markdownMagic(filePath, config, callback)
73```
74- `filePaths` - *String or Array* - Path or glob pattern. Uses [globby patterns](https://github.com/sindresorhus/multimatch/blob/master/test.js)
75- `config` - See configuration options below
76- `callback` - callback to run after markdown updates
77<!-- ⛔️ MD-MAGIC-EXAMPLE:END - Do not remove or modify this section -->
78
79<!-- ⛔️ MD-MAGIC-EXAMPLE:START (RENDERDOCS:path=./lib/processFile.js)
80- Do not remove or modify this section -->
81### Configuration Options
82
83- `transforms` - *object* - (optional) Custom commands to transform block contents, see transforms & custom transforms sections below.
84
85- `outputDir` - *string* - (optional) Change output path of new content. Default behavior is replacing the original file
86
87- `matchWord` - *string* - (optional) Comment pattern to look for & replace inner contents. Default `AUTO-GENERATED-CONTENT`
88
89- `DEBUG` - *Boolean* - (optional) set debug flag to `true` to inspect the process
90<!-- ⛔️ MD-MAGIC-EXAMPLE:END - Do not remove or modify this section -->
91
92## CLI Usage
93
94You can use `markdown-magic` as a CLI command. Run `markdown --help` to see all available CLI options
95
96```bash
97markdown --help
98# or
99md-magic
100```
101
102This is useful for adding the package quickly to your `package.json` npm scripts
103
104CLI usage example with options
105
106```bash
107md-magic --path '**/*.md' --config ./config.file.js
108```
109
110In NPM scripts, `npm run docs` would run the markdown magic and parse all the `.md` files in the directory.
111
112```json
113"scripts": {
114 "docs": "md-magic --path '**/*.md' --ignore 'node_modules'"
115},
116```
117
118If you have a `markdown.config.js` file where `markdown-magic` is invoked, it will automatically use that as the configuration unless otherwise specified by `--config` flag.
119
120<!-- ⛔️ MD-MAGIC-EXAMPLE:START (CODE:src=./markdown.config.js) -->
121<!-- The below code snippet is automatically added from ./markdown.config.js -->
122```js
123/* CLI markdown.config.js file example */
124module.exports = {
125 matchWord: 'MD-MAGIC-EXAMPLE',
126 transforms: {
127 /* Match <!-- AUTO-GENERATED-CONTENT:START (LOLZ) --> */
128 LOLZ(content, options) {
129 return `This section was generated by the cli config markdown.config.js file`
130 }
131 },
132 callback: function () {
133 console.log('markdown processing done')
134 }
135}
136```
137<!-- ⛔️ MD-MAGIC-EXAMPLE:END *-->
138
139## Transforms
140
141Markdown Magic comes with a couple of built in transforms for you to use or you can extend it with your own transforms. See 'Custom Transforms' below.
142
143<!-- ⛔️ MD-MAGIC-EXAMPLE:START (RENDERDOCS:path=./lib/transforms/index.js) - Do not remove or modify this section -->
144### > TOC
145
146Generate table of contents from markdown file
147
148**Options:**
149- `firsth1` - *boolean* - (optional): Show first h1 of doc in table of contents. Default `false`
150- `collapse` - *boolean* - (optional): Collapse the table of contents in a detail accordian. Default `false`
151- `collapseText` - *string* - (optional): Text the toc accordian summary
152- `excludeText` - *string* - (optional): Text to exclude in the table of contents. Default `Table of Contents`
153- `maxDepth` - *number* - (optional): Max depth of headings. Default 4
154
155**Example:**
156```md
157<!-- AUTO-GENERATED-CONTENT:START (TOC) -->
158toc will be generated here
159<!-- AUTO-GENERATED-CONTENT:END -->
160```
161
162Default `MATCHWORD` is `AUTO-GENERATED-CONTENT`
163
164---
165
166### > CODE
167
168Get code from file or URL and put in markdown
169
170**Options:**
171- `src`: The relative path to the code to pull in, or the `URL` where the raw code lives
172- `syntax` (optional): Syntax will be inferred by fileType if not specified
173- `header` (optional): Will add header comment to code snippet. Useful for pointing to relative source directory or adding live doc links
174- `lines` (optional): a range with lines of code which will then be replaced with code from the file. The line range should be defined as: "lines=*startLine*-*EndLine*" (for example: "lines=22-44"). Please see the example below
175
176**Example:**
177```md
178<!-- AUTO-GENERATED-CONTENT:START (CODE:src=./relative/path/to/code.js) -->
179This content will be dynamically replaced with code from the file
180<!-- AUTO-GENERATED-CONTENT:END -->
181```
182
183```md
184 <!-- AUTO-GENERATED-CONTENT:START (CODE:src=./relative/path/to/code.js&lines=22-44) -->
185 This content will be dynamically replaced with code from the file lines 22 through 44
186 <!-- AUTO-GENERATED-CONTENT:END -->
187 ```
188
189Default `MATCHWORD` is `AUTO-GENERATED-CONTENT`
190
191---
192
193### > FILE
194
195Get local file contents.
196
197**Options:**
198- `src`: The relative path to the file to pull in
199
200**Example:**
201```md
202<!-- AUTO-GENERATED-CONTENT:START (FILE:src=./path/to/file) -->
203This content will be dynamically replaced from the local file
204<!-- AUTO-GENERATED-CONTENT:END -->
205```
206
207Default `MATCHWORD` is `AUTO-GENERATED-CONTENT`
208
209---
210
211### > REMOTE
212
213Get any remote Data and put in markdown
214
215**Options:**
216- `url`: The URL of the remote content to pull in
217
218**Example:**
219```md
220<!-- AUTO-GENERATED-CONTENT:START (REMOTE:url=http://url-to-raw-md-file.md) -->
221This content will be dynamically replaced from the remote url
222<!-- AUTO-GENERATED-CONTENT:END -->
223```
224
225Default `MATCHWORD` is `AUTO-GENERATED-CONTENT`
226
227---
228<!-- ⛔️ MD-MAGIC-EXAMPLE:END - Do not remove or modify this section -->
229
230## 🔌 Markdown magic plugins
231
232* [wordcount](https://github.com/DavidWells/markdown-magic-wordcount/) - Add wordcount to markdown files
233* [github-contributors](https://github.com/DavidWells/markdown-magic-github-contributors) - List out the contributors of a given repository
234* [directory-tree](https://github.com/camacho/markdown-magic-directory-tree) - Add directory tree to markdown files
235* [install-command](https://github.com/camacho/markdown-magic-install-command) - Add install command to markdown files with `peerDependencies` included
236* [subpackage-list](https://github.com/camacho/markdown-magic-subpackage-list) - Add list of all subpackages (great for projects that use [Lerna](https://github.com/lerna/lerna))
237* [version-badge](https://github.com/camacho/markdown-magic-version-badge) - Add a badge with the latest version of the project
238* [template](https://github.com/camacho/markdown-magic-template) - Add Lodash template support
239* [dependency-table](https://github.com/camacho/markdown-magic-dependency-table) - Add a table of dependencies with links to their repositories, version information, and a short description
240* [package-scripts](https://github.com/camacho/markdown-magic-package-scripts) - Add a table of `package.json` scripts with descriptions
241* [prettier](https://github.com/camacho/markdown-magic-prettier) - Format code blocks with [`prettier`](https://github.com/prettier/prettier)
242* [engines](https://github.com/camacho/markdown-magic-engines) - Print engines list from `package.json`
243* [jsdoc](https://github.com/bradtaylorsf/markdown-magic-jsdoc) - Adds jsdoc comment support
244* [build-badge](https://github.com/rishichawda/markdown-magic-build-badge) - Update branch badges to auto-magically point to current branches.
245* [package-json](https://github.com/forresst/markdown-magic-package-json) - Add the package.json properties to markdown files
246* [local-image](https://github.com/stevenbenisek/markdown-magic-local-image)
247* [figlet](https://github.com/lafourchette/markdown-magic-figlet) - Add FIGfont text to markdown files
248* [local-image](https://github.com/stevenbenisek/markdown-magic-local-image) - plugin to add local images to markdown
249
250## Adding Custom Transforms
251
252Markdown Magic is extendable via plugins.
253
254Plugins allow developers to add new transforms to the `config.transforms` object. This allows for things like using different rendering engines, custom formatting, or any other logic you might want.
255
256Plugins run in order of registration.
257
258The below code is used to generate **this markdown file** via the plugin system.
259
260<!-- ⛔️ MD-MAGIC-EXAMPLE:START (CODE:src=./examples/generate-readme.js) -->
261<!-- The below code snippet is automatically added from ./examples/generate-readme.js -->
262```js
263const fs = require('fs')
264const path = require('path')
265const markdownMagic = require('../index')
266// const markdownMagic = require('markdown-magic')
267
268const config = {
269 matchWord: 'MD-MAGIC-EXAMPLE', // default matchWord is AUTO-GENERATED-CONTENT
270 transforms: {
271 /* Match <!-- AUTO-GENERATED-CONTENT:START (customTransform:optionOne=hi&optionOne=DUDE) --> */
272 customTransform(content, options) {
273 console.log('original content in comment block', content)
274 console.log('options defined on transform', options)
275 // options = { optionOne: hi, optionOne: DUDE}
276 return `This will replace all the contents of inside the comment ${options.optionOne}`
277 },
278 /* Match <!-- AUTO-GENERATED-CONTENT:START (RENDERDOCS:path=../file.js) --> */
279 RENDERDOCS(content, options) {
280 const fileContents = fs.readFileSync(options.path, 'utf8')
281 const docBlocs = require('doxxx').parseComments(fileContents, { raw: true, skipSingleStar: true })
282 let updatedContent = ''
283 docBlocs.forEach((data) => {
284 updatedContent += `${data.description.full}\n\n`
285 })
286 return updatedContent.replace(/^\s+|\s+$/g, '')
287 },
288 /* Match <!-- AUTO-GENERATED-CONTENT:START (pluginExample) --> */
289 pluginExample: require('./plugin-example')({ addNewLine: true }),
290 /* Include plugins from NPM */
291 // count: require('markdown-magic-wordcount'),
292 // github: require('markdown-magic-github-contributors')
293 }
294}
295
296const markdownPath = path.join(__dirname, '..', 'README.md')
297markdownMagic(markdownPath, config, () => {
298 console.log('Docs ready')
299})
300```
301<!-- ⛔️ MD-MAGIC-EXAMPLE:END -->
302
303## Plugin Example
304
305Plugins must return a transform function with the following signature.
306
307```js
308return function myCustomTransform (content, options)
309```
310
311<!-- ⛔️ MD-MAGIC-EXAMPLE:START (CODE:src=./examples/plugin-example.js) -->
312<!-- The below code snippet is automatically added from ./examples/plugin-example.js -->
313```js
314/* Custom Transform Plugin example */
315const merge = require('deepmerge')
316module.exports = function customPlugin(pluginOptions) {
317 // set plugin defaults
318 const defaultOptions = {
319 addNewLine: false
320 }
321 const userOptions = pluginOptions || {}
322 const pluginConfig = merge(defaultOptions, userOptions)
323 // return the transform function
324 return function myCustomTransform (content, options) {
325 const newLine = (pluginConfig.addNewLine) ? '\n' : ''
326 const updatedContent = content + newLine
327 return updatedContent
328 }
329}
330```
331<!-- ⛔️ MD-MAGIC-EXAMPLE:END -->
332
333[View the raw file](https://raw.githubusercontent.com/DavidWells/markdown-magic/master/README.md) file and run `npm run docs` to see this plugin run
334<!-- ⛔️ MD-MAGIC-EXAMPLE:START (pluginExample) DO not edit ⛔️ -->
335This content is altered by the `pluginExample` plugin registered in `examples/generate-readme.js`
336
337<!-- ⛔️ MD-MAGIC-EXAMPLE:END -->
338
339## Other usage examples
340
341- [With Github Actions](https://github.com/dineshsonachalam/repo-using-markdown-autodocs)
342- [Serverless Plugin Repo](https://github.com/serverless/plugins/blob/master/generate-docs.js) this example takes a `json` file and converts it into a github flavored markdown table
343- [MochaJS](https://github.com/mochajs/mocha/blob/4cc711fa00f7166a2303b77bf2487d1c2cc94621/scripts/markdown-magic.config.js)
344- [tc39/agendas](https://github.com/tc39/agendas#agendas) - [code](https://github.com/tc39/agendas/blob/65945b1b6658e9829ef95a51bf2632ff44f951e6/scripts/generate.js)
345- [moleculerjs/moleculer-addons](https://github.com/moleculerjs/moleculer-addons/blob/7cf0f72140717c52621b724cd54a710517106df0/readme-generator.js)
346- [good-first-issue](https://github.com/bnb/good-first-issue/blob/e65513a1f26167dea3c137008b8796640d8d5303/markdown.config.js)
347- [navikt/nav-frontend-moduler](https://github.com/navikt/nav-frontend-moduler/blob/20ad521c27a43d3203eab4bc32121e5b8270c077/_scripts/generateReadmes.js)
348- [country-flags-svg](https://github.com/ronatskiy/country-flags-svg/blob/cfb2368c7e634ebc1679855e13cc3e26ca11187f/markdown.config.js)
349- [react-typesetting](https://github.com/exogen/react-typesetting/blob/7114cdc8c4cb1b0d59ebc8b5364e808687419889/markdown.config.js)
350- [and many more!](https://github.com/search?o=desc&p=1&q=markdown-magic+filename%3Apackage.json+-user%3Ah13i32maru+-user%3Aesdoc+-user%3Aes-doc&s=indexed&type=Code)
351
352## Custom Transform Demo
353
354View the raw source of this `README.md` file to see the comment block and see how the `customTransform` function in `examples/generate-readme.js` works
355
356<!-- ⛔️ MD-MAGIC-EXAMPLE:START (customTransform:optionOne=hi&optionOne=DUDE) - Do not remove or modify this section -->
357This will replace all the contents of inside the comment DUDE
358<!-- ⛔️ MD-MAGIC-EXAMPLE:END - Do not remove or modify this section -->
359
360## Prior Art
361
362This was inspired by [Kent C Dodds](https://twitter.com/kentcdodds) and [jfmengels](https://github.com/jfmengels)'s [all contributors cli](https://github.com/jfmengels/all-contributors-cli) project.
363
364<!-- MD-MAGIC-EXAMPLE:START (LOLZ)-->
365This section was generated by the cli config markdown.config.js file
366<!-- MD-MAGIC-EXAMPLE:END (LOLZ)-->
367
368## License
369
370[MIT][mit] © [DavidWells][author]
371
372[npm-badge]:https://img.shields.io/npm/v/markdown-magic.svg?style=flat-square
373[npm-link]: http://www.npmjs.com/package/markdown-magic
374[mit]: http://opensource.org/licenses/MIT
375[author]: http://github.com/davidwells
376
377## Usage examples
378
379- [Project using markdown-magic](https://github.com/search?o=desc&q=filename%3Apackage.json+%22markdown-magic%22&s=indexed&type=Code)
380- [Examples in md](https://github.com/search?l=Markdown&o=desc&q=AUTO-GENERATED-CONTENT&s=indexed&type=Code)