markdown-magic 2.6.1 → 3.0.1

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 (58) hide show
  1. package/README.md +47 -37
  2. package/cli.js +5 -82
  3. package/lib/block-parser-js.test.js +171 -0
  4. package/lib/block-parser.js +382 -0
  5. package/lib/block-parser.test.js +479 -0
  6. package/lib/cli.js +245 -0
  7. package/lib/cli.test.js +409 -0
  8. package/lib/defaults.js +12 -0
  9. package/lib/globals.d.ts +66 -0
  10. package/lib/index.js +353 -184
  11. package/lib/index.test.js +11 -0
  12. package/lib/process-contents.js +371 -0
  13. package/lib/process-file.js +37 -0
  14. package/lib/transforms/code.js +67 -28
  15. package/lib/transforms/file.js +17 -17
  16. package/lib/transforms/index.js +0 -114
  17. package/lib/transforms/remote.js +8 -6
  18. package/lib/transforms/sectionToc.js +18 -0
  19. package/lib/transforms/toc.js +12 -265
  20. package/lib/transforms/wordCount.js +5 -0
  21. package/lib/types.js +11 -0
  22. package/lib/utils/fs.js +342 -0
  23. package/lib/utils/fs.test.js +267 -0
  24. package/lib/utils/index.js +19 -0
  25. package/{cli-utils.js → lib/utils/load-config.js} +2 -6
  26. package/lib/utils/logs.js +94 -0
  27. package/lib/utils/md/filters.js +20 -0
  28. package/lib/utils/md/find-code-blocks.js +88 -0
  29. package/lib/utils/md/find-date.js +32 -0
  30. package/lib/utils/md/find-frontmatter.js +92 -0
  31. package/lib/utils/md/find-frontmatter.test.js +17 -0
  32. package/lib/utils/md/find-html-tags.js +105 -0
  33. package/lib/utils/md/find-images-md.js +27 -0
  34. package/lib/utils/md/find-images.js +107 -0
  35. package/lib/utils/md/find-links.js +220 -0
  36. package/lib/utils/md/find-unmatched-html-tags.js +32 -0
  37. package/lib/utils/md/fixtures/2022-01-22-date-in-filename.md +14 -0
  38. package/lib/utils/md/fixtures/file-with-frontmatter.md +32 -0
  39. package/lib/utils/md/fixtures/file-with-links.md +153 -0
  40. package/lib/utils/md/md.test.js +105 -0
  41. package/lib/utils/md/parse.js +146 -0
  42. package/lib/utils/md/utils.js +19 -0
  43. package/lib/utils/regex-timeout.js +84 -0
  44. package/lib/utils/regex.js +40 -6
  45. package/lib/utils/remoteRequest.js +55 -0
  46. package/lib/utils/syntax.js +82 -0
  47. package/lib/utils/text.js +328 -0
  48. package/lib/utils/text.test.js +305 -0
  49. package/lib/utils/toc.js +315 -0
  50. package/package.json +30 -26
  51. package/index.js +0 -46
  52. package/lib/processFile.js +0 -154
  53. package/lib/updateContents.js +0 -125
  54. package/lib/utils/_md.test.js +0 -63
  55. package/lib/utils/new-parser.js +0 -412
  56. package/lib/utils/new-parser.test.js +0 -324
  57. package/lib/utils/weird-parse.js +0 -230
  58. package/lib/utils/weird-parse.test.js +0 -217
package/README.md CHANGED
@@ -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 -->
@@ -54,8 +55,8 @@ npm install markdown-magic --save-dev
54
55
  ```
55
56
 
56
57
  ## Usage
57
- <!-- ⛔️ MD-MAGIC-EXAMPLE:START (CODE:src=./examples/basic-usage.js) -->
58
- <!-- The below code snippet is automatically added from ./examples/basic-usage.js -->
58
+ <!-- ⛔️ MD-MAGIC-EXAMPLE:START (CODE:src=./examples/1-_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
- <!-- ⛔️ MD-MAGIC-EXAMPLE:START (RENDERDOCS:path=./index.js)
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
  ```
