UNPKG

12.6 kBMarkdownView Raw
1# Markdown Magic
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 it's contents. These code comments are hidden in markdown and when viewed as HTML.
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
15This `README.md` is generated with `markdown-magic` [view the raw file](https://raw.githubusercontent.com/DavidWells/markdown-magic/master/README.md) to see how.
16
17[Video demo](http://www.youtube.com/watch?v=4V2utrvxwJ8) • [Example Repo](https://github.com/DavidWells/repo-using-markdown-magic)
18
19## Table of Contents
20<!-- ⛔️ MD-MAGIC-EXAMPLE:START (TOC:collapse=true&collapseText=Click to expand) -->
21<details>
22<summary>Click to expand</summary>
23
24- [About](#about)
25- [Install](#install)
26- [Usage](#usage)
27- [CLI Usage](#cli-usage)
28 * [API](#api)
29 * [Configuration Options](#configuration-options)
30- [Transforms](#transforms)
31 * [🔀 CODE](#%F0%9F%94%80-code)
32 * [🔀 REMOTE](#%F0%9F%94%80-remote)
33 * [🔀 TOC](#%F0%9F%94%80-toc)
34- [🔌 Third Party Transforms a.k.a. Plugins](#%F0%9F%94%8C-third-party-transforms-aka-plugins)
35- [Adding Custom Transforms](#adding-custom-transforms)
36- [Plugin Example](#plugin-example)
37- [Other usage examples](#other-usage-examples)
38- [Custom Transform Demo](#custom-transform-demo)
39- [Prior Art](#prior-art)
40
41</details>
42<!-- ⛔️ MD-MAGIC-EXAMPLE:END -->
43
44## Install
45
46```bash
47npm install markdown-magic --save-dev
48```
49
50## Usage
51<!-- ⛔️ MD-MAGIC-EXAMPLE:START (CODE:src=./examples/basic-usage.js) -->
52<!-- The below code snippet is automatically added from ./examples/basic-usage.js -->
53```js
54import path from 'path'
55import markdownMagic from 'markdown-magic'
56
57const markdownPath = path.join(__dirname, 'README.md')
58markdownMagic(markdownPath)
59```
60<!-- ⛔️ MD-MAGIC-EXAMPLE:END *-->
61
62## CLI Usage
63
64You can use `markdown-magic` as a CLI command. Run `markdown --help` to see all available CLI options
65
66```bash
67markdown --help
68# or
69md-magic
70```
71
72This is useful for adding the package quickly to your `package.json` npm scripts
73
74CLI usage example with options
75
76```bash
77md-magic --path '**/*.md' --config ./config.file.js
78```
79
80In NPM scripts, `npm run docs` would run the markdown magic and parse all the `.md` files in the directory.
81
82```json
83"scripts": {
84 "docs": "md-magic --path '**/*.md' --ignore 'node_modules'"
85},
86```
87
88If 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.
89
90<!-- ⛔️ MD-MAGIC-EXAMPLE:START (CODE:src=./markdown.config.js) -->
91<!-- The below code snippet is automatically added from ./markdown.config.js -->
92```js
93/* CLI config file example */
94module.exports = {
95 transforms: {
96 /* Match <!-- AUTO-GENERATED-CONTENT:START (LOLZ) --> */
97 LOLZ(content, options) {
98 return `This section was generated by the cli config markdown.config.js file`
99 }
100 },
101 callback: function () {
102 console.log('done')
103 }
104}
105```
106<!-- ⛔️ MD-MAGIC-EXAMPLE:END *-->
107
108<!-- ⛔️ MD-MAGIC-EXAMPLE:START (RENDERDOCS:path=./index.js)
109- Do not remove or modify this section -->
110### API
111```js
112markdownMagic(filePath, config, callback)
113```
114- `filePaths` - *String or Array* - Path or glob pattern. Uses [globby patterns](https://github.com/sindresorhus/multimatch/blob/master/test.js)
115- `config` - See configuration options below
116- `callback` - callback to run after markdown updates
117<!-- ⛔️ MD-MAGIC-EXAMPLE:END - Do not remove or modify this section -->
118
119<!-- ⛔️ MD-MAGIC-EXAMPLE:START (RENDERDOCS:path=./lib/processFile.js)
120- Do not remove or modify this section -->
121### Configuration Options
122
123- `transforms` - *object* - (optional) Custom commands to transform block contents, see transforms & custom transforms sections below.
124
125- `outputDir` - *string* - (optional) Change output path of new content. Default behavior is replacing the original file
126
127- `matchWord` - *string* - (optional) Comment pattern to look for & replace inner contents. Default `AUTO-GENERATED-CONTENT`
128
129- `DEBUG` - *Boolean* - (optional) set debug flag to `true` to inspect the process
130<!-- ⛔️ MD-MAGIC-EXAMPLE:END - Do not remove or modify this section -->
131
132
133## Transforms
134
135Markdown 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.
136
137<!-- ⛔️ MD-MAGIC-EXAMPLE:START (RENDERDOCS:path=./lib/transforms/index.js) - Do not remove or modify this section -->
138### 🔀 CODE
139
140Get code from file or URL and put in markdown
141
142**Options:**
143- `src`: The relative path to the code to pull in, or the `URL` where the raw code lives
144- `syntax` (optional): Syntax will be inferred by fileType if not specified
145
146**Example:**
147```md
148<!-- AUTO-GENERATED-CONTENT:START (CODE:src=./relative/path/to/code.js) -->
149This content will be dynamically replaced with code from the file
150<!-- AUTO-GENERATED-CONTENT:END -->
151```
152
153Default `MATCHWORD` is `AUTO-GENERATED-CONTENT`
154
155---
156
157### 🔀 REMOTE
158
159Get any remote Data and put in markdown
160
161**Options:**
162- `url`: The URL of the remote content to pull in
163
164**Example:**
165```md
166<!-- AUTO-GENERATED-CONTENT:START (REMOTE:url=http://url-to-raw-md-file.md) -->
167This content will be dynamically replace from the remote url
168<!-- AUTO-GENERATED-CONTENT:END -->
169```
170
171Default `MATCHWORD` is `AUTO-GENERATED-CONTENT`
172
173---
174
175### 🔀 TOC
176
177Generate table of contents from markdown file
178
179**Options:**
180- `firsth1` - *boolean* - (optional): Show first h1 of doc in table of contents. Default `false`
181- `collapse` - *boolean* - (optional): Collapse the table of contents in a detail accordian. Default `false`
182- `collapseText` - *string* - (optional): Text the toc accordian summary
183- `excludeText` - *string* - (optional): Text to exclude in the table of contents. Default `Table of Contents`
184
185**Example:**
186```md
187<!-- AUTO-GENERATED-CONTENT:START (TOC) -->
188toc will be generated here
189<!-- AUTO-GENERATED-CONTENT:END -->
190```
191
192Default `MATCHWORD` is `AUTO-GENERATED-CONTENT`
193
194---
195<!-- ⛔️ MD-MAGIC-EXAMPLE:END - Do not remove or modify this section -->
196
197## 🔌 Third Party Transforms a.k.a. Plugins
198
199* [wordcount](https://github.com/DavidWells/markdown-magic-wordcount/) - Add wordcount to markdown files
200* [github-contributors](https://github.com/DavidWells/markdown-magic-github-contributors) - List out the contributors of a given repository
201* [directory-tree](https://github.com/camacho/markdown-magic-directory-tree) - Add directory tree to markdown files
202* [install-command](https://github.com/camacho/markdown-magic-install-command) - Add install command to markdown files with `peerDependencies` included
203* [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))
204* [version-badge](https://github.com/camacho/markdown-magic-version-badge) - Add a badge with the latest version of the project
205* [template](https://github.com/camacho/markdown-magic-template) - Add Lodash template support
206
207## Adding Custom Transforms
208
209Markdown Magic is extendable via plugins.
210
211Plugins 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.
212
213Plugins run in order of registration.
214
215The below code is used to generate **this markdown file** via the plugin system.
216
217<!-- ⛔️ MD-MAGIC-EXAMPLE:START (CODE:src=./examples/generate-readme.js) -->
218<!-- The below code snippet is automatically added from ./examples/generate-readme.js -->
219```js
220const fs = require('fs')
221const path = require('path')
222const execSync = require('child_process').execSync
223const markdownMagic = require('../index') // 'markdown-magic'
224
225const config = {
226 matchWord: 'MD-MAGIC-EXAMPLE', // default matchWord is AUTO-GENERATED-CONTENT
227 transforms: {
228 /* Match <!-- AUTO-GENERATED-CONTENT:START (customTransform:optionOne=hi&optionOne=DUDE) --> */
229 customTransform(content, options) {
230 console.log('original content in comment block', content)
231 console.log('options defined on transform', options)
232 // options = { optionOne: hi, optionOne: DUDE}
233 return `This will replace all the contents of inside the comment ${options.optionOne}`
234 },
235 /* Match <!-- AUTO-GENERATED-CONTENT:START (RENDERDOCS:path=../file.js) --> */
236 RENDERDOCS(content, options) {
237 const fileContents = fs.readFileSync(options.path, 'utf8')
238 const docBlocs = require('dox').parseComments(fileContents, { raw: true, skipSingleStar: true })
239 let updatedContent = ''
240 docBlocs.forEach((data) => {
241 updatedContent += `${data.description.full}\n\n`
242 })
243 return updatedContent.replace(/^\s+|\s+$/g, '')
244 },
245 /* Match <!-- AUTO-GENERATED-CONTENT:START (pluginExample) --> */
246 pluginExample: require('./plugin-example')({ addNewLine: true }),
247 /* Plugins from npm */
248 // count: require('markdown-magic-wordcount'),
249 // github: require('markdown-magic-github-contributors')
250 }
251}
252
253/* This example callback automatically updates Readme.md and commits the changes */
254const callback = function autoGitCommit(err, output) {
255 // output is array of file information
256 output.forEach(function(data) {
257 const mdPath = data.outputFilePath
258 if(!mdPath) return false
259 const gitAdd = execSync(`git add ${mdPath}`, {}, (error) => {
260 if (error) console.warn(error)
261 const msg = `${mdPath} automatically updated by markdown-magic`
262 const gitCommitCommand = `git commit -m '${msg}' --no-verify`
263 execSync(gitCommitCommand, {}, (err) => {
264 if (err) console.warn(err)
265 console.log('git commit automatically ran. Push up your changes!')
266 })
267 })
268 })
269}
270
271const markdownPath = path.join(__dirname, '..', 'README.md')
272markdownMagic(markdownPath, config, callback)
273```
274<!-- ⛔️ MD-MAGIC-EXAMPLE:END -->
275
276## Plugin Example
277
278Plugins must return a transform function with the following signature.
279
280```js
281return function myCustomTransform (content, options)
282```
283
284<!-- ⛔️ MD-MAGIC-EXAMPLE:START (CODE:src=./examples/plugin-example.js) -->
285<!-- The below code snippet is automatically added from ./examples/plugin-example.js -->
286```js
287/* Custom Transform Plugin example */
288const merge = require('deepmerge')
289module.exports = function customPlugin(pluginOptions) {
290 // set plugin defaults
291 const defaultOptions = {
292 addNewLine: false
293 }
294 const userOptions = pluginOptions || {}
295 const pluginConfig = merge(defaultOptions, userOptions)
296 // return the transform function
297 return function myCustomTransform (content, options) {
298 const newLine = (pluginConfig.addNewLine) ? '\n' : ''
299 const updatedContent = content + newLine
300 return updatedContent
301 }
302}
303```
304<!-- ⛔️ MD-MAGIC-EXAMPLE:END -->
305
306[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
307<!-- ⛔️ MD-MAGIC-EXAMPLE:START (pluginExample) DO not edit ⛔️ -->
308This content is altered by the `pluginExample` plugin registered in `examples/generate-readme.js`
309
310<!-- ⛔️ MD-MAGIC-EXAMPLE:END -->
311
312## Other usage examples
313
314- [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
315
316## Custom Transform Demo
317
318View 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
319
320<!-- ⛔️ MD-MAGIC-EXAMPLE:START (customTransform:optionOne=hi&optionOne=DUDE) - Do not remove or modify this section -->
321This will replace all the contents of inside the comment DUDE
322<!-- ⛔️ MD-MAGIC-EXAMPLE:END - Do not remove or modify this section -->
323
324## Prior Art
325
326This 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.
327
328<!-- MD-MAGIC-EXAMPLE:START (LOLZ)-->
329This section was generated by the cli config markdown.config.js file
330<!-- MD-MAGIC-EXAMPLE:END (LOLZ)-->