markdown-magic 3.6.5 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cli.js +1 -1
- package/package.json +23 -28
- package/src/{cli.js → cli-run.js} +0 -0
- package/src/{cli.test.js → cli-run.test.js} +1 -1
- package/src/defaults.js +2 -2
- package/src/index.js +30 -22
- package/src/index.test.js +2 -2
- package/src/process-contents.js +2 -458
- package/src/transforms/code/index.js +6 -6
- package/src/transforms/fileTree.js +213 -0
- package/src/transforms/index.js +87 -19
- package/src/transforms/install.js +3 -3
- package/src/transforms/sectionToc.js +4 -4
- package/src/transforms/toc.js +7 -7
- package/src/transforms/wordCount.js +3 -2
- package/src/types.js +1 -1
- package/src/utils/fs.js +22 -0
- package/src/utils/fs.test.js +5 -2
- package/src/utils/index.js +21 -0
- package/src/utils/regex-timeout.js +1 -1
- package/src/utils/syntax.js +29 -1
- package/src/utils/text.js +115 -16
- package/src/utils/text.test.js +76 -3
- package/README.md +0 -605
- package/src/block-parser-js.test.js +0 -171
- package/src/block-parser.js +0 -405
- package/src/block-parser.test.js +0 -481
- package/src/process-file.js +0 -66
package/README.md
DELETED
|
@@ -1,605 +0,0 @@
|
|
|
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
|
-
|
|
15
|
-
The comments markdown magic uses are hidden in markdown and when viewed as HTML.
|
|
16
|
-
|
|
17
|
-
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.
|
|
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 collapseText="Click to expand" -->
|
|
23
|
-
<details>
|
|
24
|
-
<summary>Click to expand</summary>
|
|
25
|
-
|
|
26
|
-
- [About](#about)
|
|
27
|
-
- [Install](#install)
|
|
28
|
-
- [Usage](#usage)
|
|
29
|
-
- [Running via CLI](#running-via-cli)
|
|
30
|
-
- [Running programmatically](#running-programmatically)
|
|
31
|
-
- [Syntax Examples](#syntax-examples)
|
|
32
|
-
- [Basic](#basic)
|
|
33
|
-
- [Curly braces](#curly-braces)
|
|
34
|
-
- [Square brackets](#square-brackets)
|
|
35
|
-
- [Parentheses](#parentheses)
|
|
36
|
-
- [Functions](#functions)
|
|
37
|
-
- [API](#api)
|
|
38
|
-
- [`MarkdownMagicOptions`](#markdownmagicoptions)
|
|
39
|
-
- [`OutputConfig`](#outputconfig)
|
|
40
|
-
- [`MarkdownMagicResult`](#markdownmagicresult)
|
|
41
|
-
- [Transforms](#transforms)
|
|
42
|
-
- [> TOC](#-toc)
|
|
43
|
-
- [> CODE](#-code)
|
|
44
|
-
- [> FILE](#-file)
|
|
45
|
-
- [> REMOTE](#-remote)
|
|
46
|
-
- [Inline transforms](#inline-transforms)
|
|
47
|
-
- [Legacy v1 & v2 plugins](#legacy-v1--v2-plugins)
|
|
48
|
-
- [Adding Custom Transforms](#adding-custom-transforms)
|
|
49
|
-
- [Plugin Example](#plugin-example)
|
|
50
|
-
- [Other usage examples](#other-usage-examples)
|
|
51
|
-
- [Custom Transform Demo](#custom-transform-demo)
|
|
52
|
-
- [Usage examples](#usage-examples)
|
|
53
|
-
- [Misc Markdown helpers](#misc-markdown-helpers)
|
|
54
|
-
- [Prior Art](#prior-art)
|
|
55
|
-
- [License](#license)
|
|
56
|
-
|
|
57
|
-
</details>
|
|
58
|
-
<!-- ⛔️ MD-MAGIC-EXAMPLE:END -->
|
|
59
|
-
|
|
60
|
-
<!-- ⛔️ MD-MAGIC-EXAMPLE:START FILE src=./docs/1_Getting-Started.md -->
|
|
61
|
-
## Install
|
|
62
|
-
|
|
63
|
-
To get started. Install the npm package.
|
|
64
|
-
|
|
65
|
-
```bash
|
|
66
|
-
npm install markdown-magic --save-dev
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
## Usage
|
|
70
|
-
|
|
71
|
-
Use comment blocks in your markdown
|
|
72
|
-
|
|
73
|
-
**Example:**
|
|
74
|
-
```md
|
|
75
|
-
<!-- doc-gen remote url=http://url-to-raw-md-file.md -->
|
|
76
|
-
This content will be dynamically replaced from the remote url
|
|
77
|
-
<!-- end-doc-gen -->
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
Then run `markdown-magic` via it's CLI or programmatically.
|
|
81
|
-
|
|
82
|
-
### Running via CLI
|
|
83
|
-
|
|
84
|
-
Run `markdown --help` to see all available CLI options
|
|
85
|
-
|
|
86
|
-
```bash
|
|
87
|
-
markdown
|
|
88
|
-
# or
|
|
89
|
-
md-magic
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
CLI usage example with options
|
|
93
|
-
|
|
94
|
-
```bash
|
|
95
|
-
md-magic --path '**/*.md' --config ./config.file.js
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
In NPM scripts, `npm run docs` would run the markdown magic and parse all the `.md` files in the directory.
|
|
99
|
-
|
|
100
|
-
```json
|
|
101
|
-
"scripts": {
|
|
102
|
-
"docs": "md-magic --path '**/*.md'"
|
|
103
|
-
},
|
|
104
|
-
```
|
|
105
|
-
|
|
106
|
-
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.
|
|
107
|
-
|
|
108
|
-
### Running programmatically
|
|
109
|
-
|
|
110
|
-
```js
|
|
111
|
-
const { markdownMagic } = require('../src')
|
|
112
|
-
|
|
113
|
-
/* By default all .md files in cwd will be processed */
|
|
114
|
-
markdownMagic().then((results) => {
|
|
115
|
-
console.log('result keys', Object.keys(results))
|
|
116
|
-
})
|
|
117
|
-
```
|
|
118
|
-
|
|
119
|
-
```js
|
|
120
|
-
import path from 'path'
|
|
121
|
-
import markdownMagic from 'markdown-magic'
|
|
122
|
-
|
|
123
|
-
// Process a Single File
|
|
124
|
-
const markdownPath = path.join(__dirname, 'README.md')
|
|
125
|
-
markdownMagic(markdownPath)
|
|
126
|
-
```
|
|
127
|
-
<!-- ⛔️ MD-MAGIC-EXAMPLE:END *-->
|
|
128
|
-
|
|
129
|
-
<!-- ⛔️ MD-MAGIC-EXAMPLE:START (FILE:src=./docs/Syntax.md) -->
|
|
130
|
-
## Syntax Examples
|
|
131
|
-
|
|
132
|
-
There are various syntax options. Choose your favorite.
|
|
133
|
-
|
|
134
|
-
### Basic
|
|
135
|
-
|
|
136
|
-
`openWord transformName [opts]`
|
|
137
|
-
|
|
138
|
-
```md
|
|
139
|
-
<!-- doc-gen transformName optionOne='hello' optionTwo='there' -->
|
|
140
|
-
content to be replaced
|
|
141
|
-
<!-- end-doc-gen -->
|
|
142
|
-
```
|
|
143
|
-
|
|
144
|
-
### Curly braces
|
|
145
|
-
|
|
146
|
-
`openWord {transformName} [opts]`
|
|
147
|
-
|
|
148
|
-
```md
|
|
149
|
-
<!-- doc-gen {transformName} optionOne='hello' optionTwo='there' -->
|
|
150
|
-
content to be replaced
|
|
151
|
-
<!-- end-doc-gen -->
|
|
152
|
-
```
|
|
153
|
-
|
|
154
|
-
### Square brackets
|
|
155
|
-
|
|
156
|
-
`openWord [transformName] [opts]`
|
|
157
|
-
|
|
158
|
-
```md
|
|
159
|
-
<!-- doc-gen [transformName] optionOne='hello' optionTwo='there' -->
|
|
160
|
-
content to be replaced
|
|
161
|
-
<!-- end-doc-gen -->
|
|
162
|
-
```
|
|
163
|
-
|
|
164
|
-
### Parentheses
|
|
165
|
-
|
|
166
|
-
`openWord (transformName) [opts]`
|
|
167
|
-
|
|
168
|
-
```md
|
|
169
|
-
<!-- doc-gen (transformName) optionOne='hello' optionTwo='there' -->
|
|
170
|
-
content to be replaced
|
|
171
|
-
<!-- end-doc-gen -->
|
|
172
|
-
```
|
|
173
|
-
|
|
174
|
-
### Functions
|
|
175
|
-
|
|
176
|
-
`openWord transformName([opts])`
|
|
177
|
-
|
|
178
|
-
```md
|
|
179
|
-
<!-- doc-gen transformName(
|
|
180
|
-
foo='bar'
|
|
181
|
-
baz=['qux', 'quux']
|
|
182
|
-
) -->
|
|
183
|
-
content to be replaced
|
|
184
|
-
<!-- end-doc-gen -->
|
|
185
|
-
```
|
|
186
|
-
<!-- ⛔️ MD-MAGIC-EXAMPLE:END *-->
|
|
187
|
-
|
|
188
|
-
<!-- ⛔️ MD-MAGIC-EXAMPLE:START JSDocs path="./src/index.js" -->
|
|
189
|
-
### API
|
|
190
|
-
|
|
191
|
-
Markdown Magic Instance
|
|
192
|
-
|
|
193
|
-
```js
|
|
194
|
-
markdownMagic(globOrOpts, options)
|
|
195
|
-
```
|
|
196
|
-
|
|
197
|
-
| Name | Type | Description |
|
|
198
|
-
|:---------------------------|:---------------:|:-----------|
|
|
199
|
-
| `globOrOpts` | `FilePathsOrGlobs or MarkdownMagicOptions` | Files to process or config. |
|
|
200
|
-
| `options` (optional) | `MarkdownMagicOptions` | Markdown magic config. |
|
|
201
|
-
|
|
202
|
-
**Returns**
|
|
203
|
-
|
|
204
|
-
`Promise<MarkdownMagicResult>`
|
|
205
|
-
|
|
206
|
-
**Example**
|
|
207
|
-
|
|
208
|
-
```js
|
|
209
|
-
markdownMagic(['**.**.md'], options).then((result) => {
|
|
210
|
-
console.log(`Processing complete`, result)
|
|
211
|
-
})
|
|
212
|
-
```
|
|
213
|
-
|
|
214
|
-
#### `MarkdownMagicOptions`
|
|
215
|
-
|
|
216
|
-
Configuration for markdown magic
|
|
217
|
-
|
|
218
|
-
Below is the main config for `markdown-magic`
|
|
219
|
-
|
|
220
|
-
| Name | Type | Description |
|
|
221
|
-
|:---------------------------|:---------------:|:-----------|
|
|
222
|
-
| `files` (optional) | `FilePathsOrGlobs` | Files to process. |
|
|
223
|
-
| `transforms` (optional) | `Array` | Custom commands to transform block contents, see transforms & custom transforms sections below. Default: `defaultTransforms` |
|
|
224
|
-
| `output` (optional) | `OutputConfig` | Output configuration. |
|
|
225
|
-
| `syntax` (optional) | `SyntaxType` | Syntax to parse. Default: `md` |
|
|
226
|
-
| `open` (optional) | `string` | Opening match word. Default: `doc-gen` |
|
|
227
|
-
| `close` (optional) | `string` | Closing match word. If not defined will be same as opening word. Default: `end-doc-gen` |
|
|
228
|
-
| `cwd` (optional) | `string` | Current working directory. Default process.cwd(). Default: `process.cwd() ` |
|
|
229
|
-
| `outputFlatten` (optional) | `boolean` | Flatten files that are output. |
|
|
230
|
-
| `useGitGlob` (optional) | `boolean` | Use git glob for LARGE file directories. |
|
|
231
|
-
| `dryRun` (optional) | `boolean` | See planned execution of matched blocks. Default: `false` |
|
|
232
|
-
| `debug` (optional) | `boolean` | See debug details. Default: `false` |
|
|
233
|
-
| `silent` (optional) | `boolean` | Silence all console output. Default: `false` |
|
|
234
|
-
| `applyTransformsToSource` (optional) | `boolean` | Apply transforms to source file. Default is true. Default: `true` |
|
|
235
|
-
| `failOnMissingTransforms` (optional) | `boolean` | Fail if transform functions are missing. Default skip blocks. Default: `false` |
|
|
236
|
-
| `failOnMissingRemote` (optional) | `boolean` | Fail if remote file is missing. Default: `true` |
|
|
237
|
-
|
|
238
|
-
#### `OutputConfig`
|
|
239
|
-
|
|
240
|
-
Optional output configuration
|
|
241
|
-
|
|
242
|
-
| Name | Type | Description |
|
|
243
|
-
|:---------------------------|:---------------:|:-----------|
|
|
244
|
-
| `directory` (optional) | `string` | Change output path of new content. Default behavior is replacing the original file. |
|
|
245
|
-
| `removeComments` (optional) | `boolean` | Remove comments from output. Default is false. Default: `false` |
|
|
246
|
-
| `pathFormatter` (optional) | `function` | Custom function for altering output paths. |
|
|
247
|
-
| `applyTransformsToSource` (optional) | `boolean` | Apply transforms to source file. Default is true. This is for when outputDir is set. Default: `false` |
|
|
248
|
-
|
|
249
|
-
#### `MarkdownMagicResult`
|
|
250
|
-
|
|
251
|
-
Result of markdown processing
|
|
252
|
-
|
|
253
|
-
| Name | Type | Description |
|
|
254
|
-
|:---------------------------|:---------------:|:-----------|
|
|
255
|
-
| `errors` | `Array` | Any errors encountered. |
|
|
256
|
-
| `filesChanged` | `Array<string>` | Modified files. |
|
|
257
|
-
| `results` | `Array` | md data. |
|
|
258
|
-
<!-- ⛔️ MD-MAGIC-EXAMPLE:END - Do not remove or modify this section -->
|
|
259
|
-
|
|
260
|
-
## Transforms
|
|
261
|
-
|
|
262
|
-
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.
|
|
263
|
-
|
|
264
|
-
<!-- ⛔️ MD-MAGIC-EXAMPLE:START JSDocs path="./src/transforms/index.js" -->
|
|
265
|
-
### > TOC
|
|
266
|
-
|
|
267
|
-
Generate table of contents from markdown file
|
|
268
|
-
|
|
269
|
-
**Options:**
|
|
270
|
-
- `firsth1` - *boolean* - (optional): Show first h1 of doc in table of contents. Default `false`
|
|
271
|
-
- `collapse` - *boolean* - (optional): Collapse the table of contents in a detail accordion. Default `false`
|
|
272
|
-
- `collapseText` - *string* - (optional): Text the toc accordion summary
|
|
273
|
-
- `excludeText` - *string* - (optional): Text to exclude in the table of contents. Default `Table of Contents`
|
|
274
|
-
- `maxDepth` - *number* - (optional): Max depth of headings. Default 4
|
|
275
|
-
|
|
276
|
-
**Example:**
|
|
277
|
-
```md
|
|
278
|
-
<!-- doc-gen TOC -->
|
|
279
|
-
toc will be generated here
|
|
280
|
-
<!-- end-doc-gen -->
|
|
281
|
-
```
|
|
282
|
-
|
|
283
|
-
Default `matchWord` is `doc-gen`
|
|
284
|
-
|
|
285
|
-
---
|
|
286
|
-
|
|
287
|
-
| Name | Type | Description |
|
|
288
|
-
|:---------------------------|:---------------:|:-----------|
|
|
289
|
-
| `content` | `string` | The current content of the comment block. |
|
|
290
|
-
| `options` | `object` | The options passed in from the comment declaration. |
|
|
291
|
-
|
|
292
|
-
### > CODE
|
|
293
|
-
|
|
294
|
-
Get code from file or URL and put in markdown
|
|
295
|
-
|
|
296
|
-
**Options:**
|
|
297
|
-
- `src`: The relative path to the code to pull in, or the `URL` where the raw code lives
|
|
298
|
-
- `syntax` (optional): Syntax will be inferred by fileType if not specified
|
|
299
|
-
- `header` (optional): Will add header comment to code snippet. Useful for pointing to relative source directory or adding live doc links
|
|
300
|
-
- `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
|
|
301
|
-
|
|
302
|
-
**Example:**
|
|
303
|
-
```md
|
|
304
|
-
<!-- doc-gen CODE src="./relative/path/to/code.js" -->
|
|
305
|
-
This content will be dynamically replaced with code from the file
|
|
306
|
-
<!-- end-doc-gen -->
|
|
307
|
-
```
|
|
308
|
-
|
|
309
|
-
```md
|
|
310
|
-
<!-- doc-gen CODE src="./relative/path/to/code.js" lines=22-44 -->
|
|
311
|
-
This content will be dynamically replaced with code from the file lines 22 through 44
|
|
312
|
-
<!-- end-doc-gen -->
|
|
313
|
-
```
|
|
314
|
-
|
|
315
|
-
Default `matchWord` is `doc-gen`
|
|
316
|
-
|
|
317
|
-
---
|
|
318
|
-
|
|
319
|
-
| Name | Type | Description |
|
|
320
|
-
|:---------------------------|:---------------:|:-----------|
|
|
321
|
-
| `content` | `string` | The current content of the comment block. |
|
|
322
|
-
| `options` | `object` | The options passed in from the comment declaration. |
|
|
323
|
-
|
|
324
|
-
### > FILE
|
|
325
|
-
|
|
326
|
-
Get local file contents.
|
|
327
|
-
|
|
328
|
-
**Options:**
|
|
329
|
-
- `src`: The relative path to the file to pull in
|
|
330
|
-
|
|
331
|
-
**Example:**
|
|
332
|
-
```md
|
|
333
|
-
<!-- doc-gen FILE src=./path/to/file -->
|
|
334
|
-
This content will be dynamically replaced from the local file
|
|
335
|
-
<!-- end-doc-gen -->
|
|
336
|
-
```
|
|
337
|
-
|
|
338
|
-
Default `matchWord` is `doc-gen`
|
|
339
|
-
|
|
340
|
-
---
|
|
341
|
-
|
|
342
|
-
| Name | Type | Description |
|
|
343
|
-
|:---------------------------|:---------------:|:-----------|
|
|
344
|
-
| `content` | `string` | The current content of the comment block. |
|
|
345
|
-
| `options` | `object` | The options passed in from the comment declaration. |
|
|
346
|
-
|
|
347
|
-
### > REMOTE
|
|
348
|
-
|
|
349
|
-
Get any remote Data and put in markdown
|
|
350
|
-
|
|
351
|
-
**Options:**
|
|
352
|
-
- `url`: The URL of the remote content to pull in
|
|
353
|
-
|
|
354
|
-
**Example:**
|
|
355
|
-
```md
|
|
356
|
-
<!-- doc-gen REMOTE url=http://url-to-raw-md-file.md -->
|
|
357
|
-
This content will be dynamically replaced from the remote url
|
|
358
|
-
<!-- end-doc-gen -->
|
|
359
|
-
```
|
|
360
|
-
|
|
361
|
-
Default `matchWord` is `doc-gen`
|
|
362
|
-
|
|
363
|
-
---
|
|
364
|
-
|
|
365
|
-
| Name | Type | Description |
|
|
366
|
-
|:---------------------------|:---------------:|:-----------|
|
|
367
|
-
| `content` | `string` | The current content of the comment block. |
|
|
368
|
-
| `options` | `object` | The options passed in from the comment declaration. |
|
|
369
|
-
<!-- ⛔️ MD-MAGIC-EXAMPLE:END - Do not remove or modify this section -->
|
|
370
|
-
|
|
371
|
-
## Inline transforms
|
|
372
|
-
|
|
373
|
-
Any transform, including custom transforms can be used inline as well to insert content into paragraphs and other places.
|
|
374
|
-
|
|
375
|
-
The face symbol 👉 <!-- MD-MAGIC-EXAMPLE:START (INLINE_EXAMPLE) -->**⊂◉‿◉つ**<!-- MD-MAGIC-EXAMPLE:END --> is auto generated inline.
|
|
376
|
-
|
|
377
|
-
**Example:**
|
|
378
|
-
```md
|
|
379
|
-
<!-- doc-gen (FILE:src=./path/to/file) -->xyz<!-- end-doc-gen -->
|
|
380
|
-
```
|
|
381
|
-
|
|
382
|
-
## Legacy v1 & v2 plugins
|
|
383
|
-
|
|
384
|
-
These plugins work with older versions of markdown-magic. Adapting them to the newer plugin syntax should be pretty straight forward.
|
|
385
|
-
|
|
386
|
-
* [wordcount](https://github.com/DavidWells/markdown-magic-wordcount/) - Add wordcount to markdown files
|
|
387
|
-
* [github-contributors](https://github.com/DavidWells/markdown-magic-github-contributors) - List out the contributors of a given repository
|
|
388
|
-
* [directory-tree](https://github.com/camacho/markdown-magic-directory-tree) - Add directory tree to markdown files
|
|
389
|
-
* [install-command](https://github.com/camacho/markdown-magic-install-command) - Add install command to markdown files with `peerDependencies` included
|
|
390
|
-
* [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))
|
|
391
|
-
* [version-badge](https://github.com/camacho/markdown-magic-version-badge) - Add a badge with the latest version of the project
|
|
392
|
-
* [template](https://github.com/camacho/markdown-magic-template) - Add Lodash template support
|
|
393
|
-
* [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
|
|
394
|
-
* [package-scripts](https://github.com/camacho/markdown-magic-package-scripts) - Add a table of `package.json` scripts with descriptions
|
|
395
|
-
* [prettier](https://github.com/camacho/markdown-magic-prettier) - Format code blocks with [`prettier`](https://github.com/prettier/prettier)
|
|
396
|
-
* [engines](https://github.com/camacho/markdown-magic-engines) - Print engines list from `package.json`
|
|
397
|
-
* [jsdoc](https://github.com/bradtaylorsf/markdown-magic-jsdoc) - Adds jsdoc comment support
|
|
398
|
-
* [build-badge](https://github.com/rishichawda/markdown-magic-build-badge) - Update branch badges to auto-magically point to current branches.
|
|
399
|
-
* [package-json](https://github.com/forresst/markdown-magic-package-json) - Add the package.json properties to markdown files
|
|
400
|
-
* [figlet](https://github.com/lafourchette/markdown-magic-figlet) - Add FIGfont text to markdown files
|
|
401
|
-
* [local-image](https://github.com/stevenbenisek/markdown-magic-local-image) - plugin to add local images to markdown
|
|
402
|
-
* [markdown-magic-build-badge](https://github.com/rishichawda/markdown-magic-build-badge) - A plugin to update your branch badges to point to correct branch status
|
|
403
|
-
|
|
404
|
-
## Adding Custom Transforms
|
|
405
|
-
|
|
406
|
-
Markdown Magic is extendable via plugins.
|
|
407
|
-
|
|
408
|
-
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.
|
|
409
|
-
|
|
410
|
-
Plugins run in order of registration.
|
|
411
|
-
|
|
412
|
-
The below code is used to generate **this markdown file** via the plugin system.
|
|
413
|
-
|
|
414
|
-
<!-- ⛔️ MD-MAGIC-EXAMPLE:START (CODE:src=./examples/generate-readme.js) -->
|
|
415
|
-
```js
|
|
416
|
-
const path = require('path')
|
|
417
|
-
const { readFileSync } = require('fs')
|
|
418
|
-
const { parseComments } = require('doxxx')
|
|
419
|
-
const { markdownMagic } = require('../src')
|
|
420
|
-
const { deepLog } = require('../src/utils/logs')
|
|
421
|
-
|
|
422
|
-
const config = {
|
|
423
|
-
matchWord: 'MD-MAGIC-EXAMPLE', // default matchWord is AUTO-GENERATED-CONTENT
|
|
424
|
-
transforms: {
|
|
425
|
-
/* Match <!-- AUTO-GENERATED-CONTENT:START (customTransform:optionOne=hi&optionOne=DUDE) --> */
|
|
426
|
-
customTransform({ content, options }) {
|
|
427
|
-
console.log('original content in comment block', content)
|
|
428
|
-
console.log('options defined on transform', options)
|
|
429
|
-
// options = { optionOne: hi, optionOne: DUDE}
|
|
430
|
-
return `This will replace all the contents of inside the comment ${options.optionOne}`
|
|
431
|
-
},
|
|
432
|
-
/* Match <!-- AUTO-GENERATED-CONTENT:START JSDocs path="../file.js" --> */
|
|
433
|
-
JSDocs(markdownMagicPluginAPI) {
|
|
434
|
-
const { options } = markdownMagicPluginAPI
|
|
435
|
-
const fileContents = readFileSync(options.path, 'utf8')
|
|
436
|
-
const docBlocs = parseComments(fileContents, { skipSingleStar: true })
|
|
437
|
-
.filter((item) => {
|
|
438
|
-
return !item.isIgnored
|
|
439
|
-
})
|
|
440
|
-
/* Remove empty comments with no tags */
|
|
441
|
-
.filter((item) => {
|
|
442
|
-
return item.tags.length
|
|
443
|
-
})
|
|
444
|
-
/* Remove inline type defs */
|
|
445
|
-
.filter((item) => {
|
|
446
|
-
return item.description.text !== ''
|
|
447
|
-
})
|
|
448
|
-
/* Sort types to end */
|
|
449
|
-
.sort((a, b) => {
|
|
450
|
-
if (a.type && !b.type) return 1
|
|
451
|
-
if (!a.type && b.type) return -1
|
|
452
|
-
return 0
|
|
453
|
-
})
|
|
454
|
-
|
|
455
|
-
docBlocs.forEach((data) => {
|
|
456
|
-
// console.log('data', data)
|
|
457
|
-
delete data.code
|
|
458
|
-
})
|
|
459
|
-
// console.log('docBlocs', docBlocs)
|
|
460
|
-
|
|
461
|
-
if (docBlocs.length === 0) {
|
|
462
|
-
throw new Error('No docBlocs found')
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
// console.log(docBlocs.length)
|
|
466
|
-
let updatedContent = ''
|
|
467
|
-
docBlocs.forEach((data) => {
|
|
468
|
-
if (data.type) {
|
|
469
|
-
updatedContent += `#### \`${data.type}\`\n\n`
|
|
470
|
-
}
|
|
471
|
-
|
|
472
|
-
updatedContent += `${data.description.text}\n`
|
|
473
|
-
|
|
474
|
-
if (data.tags.length) {
|
|
475
|
-
let table = '| Name | Type | Description |\n'
|
|
476
|
-
table += '|:---------------------------|:---------------:|:-----------|\n'
|
|
477
|
-
data.tags.filter((tag) => {
|
|
478
|
-
if (tag.tagType === 'param') return true
|
|
479
|
-
if (tag.tagType === 'property') return true
|
|
480
|
-
return false
|
|
481
|
-
}).forEach((tag) => {
|
|
482
|
-
const optionalText = tag.isOptional ? ' (optional) ' : ' '
|
|
483
|
-
const defaultValueText = (typeof tag.defaultValue !== 'undefined') ? ` Default: \`${tag.defaultValue}\` ` : ' '
|
|
484
|
-
table += `| \`${tag.name}\`${optionalText}`
|
|
485
|
-
table += `| \`${tag.type.replace('|', 'or')}\` `
|
|
486
|
-
table += `| ${tag.description.replace(/\.\s?$/, '')}.${defaultValueText}|\n`
|
|
487
|
-
})
|
|
488
|
-
updatedContent+= `\n${table}\n`
|
|
489
|
-
|
|
490
|
-
const returnValues = data.tags.filter((tag) => tag.tagType === 'returns')
|
|
491
|
-
if (returnValues.length) {
|
|
492
|
-
returnValues.forEach((returnValue) => {
|
|
493
|
-
updatedContent += `**Returns**\n\n`
|
|
494
|
-
updatedContent += `\`${returnValue.type}\`\n\n`
|
|
495
|
-
})
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
const examples = data.tags.filter((tag) => tag.tagType === 'example')
|
|
499
|
-
if (examples.length) {
|
|
500
|
-
examples.forEach((example) => {
|
|
501
|
-
updatedContent += `**Example**\n\n`
|
|
502
|
-
updatedContent += `\`\`\`js\n${example.tagValue}\n\`\`\`\n\n`
|
|
503
|
-
})
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
|
-
})
|
|
507
|
-
return updatedContent.replace(/^\s+|\s+$/g, '')
|
|
508
|
-
},
|
|
509
|
-
INLINE_EXAMPLE: () => {
|
|
510
|
-
return '**⊂◉‿◉つ**'
|
|
511
|
-
},
|
|
512
|
-
lolz() {
|
|
513
|
-
return `This section was generated by the cli config markdown.config.js file`
|
|
514
|
-
},
|
|
515
|
-
/* Match <!-- AUTO-GENERATED-CONTENT:START (pluginExample) --> */
|
|
516
|
-
pluginExample: require('./plugin-example')({ addNewLine: true }),
|
|
517
|
-
/* Include plugins from NPM */
|
|
518
|
-
// count: require('markdown-magic-wordcount'),
|
|
519
|
-
// github: require('markdown-magic-github-contributors')
|
|
520
|
-
}
|
|
521
|
-
}
|
|
522
|
-
|
|
523
|
-
const markdownPath = path.join(__dirname, '..', 'README.md')
|
|
524
|
-
markdownMagic(markdownPath, config, () => {
|
|
525
|
-
console.log('Docs ready')
|
|
526
|
-
})
|
|
527
|
-
```
|
|
528
|
-
<!-- ⛔️ MD-MAGIC-EXAMPLE:END -->
|
|
529
|
-
|
|
530
|
-
## Plugin Example
|
|
531
|
-
|
|
532
|
-
Plugins must return a transform function with the following signature.
|
|
533
|
-
|
|
534
|
-
```js
|
|
535
|
-
return function myCustomTransform (content, options)
|
|
536
|
-
```
|
|
537
|
-
|
|
538
|
-
<!-- ⛔️ MD-MAGIC-EXAMPLE:START (CODE:src=./examples/plugin-example.js) -->
|
|
539
|
-
```js
|
|
540
|
-
/* Custom Transform Plugin example */
|
|
541
|
-
module.exports = function customPlugin(pluginOptions) {
|
|
542
|
-
// set plugin defaults
|
|
543
|
-
const defaultOptions = {
|
|
544
|
-
addNewLine: false
|
|
545
|
-
}
|
|
546
|
-
const userOptions = pluginOptions || {}
|
|
547
|
-
const pluginConfig = Object.assign(defaultOptions, userOptions)
|
|
548
|
-
// return the transform function
|
|
549
|
-
return function myCustomTransform ({ content, options }) {
|
|
550
|
-
const newLine = (pluginConfig.addNewLine) ? '\n' : ''
|
|
551
|
-
const updatedContent = content + newLine
|
|
552
|
-
return updatedContent
|
|
553
|
-
}
|
|
554
|
-
}
|
|
555
|
-
```
|
|
556
|
-
<!-- ⛔️ MD-MAGIC-EXAMPLE:END -->
|
|
557
|
-
|
|
558
|
-
[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
|
|
559
|
-
<!-- ⛔️ MD-MAGIC-EXAMPLE:START (pluginExample) ⛔️ -->
|
|
560
|
-
This content is altered by the `pluginExample` plugin registered in `examples/generate-readme.js`
|
|
561
|
-
<!-- ⛔️ MD-MAGIC-EXAMPLE:END -->
|
|
562
|
-
|
|
563
|
-
## Other usage examples
|
|
564
|
-
|
|
565
|
-
- [With Github Actions](https://github.com/dineshsonachalam/repo-using-markdown-autodocs)
|
|
566
|
-
- [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
|
|
567
|
-
- [MochaJS](https://github.com/mochajs/mocha/blob/4cc711fa00f7166a2303b77bf2487d1c2cc94621/scripts/markdown-magic.config.js)
|
|
568
|
-
- [tc39/agendas](https://github.com/tc39/agendas#agendas) - [code](https://github.com/tc39/agendas/blob/65945b1b6658e9829ef95a51bf2632ff44f951e6/scripts/generate.js)
|
|
569
|
-
- [moleculerjs/moleculer-addons](https://github.com/moleculerjs/moleculer-addons/blob/7cf0f72140717c52621b724cd54a710517106df0/readme-generator.js)
|
|
570
|
-
- [good-first-issue](https://github.com/bnb/good-first-issue/blob/e65513a1f26167dea3c137008b8796640d8d5303/markdown.config.js)
|
|
571
|
-
- [navikt/nav-frontend-moduler](https://github.com/navikt/nav-frontend-moduler/blob/20ad521c27a43d3203eab4bc32121e5b8270c077/_scripts/generateReadmes.js)
|
|
572
|
-
- [country-flags-svg](https://github.com/ronatskiy/country-flags-svg/blob/cfb2368c7e634ebc1679855e13cc3e26ca11187f/markdown.config.js)
|
|
573
|
-
- [react-typesetting](https://github.com/exogen/react-typesetting/blob/7114cdc8c4cb1b0d59ebc8b5364e808687419889/markdown.config.js)
|
|
574
|
-
- [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)
|
|
575
|
-
|
|
576
|
-
## Custom Transform Demo
|
|
577
|
-
|
|
578
|
-
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
|
|
579
|
-
|
|
580
|
-
<!-- ⛔️ MD-MAGIC-EXAMPLE:START (customTransform:optionOne=hi&optionOne=DUDE) - Do not remove or modify this section -->
|
|
581
|
-
This will replace all the contents of inside the comment DUDE
|
|
582
|
-
<!-- ⛔️ MD-MAGIC-EXAMPLE:END - Do not remove or modify this section -->
|
|
583
|
-
|
|
584
|
-
## Usage examples
|
|
585
|
-
|
|
586
|
-
- [Projects using markdown-magic](https://github.com/search?q=path%3A**%2Fpackage.json+%22markdown-magic%22&type=code)
|
|
587
|
-
- [Examples in md](https://github.com/search?l=Markdown&o=desc&q=AUTO-GENERATED-CONTENT&s=indexed&type=Code)
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
## Misc Markdown helpers
|
|
591
|
-
|
|
592
|
-
- https://github.com/azu/markdown-function
|
|
593
|
-
|
|
594
|
-
## Prior Art
|
|
595
|
-
|
|
596
|
-
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.
|
|
597
|
-
|
|
598
|
-
## License
|
|
599
|
-
|
|
600
|
-
[MIT][mit] © [DavidWells][author]
|
|
601
|
-
|
|
602
|
-
[npm-badge]:https://img.shields.io/npm/v/markdown-magic.svg?style=flat-square
|
|
603
|
-
[npm-link]: http://www.npmjs.com/package/markdown-magic
|
|
604
|
-
[mit]: http://opensource.org/licenses/MIT
|
|
605
|
-
[author]: http://github.com/davidwells
|