UNPKG

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