markdown-magic 3.0.0 → 3.0.2

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.
Files changed (49) hide show
  1. package/README.md +43 -29
  2. package/lib/block-parser-js.test.js +148 -156
  3. package/lib/block-parser.js +255 -262
  4. package/lib/block-parser.test.js +43 -6
  5. package/lib/cli.js +30 -19
  6. package/lib/cli.test.js +73 -73
  7. package/lib/globals.d.ts +66 -0
  8. package/lib/index.js +43 -9
  9. package/lib/process-contents.js +80 -39
  10. package/lib/process-file.js +4 -1
  11. package/lib/transforms/code.js +4 -10
  12. package/lib/transforms/file.js +7 -10
  13. package/lib/transforms/index.js +0 -0
  14. package/lib/transforms/remote.js +2 -3
  15. package/lib/transforms/sectionToc.js +18 -0
  16. package/lib/transforms/toc.js +10 -335
  17. package/lib/types.js +11 -0
  18. package/lib/utils/fs.js +21 -19
  19. package/lib/utils/fs.test.js +4 -5
  20. package/lib/utils/logs.js +7 -2
  21. package/lib/utils/md/filters.js +5 -5
  22. package/lib/utils/md/find-code-blocks.js +16 -8
  23. package/lib/utils/md/find-frontmatter.js +11 -13
  24. package/lib/utils/md/find-frontmatter.test.js +2 -2
  25. package/lib/utils/md/find-html-tags.js +1 -1
  26. package/lib/utils/md/find-images-md.js +27 -0
  27. package/lib/utils/md/find-images.js +39 -34
  28. package/lib/utils/md/find-links.js +72 -54
  29. package/lib/utils/md/find-unmatched-html-tags.js +1 -2
  30. package/lib/utils/md/fixtures/file-with-links.md +10 -0
  31. package/lib/utils/md/md.test.js +72 -4
  32. package/lib/utils/md/parse.js +91 -67
  33. package/lib/utils/regex-timeout.js +2 -1
  34. package/lib/utils/regex.js +3 -2
  35. package/lib/utils/remoteRequest.js +1 -0
  36. package/lib/utils/syntax.js +3 -0
  37. package/lib/utils/text.js +71 -3
  38. package/lib/utils/text.test.js +3 -9
  39. package/lib/utils/toc.js +315 -0
  40. package/package.json +7 -3
  41. package/lib/options-parser.js +0 -498
  42. package/lib/options-parser.test.js +0 -1237
  43. package/lib/utils/html-to-json/compat.js +0 -42
  44. package/lib/utils/html-to-json/format.js +0 -64
  45. package/lib/utils/html-to-json/index.js +0 -37
  46. package/lib/utils/html-to-json/lexer.js +0 -345
  47. package/lib/utils/html-to-json/parser.js +0 -146
  48. package/lib/utils/html-to-json/stringify.js +0 -37
  49. package/lib/utils/html-to-json/tags.js +0 -171