@@ -139,12 +142,12 @@ module.exports = {
139
142
 
140
143
  ## Transforms
141
144
 
142
- 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.
145
+ 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.
143
146
 
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`
@@ -255,9 +258,9 @@ The face symbol 👉 <!-- MD-MAGIC-EXAMPLE:START (INLINE_EXAMPLE) -->**⊂◉‿
255
258
  * [jsdoc](https://github.com/bradtaylorsf/markdown-magic-jsdoc) - Adds jsdoc comment support
256
259
  * [build-badge](https://github.com/rishichawda/markdown-magic-build-badge) - Update branch badges to auto-magically point to current branches.
257
260
  * [package-json](https://github.com/forresst/markdown-magic-package-json) - Add the package.json properties to markdown files
258
- * [local-image](https://github.com/stevenbenisek/markdown-magic-local-image)
259
261
  * [figlet](https://github.com/lafourchette/markdown-magic-figlet) - Add FIGfont text to markdown files
260
262
  * [local-image](https://github.com/stevenbenisek/markdown-magic-local-image) - plugin to add local images to markdown
263
+ * [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
261
264
 
262
265
  ## Adding Custom Transforms
263
266
 
@@ -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
@@ -346,9 +361,8 @@ module.exports = function customPlugin(pluginOptions) {
346
361
  <!-- ⛔️ MD-MAGIC-EXAMPLE:END -->
347
362
 
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
- <!-- ⛔️ MD-MAGIC-EXAMPLE:START (pluginExample) DO not edit ⛔️ -->
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
@@ -376,10 +390,6 @@ This will replace all the contents of inside the comment DUDE
376
390
 
377
391
  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.
378
392
 
379
- <!-- MD-MAGIC-EXAMPLE:START (LOLZ)-->
380
- This section was generated by the cli config markdown.config.js file
381
- <!-- MD-MAGIC-EXAMPLE:END (LOLZ)-->
382
-
383
393
  ## License
384
394
 
385
395
  [MIT][mit] © [DavidWells][author]
package/cli.js CHANGED
@@ -1,84 +1,7 @@
1
- #!/usr/bin/env node
1
+ const mri = require('mri')
2
+ const { runCli } = require('./lib/cli')
3
+ const argv = process.argv.slice(2)
4
+ const cliArgs = mri(argv)
2
5
 
3
- const program = require('commander')
4
- const pkg = require('./package.json')
5
- const markdownMagic = require('./index')
6
- const findUp = require('find-up')
7
- const defaultFileName = 'README.md'
8
- const defaultConfigPath = './markdown.config.js'
9
- const loadConfig = require('./cli-utils').loadConfig
6
+ runCli(cliArgs)
10
7
 
11
- var filePaths = defaultFileName
12
- var callbackFunction = defaultCallback // eslint-disable-line
13
- var ignorePath // eslint-disable-line
14
-
15
- // start commander.js
16
- program
17
- .version(pkg.version)
18
- .option('-p, --path [path]', `Define path to markdown (single path or glob). Default ${defaultFileName}`, parsePaths, defaultFileName)
19
- .option('-i, --ignore [path]', '(Optional) Define path to ignore', parseIgnorePaths, defaultConfigPath)
20
- .option('-c, --config [path]', '(Optional) Define config file path. If you have custom transforms or a callback you will want to specify this option', null, defaultConfigPath)
21
- // .option('-cb, --callback [path]', 'Define path', parsePaths, defaultFileName)
22
- .parse(process.argv)
23
-
24
- const configFile = getConfigFilepath()
25
- const foundConfig = (configFile) ? loadConfig(configFile) : false
26
-
27
- if (foundConfig && foundConfig.callback) {
28
- callbackFunction = foundConfig.callback
29
- }
30
- if (!foundConfig) {
31
- // console.log('No markdown magic config set using {empty object}')
32
- }
33
- const configuration = foundConfig || {}
34
-
35
- function parsePaths(path, defaultValue) {
36
- if (path) {
37
- filePaths = path
38
- }
39
- }
40
-
41
- function parseIgnorePaths(path, defaultValue) {
42
- if (path) {
43
- ignorePath = path.split(',').map((p) => {
44
- const fp = p.trim()
45
- if (fp.match(/\bnode_modules\b/)) {
46
- // exact node_module match. Ignore entire DIR
47
- return '!node_modules/**'
48
- }
49
- if (!fp.match(/^!/)) {
50
- return `!${fp}`
51
- }
52
- return fp
53
- })
54
- }
55
- }
56
-
57
- // process default
58
- if (program.path) {
59
- filePaths = program.path
60
- }
61
-
62
- if (ignorePath) {
63
- // console.log('ignore path', ignorePath)
64
- filePaths = [filePaths].concat(ignorePath)
65
- }
66
-
67
- // console.log('filePaths', filePaths)
68
- // console.log('configuration', configuration)
69
- // console.log('callbackFunction', callbackFunction)
70
- console.log('Starting markdown-magic', filePaths)
71
- markdownMagic(filePaths, configuration, callbackFunction)
72
-
73
- function defaultCallback(err, msg) {
74
- if (err) {
75
- console.log('Error:', err)
76
- }
77
- if (msg) {
78
- console.log('Files processed. markdown-magic Finished! ⊂◉‿◉つ')
79
- }
80
- }
81
-
82
- function getConfigFilepath() {
83
- return program.config || findUp.sync(defaultConfigPath) || findUp.sync('md-magic.config.js')
84
- }
@@ -0,0 +1,171 @@
1
+ const path = require('path')
2
+ const fs = require('fs').promises
3
+ const { test } = require('uvu')
4
+ const assert = require('uvu/assert')
5
+ const { parseBlocks } = require('./block-parser')
6
+ const { deepLog } = require('./utils/logs')
7
+
8
+ test('JS file parse', async () => {
9
+ const contents = await fs.readFile(path.join(__dirname, '../test/fixtures/js/simple.js'), 'utf-8')
10
+ const blocks = parseBlocks(contents, {
11
+ syntax: 'js',
12
+ open: 'GENERATED',
13
+ close: 'END-GENERATED',
14
+ })
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
30
+ },
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
99
+ },
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
122
+ },
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
147
+ },
148
+ content: {
149
+ value: "/* Awesome */\nconsole.log('noooo')",
150
+ start: 492,
151
+ end: 526,
152
+ indentation: 0
153
+ },
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 */'
166
+ }
167
+ }
168
+ ])
169
+ })
170
+
171
+ test.run()