markdown-magic 2.6.0 → 3.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.
Files changed (61) hide show
  1. package/README.md +6 -10
  2. package/cli.js +5 -82
  3. package/lib/block-parser-js.test.js +179 -0
  4. package/lib/block-parser.js +389 -0
  5. package/lib/{utils/new-parser.test.js → block-parser.test.js} +168 -50
  6. package/lib/cli.js +234 -0
  7. package/lib/cli.test.js +409 -0
  8. package/lib/defaults.js +12 -0
  9. package/lib/index.js +319 -184
  10. package/lib/index.test.js +11 -0
  11. package/lib/options-parser.js +498 -0
  12. package/lib/options-parser.test.js +1237 -0
  13. package/lib/process-contents.js +330 -0
  14. package/lib/process-file.js +34 -0
  15. package/lib/transforms/code.js +67 -22
  16. package/lib/transforms/file.js +13 -10
  17. package/lib/transforms/remote.js +9 -6
  18. package/lib/transforms/toc.js +136 -64
  19. package/lib/transforms/wordCount.js +5 -0
  20. package/lib/utils/fs.js +340 -0
  21. package/lib/utils/fs.test.js +268 -0
  22. package/lib/utils/html-to-json/compat.js +42 -0
  23. package/lib/utils/html-to-json/format.js +64 -0
  24. package/lib/utils/html-to-json/index.js +37 -0
  25. package/lib/utils/html-to-json/lexer.js +345 -0
  26. package/lib/utils/html-to-json/parser.js +146 -0
  27. package/lib/utils/html-to-json/stringify.js +37 -0
  28. package/lib/utils/html-to-json/tags.js +171 -0
  29. package/lib/utils/index.js +19 -0
  30. package/{cli-utils.js → lib/utils/load-config.js} +2 -6
  31. package/lib/utils/logs.js +89 -0
  32. package/lib/utils/md/filters.js +20 -0
  33. package/lib/utils/md/find-code-blocks.js +80 -0
  34. package/lib/utils/md/find-date.js +32 -0
  35. package/lib/utils/md/find-frontmatter.js +94 -0
  36. package/lib/utils/md/find-frontmatter.test.js +17 -0
  37. package/lib/utils/md/find-html-tags.js +105 -0
  38. package/lib/utils/md/find-images.js +102 -0
  39. package/lib/utils/md/find-links.js +202 -0
  40. package/lib/utils/md/find-unmatched-html-tags.js +33 -0
  41. package/lib/utils/md/fixtures/2022-01-22-date-in-filename.md +14 -0
  42. package/lib/utils/md/fixtures/file-with-frontmatter.md +32 -0
  43. package/lib/utils/md/fixtures/file-with-links.md +143 -0
  44. package/lib/utils/md/md.test.js +37 -0
  45. package/lib/utils/md/parse.js +122 -0
  46. package/lib/utils/md/utils.js +19 -0
  47. package/lib/utils/regex-timeout.js +83 -0
  48. package/lib/utils/regex.js +38 -5
  49. package/lib/utils/remoteRequest.js +54 -0
  50. package/lib/utils/syntax.js +79 -0
  51. package/lib/utils/text.js +260 -0
  52. package/lib/utils/text.test.js +311 -0
  53. package/package.json +26 -26
  54. package/index.js +0 -46
  55. package/lib/processFile.js +0 -154
  56. package/lib/transforms/index.js +0 -114
  57. package/lib/updateContents.js +0 -125
  58. package/lib/utils/_md.test.js +0 -63
  59. package/lib/utils/new-parser.js +0 -412
  60. package/lib/utils/weird-parse.js +0 -230
  61. package/lib/utils/weird-parse.test.js +0 -217
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 expand</summary>
24
+ <summary>Click to expaxnd</summary>
25
25
 
26
26
  - [About](#about)
27
27
  - [Install](#install)
@@ -54,7 +54,7 @@ npm install markdown-magic --save-dev
54
54
  ```
55
55
 
56
56
  ## Usage
57
- <!-- ⛔️ MD-MAGIC-EXAMPLE:START (CODE:src=./examples/basic-usage.js) -->
57
+ <!-- ⛔️ MD-MAGIC-EXAMPLE:START (CODE:src=./examples/1-_basic-usage.js) -->
58
58
  <!-- The below code snippet is automatically added from ./examples/basic-usage.js -->
59
59
  ```js
60
60
  import path from 'path'
@@ -66,7 +66,7 @@ markdownMagic(markdownPath)
66
66
  <!-- ⛔️ MD-MAGIC-EXAMPLE:END *-->
67
67
 
68
68
 
69
- <!-- ⛔️ MD-MAGIC-EXAMPLE:START (RENDERDOCS:path=./index.js)
69
+ <!-- ⛔️ MD-MAGIC-EXAMPLE:START (RENDERDOCS:path=./lib/index.js)
70
70
  - Do not remove or modify this section -->
71
71
  ### API
72
72
  ```js
@@ -139,7 +139,7 @@ module.exports = {
139
139
 
140
140
  ## Transforms
141
141
 
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.
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.
143
143
 
144
144
  <!-- ⛔️ MD-MAGIC-EXAMPLE:START (RENDERDOCS:path=./lib/transforms/index.js) - Do not remove or modify this section -->
145
145
  ### > TOC
@@ -255,9 +255,9 @@ The face symbol 👉 <!-- MD-MAGIC-EXAMPLE:START (INLINE_EXAMPLE) -->**⊂◉‿
255
255
  * [jsdoc](https://github.com/bradtaylorsf/markdown-magic-jsdoc) - Adds jsdoc comment support
256
256
  * [build-badge](https://github.com/rishichawda/markdown-magic-build-badge) - Update branch badges to auto-magically point to current branches.
257
257
  * [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
258
  * [figlet](https://github.com/lafourchette/markdown-magic-figlet) - Add FIGfont text to markdown files
260
259
  * [local-image](https://github.com/stevenbenisek/markdown-magic-local-image) - plugin to add local images to markdown
260
+ * [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
261
 
262
262
  ## Adding Custom Transforms
263
263
 
@@ -346,7 +346,7 @@ module.exports = function customPlugin(pluginOptions) {
346
346
  <!-- ⛔️ MD-MAGIC-EXAMPLE:END -->
347
347
 
348
348
  [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 ⛔️ -->
349
+ <!-- ⛔️ MD-MAGIC-EXAMPLE:START (pluginExample) ⛔️ -->
350
350
  This content is altered by the `pluginExample` plugin registered in `examples/generate-readme.js`
351
351
 
352
352
  <!-- ⛔️ MD-MAGIC-EXAMPLE:END -->
@@ -376,10 +376,6 @@ This will replace all the contents of inside the comment DUDE
376
376
 
377
377
  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
378
 
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
379
  ## License
384
380
 
385
381
  [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,179 @@
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
+ // 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
+ }
44
+ },
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
+ }
67
+ },
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
+ }
91
+ },
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
+ }
115
+ },
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
+ }
143
+ },
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
+ }
174
+ }
175
+ ]
176
+ })
177
+ })
178
+
179
+ test.run()