package/README.md CHANGED
@@ -21,7 +21,7 @@ This `README.md` is generated with `markdown-magic` [view the raw file](https://
21
21
  ## Table of Contents
22
22
  <!-- ⛔️ MD-MAGIC-EXAMPLE:START (TOC:collapse=true&collapseText=Click to expand) -->
23
23
  <details>
24
- <summary>Click to expaxnd</summary>
24
+ <summary>Click to expand</summary>
25
25
 
26
26
  - [About](#about)
27
27
  - [Install](#install)
@@ -43,6 +43,7 @@ This `README.md` is generated with `markdown-magic` [view the raw file](https://
43
43
  - [Prior Art](#prior-art)
44
44
  - [License](#license)
45
45
  - [Usage examples](#usage-examples)
46
+ - [Misc Markdown helpers](#misc-markdown-helpers)
46
47
 
47
48
  </details>
48
49
  <!-- ⛔️ MD-MAGIC-EXAMPLE:END -->
@@ -55,7 +56,7 @@ npm install markdown-magic --save-dev
55
56
 
56
57
  ## Usage
57
58
  <!-- ⛔️ MD-MAGIC-EXAMPLE:START (CODE:src=./examples/1-_basic-usage.js) -->
58
- <!-- The below code snippet is automatically added from ./examples/basic-usage.js -->
59
+ <!-- The below code snippet is automatically added from ./examples/1-_basic-usage.js -->
59
60
  ```js
60
61
  import path from 'path'
61
62
  import markdownMagic from 'markdown-magic'
@@ -65,19 +66,14 @@ markdownMagic(markdownPath)
65
66
  ```
66
67
  <!-- ⛔️ MD-MAGIC-EXAMPLE:END *-->
67
68
 
69
+ ### API
68
70
 
69
71
  <!-- ⛔️ MD-MAGIC-EXAMPLE:START (RENDERDOCS:path=./lib/index.js)
70
72
  - Do not remove or modify this section -->
71
- ### API
72
- ```js
73
- markdownMagic(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
73
+ Markdown Magic
78
74
  <!-- ⛔️ MD-MAGIC-EXAMPLE:END - Do not remove or modify this section -->
79
75
 
80
- <!-- ⛔️ MD-MAGIC-EXAMPLE:START (RENDERDOCS:path=./lib/processFile.js)
76
+ <!-- ⛔️ MD-MAGIC-EXAMPLE:START (RENDERDOCS:path=./lib/process-file.js)
81
77
  - Do not remove or modify this section -->
82
78
  ### Configuration Options
83
79
 
@@ -118,20 +114,27 @@ In NPM scripts, `npm run docs` would run the markdown magic and parse all the `.
118
114
 
119
115
  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.
120
116
 
121
- <!-- ⛔️ MD-MAGIC-EXAMPLE:START (CODE:src=./markdown.config.js) -->
122
- <!-- The below code snippet is automatically added from ./markdown.config.js -->
117
+ <!-- ⛔️ MD-MAGIC-EXAMPLE:START (CODE:src=./md.config.js) -->
118
+ <!-- The below code snippet is automatically added from ./md.config.js -->
123
119
  ```js
124
120
  /* CLI markdown.config.js file example */
125
121
  module.exports = {
126
- matchWord: 'MD-MAGIC-EXAMPLE',
122
+ // handleOutputPath: (currentPath) => {
123
+ // const newPath = 'x' + currentPath
124
+ // return newPath
125
+ // },
126
+ // handleOutputPath: (currentPath) => {
127
+ // const newPath = currentPath.replace(/fixtures/, 'fixtures-out')
128
+ // return newPath
129
+ // },
127
130
  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
+ /* Match <!-- AUTO-GENERATED-CONTENT:START (transformOne) --> */
132
+ transformOne() {
133
+ return `This section was generated by the cli config md.config.js file`
134
+ },
135
+ functionName() {
136
+ return `xyz`
131
137
  }
132
- },
133
- callback: function () {
134
- console.log('markdown processing done')
135
138
  }
136
139
  }
137
140
  ```
@@ -144,7 +147,7 @@ Markdown Magic comes with a couple of built-in transforms for you to use or you
144
147
  <!-- ⛔️ MD-MAGIC-EXAMPLE:START (RENDERDOCS:path=./lib/transforms/index.js) - Do not remove or modify this section -->
145
148
  ### > TOC
146
149
 
147
- Generate table of contents from markdown file
150
+ Generate taxxxxble of contents from markdown file
148
151
 
149
152
  **Options:**
150
153
  - `firsth1` - *boolean* - (optional): Show first h1 of doc in table of contents. Default `false`
@@ -274,23 +277,32 @@ The below code is used to generate **this markdown file** via the plugin system.
274
277
  ```js
275
278
  const fs = require('fs')
276
279
  const path = require('path')
277
- const markdownMagic = require('../index')
278
- // const markdownMagic = require('markdown-magic')
280
+ const doxxx = require('doxxx')
281
+ const { markdownMagic } = require('../lib')
282
+ // const { markdownMagic } = require('markdown-magic')
279
283
 
280
284
  const config = {
281
285
  matchWord: 'MD-MAGIC-EXAMPLE', // default matchWord is AUTO-GENERATED-CONTENT
282
286
  transforms: {
283
287
  /* Match <!-- AUTO-GENERATED-CONTENT:START (customTransform:optionOne=hi&optionOne=DUDE) --> */
284
- customTransform(content, options) {
288
+ customTransform({ content, options }) {
285
289
  console.log('original content in comment block', content)
286
290
  console.log('options defined on transform', options)
287
291
  // options = { optionOne: hi, optionOne: DUDE}
288
292
  return `This will replace all the contents of inside the comment ${options.optionOne}`
289
293
  },
290
294
  /* Match <!-- AUTO-GENERATED-CONTENT:START (RENDERDOCS:path=../file.js) --> */
291
- RENDERDOCS(content, options) {
295
+ RENDERDOCS(api) {
296
+ console.log('api', api)
297
+ const { options } = api
298
+ console.log('options.path', path.resolve(options.path))
292
299
  const fileContents = fs.readFileSync(options.path, 'utf8')
293
- const docBlocs = require('doxxx').parseComments(fileContents, { raw: true, skipSingleStar: true })
300
+ const docBlocs = doxxx.parseComments(fileContents, { raw: true, skipSingleStar: true })
301
+ .filter((item) => {
302
+ return item.tags.length
303
+ })
304
+ console.log('docBlocs', docBlocs)
305
+ // process.exit(1)
294
306
  let updatedContent = ''
295
307
  docBlocs.forEach((data) => {
296
308
  updatedContent += `${data.description.full}\n\n`
@@ -300,6 +312,10 @@ const config = {
300
312
  INLINE_EXAMPLE: () => {
301
313
  return '**⊂◉‿◉つ**'
302
314
  },
315
+ lolz() {
316
+ console.log('run lol')
317
+ return `This section was generated by the cli config markdown.config.js file`
318
+ },
303
319
  /* Match <!-- AUTO-GENERATED-CONTENT:START (pluginExample) --> */
304
320
  pluginExample: require('./plugin-example')({ addNewLine: true }),
305
321
  /* Include plugins from NPM */
@@ -327,16 +343,15 @@ return function myCustomTransform (content, options)
327
343
  <!-- The below code snippet is automatically added from ./examples/plugin-example.js -->
328
344
  ```js
329
345
  /* Custom Transform Plugin example */
330
- const merge = require('deepmerge')
331
346
  module.exports = function customPlugin(pluginOptions) {
332
347
  // set plugin defaults
333
348
  const defaultOptions = {
334
349
  addNewLine: false
335
350
  }
336
351
  const userOptions = pluginOptions || {}
337
- const pluginConfig = merge(defaultOptions, userOptions)
352
+ const pluginConfig = Object.assign(defaultOptions, userOptions)
338
353
  // return the transform function
339
- return function myCustomTransform (content, options) {
354
+ return function myCustomTransform ({ content, options }) {
340
355
  const newLine = (pluginConfig.addNewLine) ? '\n' : ''
341
356
  const updatedContent = content + newLine
342
357
  return updatedContent
@@ -348,7 +363,6 @@ module.exports = function customPlugin(pluginOptions) {
348
363
  [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
364
  <!-- ⛔️ MD-MAGIC-EXAMPLE:START (pluginExample) ⛔️ -->
350
365
  This content is altered by the `pluginExample` plugin registered in `examples/generate-readme.js`
351
-
352
366
  <!-- ⛔️ MD-MAGIC-EXAMPLE:END -->
353
367
 
354
368
  ## Other usage examples
@@ -12,168 +12,160 @@ test('JS file parse', async () => {
12
12
  open: 'GENERATED',
13
13
  close: 'END-GENERATED',
14
14
  })
15
- // deepLog(blocks)
16
- assert.equal(blocks, {
17
- pattern: /([ \t]*)(?:\/\*+(?:.*|\r?|\n?|\s*)GENERATED\s*([(\[\{]*[A-Za-z0-9_$-]*[)\]\}]*)\s*)((?:.*?|.*?\r?\n?)*?)\/\*+(?:.*|\r?|\n?|\s*)END-GENERATED(?:.|\r?\n)*?\*+\//gim,
18
- COMMENT_OPEN_REGEX: /([ \t]*)(\/\*+(?:.|\r?|\n?|\s*)\bGENERATED\b)((?:.|\r?\n)*?\*+\/\n?)/gi,
19
- COMMENT_CLOSE_REGEX: /\*+\/(?:.|\r?\n)*?([ ]*)((?:\/\*+(?:.*|\r?\n)(?:.*|\r?\n))*?\bEND-GENERATED\b)((?:.|\r?\n)*?\*+\/)/gi,
20
- blocks: [
21
- {
22
- index: 2,
23
- type: 'a',
24
- options: {},
25
- context: { isMultiline: true },
26
- open: { value: '/* GENERATED a */\n', start: 96, end: 113 },
27
- content: {
28
- value: '// comment inside',
29
- start: 113,
30
- end: 132,
31
- indentation: 0
32
- },
33
- close: { value: '\n/* END-GENERATED */', start: 132, end: 151 },
34
- block: {
35
- indentation: '',
36
- lines: [ 7, 9 ],
37
- start: 96,
38
- end: 151,
39
- rawType: 'a',
40
- rawArgs: '',
41
- rawContent: '\n// comment inside\n',
42
- value: '/* GENERATED a */\n// comment inside\n/* END-GENERATED */'
43
- }
15
+ /*
16
+ deepLog(blocks.blocks)
17
+ /** */
18
+ assert.equal(blocks.blocks, [
19
+ {
20
+ index: 1,
21
+ type: 'a',
22
+ options: {},
23
+ context: { isMultiline: true },
24
+ open: { value: '/* ⛔️ GENERATED a */\n', start: 96, end: 117 },
25
+ content: {
26
+ value: '// comment inside',
27
+ start: 117,
28
+ end: 134,
29
+ indentation: 0
44
30
  },
45
- {
46
- index: 3,
47
- type: 'b',
48
- options: {},
49
- context: { isMultiline: true },
50
- open: { value: '/* GENERATED b */\n', start: 153, end: 170 },
51
- content: { value: '', start: 170, end: 171, indentation: 0 },
52
- close: {
53
- value: '/* comment inside */\n/* END-GENERATED */',
54
- start: 171,
55
- end: 211
56
- },
57
- block: {
58
- indentation: '',
59
- lines: [ 11, 13 ],
60
- start: 153,
61
- end: 211,
62
- rawType: 'b',
63
- rawArgs: '',
64
- rawContent: '\n',
65
- value: '/* GENERATED b */\n/* comment inside */\n/* END-GENERATED */'
66
- }
31
+ close: { value: '\n/* END-GENERATED */', start: 134, end: 154 },
32
+ block: {
33
+ indentation: '',
34
+ lines: [ 7, 9 ],
35
+ start: 96,
36
+ end: 154,
37
+ rawArgs: '',
38
+ rawContent: '// comment inside',
39
+ value: '/* ⛔️ GENERATED a */\n// comment inside\n/* END-GENERATED */'
40
+ }
41
+ },
42
+ {
43
+ index: 2,
44
+ type: 'b',
45
+ options: {},
46
+ context: { isMultiline: true },
47
+ open: { value: '/* GENERATED b */\n', start: 156, end: 174 },
48
+ content: {
49
+ value: '/* comment inside */',
50
+ start: 174,
51
+ end: 194,
52
+ indentation: 0
53
+ },
54
+ close: { value: '\n/* END-GENERATED */', start: 194, end: 214 },
55
+ block: {
56
+ indentation: '',
57
+ lines: [ 11, 13 ],
58
+ start: 156,
59
+ end: 214,
60
+ rawArgs: '',
61
+ rawContent: '/* comment inside */',
62
+ value: '/* GENERATED b */\n/* comment inside */\n/* END-GENERATED */'
63
+ }
64
+ },
65
+ {
66
+ index: 3,
67
+ type: 'c',
68
+ options: {},
69
+ context: { isMultiline: true },
70
+ open: { value: '/* GENERATED c */\n', start: 216, end: 234 },
71
+ content: {
72
+ value: '/* \n comment inside \n*/',
73
+ start: 234,
74
+ end: 258,
75
+ indentation: 0
76
+ },
77
+ close: { value: '\n/* END-GENERATED */', start: 258, end: 278 },
78
+ block: {
79
+ indentation: '',
80
+ lines: [ 15, 19 ],
81
+ start: 216,
82
+ end: 278,
83
+ rawArgs: '',
84
+ rawContent: '/* \n comment inside \n*/',
85
+ value: '/* GENERATED c */\n/* \n comment inside \n*/\n/* END-GENERATED */'
86
+ }
87
+ },
88
+ {
89
+ index: 4,
90
+ type: 'd',
91
+ options: {},
92
+ context: { isMultiline: true },
93
+ open: { value: '/* GENERATED d */\n', start: 280, end: 298 },
94
+ content: {
95
+ value: '/**\n * comment inside \n */',
96
+ start: 298,
97
+ end: 324,
98
+ indentation: 0
67
99
  },
68
- {
69
- index: 4,
70
- type: 'c',
71
- options: {},
72
- context: { isMultiline: true },
73
- open: { value: '/* GENERATED c */\n', start: 213, end: 230 },
74
- content: {
75
- value: '/* \n comment inside \n*/',
76
- start: 230,
77
- end: 256,
78
- indentation: 0
79
- },
80
- close: { value: '\n/* END-GENERATED */', start: 256, end: 275 },
81
- block: {
82
- indentation: '',
83
- lines: [ 15, 19 ],
84
- start: 213,
85
- end: 275,
86
- rawType: 'c',
87
- rawArgs: '',
88
- rawContent: '\n/* \n comment inside \n*/\n',
89
- value: '/* GENERATED c */\n/* \n comment inside \n*/\n/* END-GENERATED */'
90
- }
100
+ close: { value: '\n/* END-GENERATED */', start: 324, end: 344 },
101
+ block: {
102
+ indentation: '',
103
+ lines: [ 21, 25 ],
104
+ start: 280,
105
+ end: 344,
106
+ rawArgs: '',
107
+ rawContent: '/**\n * comment inside \n */',
108
+ value: '/* GENERATED d */\n/**\n * comment inside \n */\n/* END-GENERATED */'
109
+ }
110
+ },
111
+ {
112
+ index: 5,
113
+ type: 'e',
114
+ options: {},
115
+ context: { isMultiline: true },
116
+ open: { value: '/* GENERATED e */\n', start: 346, end: 364 },
117
+ content: {
118
+ value: '/****************\n comment inside \n******************/',
119
+ start: 364,
120
+ end: 418,
121
+ indentation: 0
91
122
  },
92
- {
93
- index: 5,
94
- type: 'd',
95
- options: {},
96
- context: { isMultiline: true },
97
- open: { value: '/* GENERATED d */\n', start: 277, end: 294 },
98
- content: {
99
- value: '/**\n * comment inside \n */',
100
- start: 294,
101
- end: 322,
102
- indentation: 0
103
- },
104
- close: { value: '\n/* END-GENERATED */', start: 322, end: 341 },
105
- block: {
106
- indentation: '',
107
- lines: [ 21, 25 ],
108
- start: 277,
109
- end: 341,
110
- rawType: 'd',
111
- rawArgs: '',
112
- rawContent: '\n/**\n * comment inside \n */\n',
113
- value: '/* GENERATED d */\n/**\n * comment inside \n */\n/* END-GENERATED */'
114
- }
123
+ close: { value: '\n/* END-GENERATED */', start: 418, end: 438 },
124
+ block: {
125
+ indentation: '',
126
+ lines: [ 27, 31 ],
127
+ start: 346,
128
+ end: 438,
129
+ rawArgs: '',
130
+ rawContent: '/****************\n comment inside \n******************/',
131
+ value: '/* GENERATED e */\n' +
132
+ '/****************\n' +
133
+ ' comment inside \n' +
134
+ '******************/\n' +
135
+ '/* END-GENERATED */'
136
+ }
137
+ },
138
+ {
139
+ index: 6,
140
+ type: 'MyCodeGen',
141
+ options: { yay: 'nice' },
142
+ context: { isMultiline: true },
143
+ open: {
144
+ value: "/* GENERATED MyCodeGen yay='nice' */\n",
145
+ start: 455,
146
+ end: 492
115
147
  },
116
- {
117
- index: 6,
118
- type: 'e',
119
- options: {},
120
- context: { isMultiline: true },
121
- open: { value: '/* GENERATED e */\n', start: 343, end: 360 },
122
- content: {
123
- value: '/****************\n comment inside \n******************/',
124
- start: 360,
125
- end: 416,
126
- indentation: 0
127
- },
128
- close: { value: '\n/* END-GENERATED */', start: 416, end: 435 },
129
- block: {
130
- indentation: '',
131
- lines: [ 27, 31 ],
132
- start: 343,
133
- end: 435,
134
- rawType: 'e',
135
- rawArgs: '',
136
- rawContent: '\n/****************\n comment inside \n******************/\n',
137
- value: '/* GENERATED e */\n' +
138
- '/****************\n' +
139
- ' comment inside \n' +
140
- '******************/\n' +
141
- '/* END-GENERATED */'
142
- }
148
+ content: {
149
+ value: "/* Awesome */\nconsole.log('noooo')",
150
+ start: 492,
151
+ end: 526,
152
+ indentation: 0
143
153
  },
144
- {
145
- index: 7,
146
- type: 'MyCodeGen',
147
- options: { yay: 'nice' },
148
- context: { isMultiline: true },
149
- open: {
150
- value: "/* GENERATED MyCodeGen yay='nice' */\n",
151
- start: 452,
152
- end: 488
153
- },
154
- content: {
155
- value: "/* Awesome */\nconsole.log('noooo')",
156
- start: 488,
157
- end: 524,
158
- indentation: 0
159
- },
160
- close: { value: '\n/* END-GENERATED */', start: 524, end: 543 },
161
- block: {
162
- indentation: '',
163
- lines: [ 35, 38 ],
164
- start: 452,
165
- end: 543,
166
- rawType: 'MyCodeGen',
167
- rawArgs: "yay='nice'",
168
- rawContent: "\n/* Awesome */\nconsole.log('noooo')\n",
169
- value: "/* GENERATED MyCodeGen yay='nice' */\n" +
170
- '/* Awesome */\n' +
171
- "console.log('noooo')\n" +
172
- '/* END-GENERATED */'
173
- }
154
+ close: { value: '\n/* END-GENERATED */', start: 526, end: 546 },
155
+ block: {
156
+ indentation: '',
157
+ lines: [ 35, 38 ],
158
+ start: 455,
159
+ end: 546,
160
+ rawArgs: "yay='nice'",
161
+ rawContent: "/* Awesome */\nconsole.log('noooo')",
162
+ value: "/* GENERATED MyCodeGen yay='nice' */\n" +
163
+ '/* Awesome */\n' +
164
+ "console.log('noooo')\n" +
165
+ '/* END-GENERATED */'
174
166
  }
175
- ]
176
- })
167
+ }
168
+ ])
177
169
  })
178
170
 
179
171
  test.run()