UNPKG

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