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
@@ -0,0 +1,409 @@
1
+ const { test } = require('uvu')
2
+ const assert = require('uvu/assert')
3
+ const { deepLog } = require('./utils/logs')
4
+ const { getGlobGroupsFromArgs, runCli } = require('./cli')
5
+
6
+ const DEBUG = true
7
+ const logger = (DEBUG) ? console.log : () => {}
8
+ const deepLogger = (DEBUG) ? deepLog : () => {}
9
+
10
+ function logInput(rawArgs) {
11
+ logger('\n───────────────────────')
12
+ logger('Input:')
13
+ logger(rawArgs.join(" "))
14
+ logger('───────────────────────\n')
15
+ }
16
+
17
+ test('Exports API', () => {
18
+ assert.equal(typeof runCli, 'function', 'undefined val')
19
+ assert.equal(typeof getGlobGroupsFromArgs, 'function', 'undefined val')
20
+ })
21
+
22
+ const longArgs = [
23
+ 'test/fixtures/md/basic.md',
24
+ 'test/fixtures/md/error-missing-transforms-two.md',
25
+ 'test/fixtures/md/error-missing-transforms.md',
26
+ 'test/fixtures/md/error-no-block-transform-defined.md',
27
+ 'test/fixtures/md/error-unbalanced.md',
28
+ 'test/fixtures/md/format-inline.md',
29
+ 'test/fixtures/md/format-with-wacky-indentation.md',
30
+ 'test/fixtures/md/inline-two.md',
31
+ 'test/fixtures/md/inline.md',
32
+ 'test/fixtures/md/missing-transform.md',
33
+ 'test/fixtures/md/mixed.md',
34
+ 'test/fixtures/md/no-transforms.md',
35
+ 'test/fixtures/md/string.md',
36
+ 'test/fixtures/md/syntax-legacy-colon.md',
37
+ 'test/fixtures/md/syntax-legacy-query.md',
38
+ 'test/fixtures/md/transform-code.md',
39
+ 'test/fixtures/md/transform-custom.md',
40
+ 'test/fixtures/md/transform-file.md',
41
+ 'test/fixtures/md/transform-remote.md',
42
+ 'test/fixtures/md/transform-toc.md',
43
+ 'test/fixtures/md/transform-wordCount.md',
44
+ 'debug',
45
+ '--transform',
46
+ 'test/fixtures/md/transform-code.md',
47
+ 'test/fixtures/md/transform-custom.md',
48
+ 'test/fixtures/md/transform-file.md',
49
+ 'test/fixtures/md/transform-remote.md',
50
+ 'test/fixtures/md/transform-toc.md',
51
+ 'test/fixtures/md/transform-wordCount.md',
52
+ '1233535235',
53
+ '=',
54
+ 'hahah',
55
+ 'funky=hi',
56
+ '--ignore',
57
+ 'test/fixtures/output/basic.md',
58
+ 'test/fixtures/output/block-no-transform.md',
59
+ 'test/fixtures/output/error-missing-transforms-two.md',
60
+ 'test/fixtures/output/error-missing-transforms.md',
61
+ 'test/fixtures/output/fixture-code.md',
62
+ 'test/fixtures/output/format-inline.md',
63
+ 'test/fixtures/output/format-with-wacky-indentation.md',
64
+ 'test/fixtures/output/go-simple.md',
65
+ 'test/fixtures/output/inline-two.md',
66
+ 'test/fixtures/output/inline.md',
67
+ 'test/fixtures/output/missing-transform.md',
68
+ 'test/fixtures/output/mixed.md',
69
+ 'test/fixtures/output/params.md',
70
+ 'test/fixtures/output/remote.md',
71
+ 'test/fixtures/output/toc.md',
72
+ 'test/fixtures/output/transform-code.md',
73
+ 'test/fixtures/output/transform-custom.md',
74
+ 'test/fixtures/output/transform-file.md',
75
+ 'test/fixtures/output/transform-remote.md',
76
+ 'test/fixtures/output/transform-toc.md',
77
+ 'test/fixtures/output/transform-wordCount.md',
78
+ 'test/fixtures/output/with-wacky-indentation.md',
79
+ '--lol',
80
+ '--whatever',
81
+ 'test/fixtures/md/syntax-legacy-colon.md',
82
+ 'test/fixtures/md/syntax-legacy-query.md',
83
+ '--foo=bar',
84
+ '--fun',
85
+ 'lol.md',
86
+ 'what=no.md',
87
+ 'x',
88
+ 'xno.md',
89
+ 'what'
90
+ ]
91
+
92
+ test('getGlobGroupsFromArgs returns globs', () => {
93
+ /* CLI command with list of files already passed in
94
+ node ./cli.js test/fixtures/md/**.md debug --transform test/fixtures/md/transform-**.md 1233535235 = hahah funky=hi --ignore test/fixtures/output/**.md --lol --whatever test/fixtures/md/syntax-**.md --foo=bar --fun lol.md what=no.md x 'xno.md' what
95
+ */
96
+ const globData = getGlobGroupsFromArgs(longArgs)
97
+ //*
98
+ logInput(longArgs)
99
+ deepLogger(globData)
100
+ /** */
101
+ assert.equal(globData.globGroups, [
102
+ {
103
+ key: '',
104
+ raw: '',
105
+ values: [
106
+ 'test/fixtures/md/basic.md',
107
+ 'test/fixtures/md/error-missing-transforms-two.md',
108
+ 'test/fixtures/md/error-missing-transforms.md',
109
+ 'test/fixtures/md/error-no-block-transform-defined.md',
110
+ 'test/fixtures/md/error-unbalanced.md',
111
+ 'test/fixtures/md/format-inline.md',
112
+ 'test/fixtures/md/format-with-wacky-indentation.md',
113
+ 'test/fixtures/md/inline-two.md',
114
+ 'test/fixtures/md/inline.md',
115
+ 'test/fixtures/md/missing-transform.md',
116
+ 'test/fixtures/md/mixed.md',
117
+ 'test/fixtures/md/no-transforms.md',
118
+ 'test/fixtures/md/string.md',
119
+ 'test/fixtures/md/syntax-legacy-colon.md',
120
+ 'test/fixtures/md/syntax-legacy-query.md',
121
+ 'test/fixtures/md/transform-code.md',
122
+ 'test/fixtures/md/transform-custom.md',
123
+ 'test/fixtures/md/transform-file.md',
124
+ 'test/fixtures/md/transform-remote.md',
125
+ 'test/fixtures/md/transform-toc.md',
126
+ 'test/fixtures/md/transform-wordCount.md'
127
+ ]
128
+ },
129
+ {
130
+ key: 'transform',
131
+ raw: '--transform',
132
+ values: [
133
+ 'test/fixtures/md/transform-code.md',
134
+ 'test/fixtures/md/transform-custom.md',
135
+ 'test/fixtures/md/transform-file.md',
136
+ 'test/fixtures/md/transform-remote.md',
137
+ 'test/fixtures/md/transform-toc.md',
138
+ 'test/fixtures/md/transform-wordCount.md'
139
+ ]
140
+ },
141
+ {
142
+ key: 'ignore',
143
+ raw: '--ignore',
144
+ values: [
145
+ 'test/fixtures/output/basic.md',
146
+ 'test/fixtures/output/block-no-transform.md',
147
+ 'test/fixtures/output/error-missing-transforms-two.md',
148
+ 'test/fixtures/output/error-missing-transforms.md',
149
+ 'test/fixtures/output/fixture-code.md',
150
+ 'test/fixtures/output/format-inline.md',
151
+ 'test/fixtures/output/format-with-wacky-indentation.md',
152
+ 'test/fixtures/output/go-simple.md',
153
+ 'test/fixtures/output/inline-two.md',
154
+ 'test/fixtures/output/inline.md',
155
+ 'test/fixtures/output/missing-transform.md',
156
+ 'test/fixtures/output/mixed.md',
157
+ 'test/fixtures/output/params.md',
158
+ 'test/fixtures/output/remote.md',
159
+ 'test/fixtures/output/toc.md',
160
+ 'test/fixtures/output/transform-code.md',
161
+ 'test/fixtures/output/transform-custom.md',
162
+ 'test/fixtures/output/transform-file.md',
163
+ 'test/fixtures/output/transform-remote.md',
164
+ 'test/fixtures/output/transform-toc.md',
165
+ 'test/fixtures/output/transform-wordCount.md',
166
+ 'test/fixtures/output/with-wacky-indentation.md'
167
+ ]
168
+ },
169
+ {
170
+ key: 'whatever',
171
+ raw: '--whatever',
172
+ values: [
173
+ 'test/fixtures/md/syntax-legacy-colon.md',
174
+ 'test/fixtures/md/syntax-legacy-query.md'
175
+ ]
176
+ },
177
+ { key: 'fun', raw: '--fun', values: [ 'lol.md' ] }
178
+ ])
179
+ })
180
+
181
+ test('test 2', () => {
182
+ /* CLI command with list of files already passed in
183
+ node ./cli.js --file '**.md' 'funky**.md' billy --foo 'bar*123'
184
+ */
185
+ const rawArgv = [ '--file', '**.md', 'funky**.md', 'billy', '--foo', 'bar*123' ]
186
+
187
+ logger(rawArgv.join(" "))
188
+
189
+ const globData = getGlobGroupsFromArgs(rawArgv, {
190
+ globKeys: ['file']
191
+ })
192
+ //*
193
+ deepLogger(globData)
194
+ /** */
195
+ assert.equal(globData, {
196
+ globGroups: [ { key: 'file', raw: '--file', values: [ '**.md', 'funky**.md' ] } ],
197
+ otherOpts: [ 'billy', '--foo', 'bar*123' ]
198
+ })
199
+ })
200
+
201
+ test('Handles multiple string values and groups them', () => {
202
+ /* CLI command with list of files already passed in
203
+ node ./cli.js 'test/fixtures/md/bar.md' 'test/fixtures/output/foo.md'
204
+ */
205
+ const rawArgv = [ 'test/fixtures/md/bar.md', 'test/fixtures/output/foo.md' ]
206
+
207
+ const globData = getGlobGroupsFromArgs(rawArgv)
208
+ //*
209
+ logInput(rawArgv)
210
+ deepLogger(globData)
211
+ /** */
212
+ assert.equal(globData, {
213
+ globGroups: [
214
+ {
215
+ key: '',
216
+ raw: '',
217
+ values: [ 'test/fixtures/md/bar.md', 'test/fixtures/output/foo.md' ]
218
+ }
219
+ ],
220
+ otherOpts: []
221
+ })
222
+ })
223
+
224
+ test('Handles multiple string GLOB values and groups them', () => {
225
+ /* CLI command with list of files already passed in
226
+ node ./cli.js 'test/fixtures/md/**.md' 'test/fixtures/output/**.md'
227
+ */
228
+ const rawArgv = [ 'test/fixtures/md/**.md', 'test/fixtures/output/**.md' ]
229
+
230
+ const globData = getGlobGroupsFromArgs(rawArgv)
231
+ //*
232
+ logInput(rawArgv)
233
+ deepLogger(globData)
234
+ /** */
235
+ assert.equal(globData, {
236
+ globGroups: [
237
+ {
238
+ key: '',
239
+ raw: '',
240
+ values: [ 'test/fixtures/md/**.md', 'test/fixtures/output/**.md' ]
241
+ }
242
+ ],
243
+ otherOpts: []
244
+ })
245
+ })
246
+
247
+
248
+ test('Handles globKey set with multiple file/glob like values supplied afterwards', () => {
249
+ /* CLI command with list of files already passed in
250
+ node ./cli.js --file 'foobar.md' "funktown/bar.md" '**.md' 'funky**.md' billy --foo 'bar*123'
251
+ */
252
+ const rawArgv = [
253
+ '--file',
254
+ 'foobar.md',
255
+ 'funktown/bar.md',
256
+ '**.md',
257
+ 'funky**.md',
258
+ 'billy',
259
+ '--foo',
260
+ 'bar*123'
261
+ ]
262
+
263
+ logger(rawArgv.join(" "))
264
+
265
+ const globData = getGlobGroupsFromArgs(rawArgv, {
266
+ globKeys: ['file']
267
+ })
268
+ //*
269
+ logInput(rawArgv)
270
+ deepLogger(globData)
271
+ /** */
272
+ assert.equal(globData, {
273
+ globGroups: [ { key: 'file', raw: '--file', values: [
274
+ 'foobar.md',
275
+ 'funktown/bar.md',
276
+ '**.md',
277
+ 'funky**.md'
278
+ ]
279
+ }
280
+ ],
281
+ otherOpts: [ 'billy', '--foo', 'bar*123' ]
282
+ })
283
+ })
284
+
285
+ test('Handles mixed strings, glob strings, array strings, and regex strings', () => {
286
+ /* CLI command with list of files already passed in
287
+ node ./cli.js --file 'test/fixtures/md/**.md' 'test/fixtures/output/**.md' '[foo.md, bar.whatever.md, /patter.md/]'
288
+ */
289
+ const rawArgv = [
290
+ '--file',
291
+ 'funktown/bar.md',
292
+ 'test/fixtures/md/**.md',
293
+ 'test/fixtures/output/**.md',
294
+ '[foo.md, bar.whatever.md, /patter.md/]',
295
+ '/funk.md/'
296
+ ]
297
+
298
+ logger(rawArgv.join(" "))
299
+
300
+ const globData = getGlobGroupsFromArgs(rawArgv, {
301
+ globKeys: ['file']
302
+ })
303
+ //*
304
+ logInput(rawArgv)
305
+ deepLogger(globData)
306
+ /** */
307
+ assert.equal(globData, {
308
+ globGroups: [
309
+ {
310
+ key: 'file',
311
+ raw: '--file',
312
+ values: [
313
+ 'funktown/bar.md',
314
+ 'test/fixtures/md/**.md',
315
+ 'test/fixtures/output/**.md',
316
+ 'foo.md',
317
+ 'bar.whatever.md',
318
+ /patter\.md/,
319
+ /funk\.md/
320
+ ]
321
+ }
322
+ ],
323
+ otherOpts: []
324
+ })
325
+ })
326
+
327
+ test('Handles string arrays if globKeys set', () => {
328
+ /* CLI command with list of files already passed in
329
+ node ./cli.js --file '[**.md, /xyz**.md/]' 'funky**.md' billy --foo 'bar*123' --fuzz test/fixtures/output/**.md
330
+ */
331
+ const rawArgv = [
332
+ '--file',
333
+ '[**.md, /xyz**.md/]',
334
+ 'funky**.md',
335
+ 'billy',
336
+ '--foo',
337
+ 'bar*123',
338
+ '--fuzz',
339
+ 'test/fixtures/output/basic.md',
340
+ 'test/fixtures/output/block-no-transform.md',
341
+ 'test/fixtures/output/error-missing-transforms-two.md',
342
+ 'test/fixtures/output/error-missing-transforms.md',
343
+ 'test/fixtures/output/fixture-code.md',
344
+ 'test/fixtures/output/format-inline.md',
345
+ 'test/fixtures/output/format-with-wacky-indentation.md',
346
+ 'test/fixtures/output/go-simple.md',
347
+ 'test/fixtures/output/inline-two.md',
348
+ 'test/fixtures/output/inline.md',
349
+ 'test/fixtures/output/missing-transform.md',
350
+ 'test/fixtures/output/mixed.md',
351
+ 'test/fixtures/output/params.md',
352
+ 'test/fixtures/output/remote.md',
353
+ 'test/fixtures/output/toc.md',
354
+ 'test/fixtures/output/transform-code.md',
355
+ 'test/fixtures/output/transform-custom.md',
356
+ 'test/fixtures/output/transform-file.md',
357
+ 'test/fixtures/output/transform-remote.md',
358
+ 'test/fixtures/output/transform-toc.md',
359
+ 'test/fixtures/output/transform-wordCount.md',
360
+ 'test/fixtures/output/with-wacky-indentation.md'
361
+ ]
362
+ const globData = getGlobGroupsFromArgs(rawArgv, {
363
+ globKeys: ['file']
364
+ })
365
+ //*
366
+ logInput(rawArgv)
367
+ deepLogger(globData)
368
+ /** */
369
+ assert.equal(globData, {
370
+ globGroups: [
371
+ {
372
+ key: 'file',
373
+ raw: '--file',
374
+ values: [ '**.md', /xyz\*\*\.md/, 'funky**.md' ]
375
+ },
376
+ {
377
+ key: 'fuzz',
378
+ raw: '--fuzz',
379
+ values: [
380
+ 'test/fixtures/output/basic.md',
381
+ 'test/fixtures/output/block-no-transform.md',
382
+ 'test/fixtures/output/error-missing-transforms-two.md',
383
+ 'test/fixtures/output/error-missing-transforms.md',
384
+ 'test/fixtures/output/fixture-code.md',
385
+ 'test/fixtures/output/format-inline.md',
386
+ 'test/fixtures/output/format-with-wacky-indentation.md',
387
+ 'test/fixtures/output/go-simple.md',
388
+ 'test/fixtures/output/inline-two.md',
389
+ 'test/fixtures/output/inline.md',
390
+ 'test/fixtures/output/missing-transform.md',
391
+ 'test/fixtures/output/mixed.md',
392
+ 'test/fixtures/output/params.md',
393
+ 'test/fixtures/output/remote.md',
394
+ 'test/fixtures/output/toc.md',
395
+ 'test/fixtures/output/transform-code.md',
396
+ 'test/fixtures/output/transform-custom.md',
397
+ 'test/fixtures/output/transform-file.md',
398
+ 'test/fixtures/output/transform-remote.md',
399
+ 'test/fixtures/output/transform-toc.md',
400
+ 'test/fixtures/output/transform-wordCount.md',
401
+ 'test/fixtures/output/with-wacky-indentation.md'
402
+ ]
403
+ }
404
+ ],
405
+ otherOpts: [ 'billy', '--foo', 'bar*123' ]
406
+ })
407
+ })
408
+
409
+ test.run()
@@ -0,0 +1,12 @@
1
+
2
+ const SYNTAX = 'md'
3
+ const OPEN_WORD = 'doc-gen'
4
+ const CLOSE_WORD = 'end-doc-gen'
5
+ const DEFAULT_GLOB_PATTERN = '**/**.md'
6
+
7
+ module.exports = {
8
+ SYNTAX,
9
+ OPEN_WORD,
10
+ CLOSE_WORD,
11
+ DEFAULT_GLOB_PATTERN
12
+ }
@@ -0,0 +1,66 @@
1
+ export {}
2
+
3
+ declare global {
4
+ const BUILD_VERSION: string;
5
+ interface TestGlob {
6
+ greeting: string;
7
+ duration?: number;
8
+ color?: string;
9
+ }
10
+ }
11
+
12
+
13
+
14
+ export type TinyProps = {
15
+ message: string;
16
+ count: number;
17
+ disabled: boolean;
18
+ };
19
+
20
+ declare var Age: number;
21
+
22
+ declare function greetTwo(greeting: string): void;
23
+
24
+ interface GreetingSettings {
25
+ greeting: string;
26
+ duration?: number;
27
+ color?: string;
28
+ }
29
+ declare function greet(setting: GreetingSettings): void;
30
+
31
+ export namespace testFnTypes {
32
+ type input = boolean | Function;
33
+ }
34
+
35
+ // Use namespaces to organize types.
36
+ // Ref like GreetingLib.LogOptions
37
+ export declare namespace GreetingLib {
38
+ interface LogOptions {
39
+ verbose?: boolean;
40
+ }
41
+ interface AlertOptions {
42
+ modal: boolean;
43
+ title?: string;
44
+ color?: string;
45
+ }
46
+ }
47
+
48
+ export declare namespace Xyz.Options {
49
+ // Refer to via Xyz.Options.Log
50
+ interface Log {
51
+ verbose?: boolean;
52
+ }
53
+ interface Alert {
54
+ modal: boolean;
55
+ title?: string;
56
+ color?: string;
57
+ }
58
+ }
59
+
60
+ export namespace Dotted {
61
+ type Name = number;
62
+ }
63
+
64
+ export declare class A {
65
+ static foo(): void;
66
+ }