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 |
|
15 | This `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 |
|
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 |
|
43 |
|
44 | ## Install
|
45 |
|
46 | ```bash
|
47 | npm install markdown-magic --save-dev
|
48 | ```
|
49 |
|
50 | ## Usage
|
51 |
|
52 |
|
53 | ```js
|
54 | import path from 'path'
|
55 | import markdownMagic from 'markdown-magic'
|
56 |
|
57 | const markdownPath = path.join(__dirname, 'README.md')
|
58 | markdownMagic(markdownPath)
|
59 | ```
|
60 |
|
61 |
|
62 | ## CLI Usage
|
63 |
|
64 | You can use `markdown-magic` as a CLI command. Run `markdown --help` to see all available CLI options
|
65 |
|
66 | ```bash
|
67 | markdown --help
|
68 | # or
|
69 | md-magic
|
70 | ```
|
71 |
|
72 | This is useful for adding the package quickly to your `package.json` npm scripts
|
73 |
|
74 | CLI usage example with options
|
75 |
|
76 | ```bash
|
77 | md-magic --path '**/*.md' --config ./config.file.js
|
78 | ```
|
79 |
|
80 | In 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 |
|
88 | If 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 |
|
91 |
|
92 | ```js
|
93 | /* CLI config file example */
|
94 | module.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 |
|
107 |
|
108 |
|
109 | - Do not remove or modify this section -->
|
110 | ### API
|
111 | ```js
|
112 | markdownMagic(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 |
|
118 |
|
119 |
|
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 |
|
131 |
|
132 |
|
133 | ## Transforms
|
134 |
|
135 | Markdown 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 |
|
138 | ### 🔀 CODE
|
139 |
|
140 | Get 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) -->
|
149 | This content will be dynamically replaced with code from the file
|
150 | <!-- AUTO-GENERATED-CONTENT:END -->
|
151 | ```
|
152 |
|
153 | Default `MATCHWORD` is `AUTO-GENERATED-CONTENT`
|
154 |
|
155 | ---
|
156 |
|
157 | ### 🔀 REMOTE
|
158 |
|
159 | Get 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) -->
|
167 | This content will be dynamically replace from the remote url
|
168 | <!-- AUTO-GENERATED-CONTENT:END -->
|
169 | ```
|
170 |
|
171 | Default `MATCHWORD` is `AUTO-GENERATED-CONTENT`
|
172 |
|
173 | ---
|
174 |
|
175 | ### 🔀 TOC
|
176 |
|
177 | Generate 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) -->
|
188 | toc will be generated here
|
189 | <!-- AUTO-GENERATED-CONTENT:END -->
|
190 | ```
|
191 |
|
192 | Default `MATCHWORD` is `AUTO-GENERATED-CONTENT`
|
193 |
|
194 | ---
|
195 |
|
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 |
|
209 | Markdown Magic is extendable via plugins.
|
210 |
|
211 | Plugins 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 |
|
213 | Plugins run in order of registration.
|
214 |
|
215 | The below code is used to generate **this markdown file** via the plugin system.
|
216 |
|
217 |
|
218 |
|
219 | ```js
|
220 | const fs = require('fs')
|
221 | const path = require('path')
|
222 | const execSync = require('child_process').execSync
|
223 | const markdownMagic = require('../index') // 'markdown-magic'
|
224 |
|
225 | const 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 */
|
254 | const 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 |
|
271 | const markdownPath = path.join(__dirname, '..', 'README.md')
|
272 | markdownMagic(markdownPath, config, callback)
|
273 | ```
|
274 |
|
275 |
|
276 | ## Plugin Example
|
277 |
|
278 | Plugins must return a transform function with the following signature.
|
279 |
|
280 | ```js
|
281 | return function myCustomTransform (content, options)
|
282 | ```
|
283 |
|
284 |
|
285 |
|
286 | ```js
|
287 | /* Custom Transform Plugin example */
|
288 | const merge = require('deepmerge')
|
289 | module.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 |
|
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 |
|
308 | This content is altered by the `pluginExample` plugin registered in `examples/generate-readme.js`
|
309 |
|
310 |
|
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 |
|
318 | View 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 |
|
321 | This will replace all the contents of inside the comment DUDE
|
322 |
|
323 |
|
324 | ## Prior Art
|
325 |
|
326 | This 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 |
|
329 | This section was generated by the cli config markdown.config.js file
|
330 |
|