markdown-magic 3.3.0 → 3.3.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.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Markdown Magic [![npm-version][npm-badge]][npm-link]
2
2
 
3
- ✨ Add a little magic to your markdown ✨
3
+ ✨ Add a little magic to your markdown!
4
4
 
5
5
  ## About
6
6
 
@@ -305,10 +305,10 @@ This content will be dynamically replaced with code from the file
305
305
  ```
306
306
 
307
307
  ```md
308
- <!-- doc-gen CODE src="./relative/path/to/code.js" lines=22-44 -->
309
- This content will be dynamically replaced with code from the file lines 22 through 44
310
- <!-- end-doc-gen -->
311
- ```
308
+ <!-- doc-gen CODE src="./relative/path/to/code.js" lines=22-44 -->
309
+ This content will be dynamically replaced with code from the file lines 22 through 44
310
+ <!-- end-doc-gen -->
311
+ ```
312
312
 
313
313
  Default `MATCHWORD` is `AUTO-GENERATED-CONTENT`
314
314
 
@@ -581,7 +581,7 @@ This will replace all the contents of inside the comment DUDE
581
581
 
582
582
  ## Usage examples
583
583
 
584
- - [Project using markdown-magic](https://github.com/search?o=desc&q=filename%3Apackage.json+%22markdown-magic%22&s=indexed&type=Code)
584
+ - [Projects using markdown-magic](https://github.com/search?q=path%3A**%2Fpackage.json+%22markdown-magic%22&type=code)
585
585
  - [Examples in md](https://github.com/search?l=Markdown&o=desc&q=AUTO-GENERATED-CONTENT&s=indexed&type=Code)
586
586
 
587
587
 
@@ -600,4 +600,4 @@ This was inspired by [Kent C Dodds](https://twitter.com/kentcdodds) and [jfmenge
600
600
  [npm-badge]:https://img.shields.io/npm/v/markdown-magic.svg?style=flat-square
601
601
  [npm-link]: http://www.npmjs.com/package/markdown-magic
602
602
  [mit]: http://opensource.org/licenses/MIT
603
- [author]: http://github.com/davidwells
603
+ [author]: http://github.com/davidwells
package/cli.js CHANGED
@@ -5,7 +5,8 @@ const argv = process.argv.slice(2)
5
5
  const cliArgs = mri(argv)
6
6
 
7
7
  /*
8
- console.log('Raw args:', argv)
9
- console.log('Before args:', cliArgs)
8
+ console.log('Raw argv:', argv)
9
+ console.log('mri argv:', cliArgs)
10
+ // process.exit(1)
10
11
  /** */
11
- runCli(cliArgs)
12
+ runCli(cliArgs, argv)
@@ -0,0 +1,11 @@
1
+ # Argparse
2
+
3
+ WIP. A forgiven argv parser.
4
+
5
+ See `argparse.test.js` for usage.
6
+
7
+ Rules
8
+
9
+ - If single char `-x` is found, it is a boolean flag.
10
+ - If `-x=value` is found, it is a key value pair.
11
+ - If `--` is found, then all following args collected as a full arg `rest`
@@ -0,0 +1,317 @@
1
+ const mri = require('mri')
2
+ const { parse } = require('oparser')
3
+ const { getGlobGroupsFromArgs } = require('../globparse')
4
+ const { splitOutsideQuotes } = require('./splitOutsideQuotes')
5
+ /*
6
+ const yargs = require('yargs/yargs')
7
+ const { hideBin } = require('yargs/helpers')
8
+ // process.exit(1)
9
+ /** */
10
+
11
+ const IS_KEY_VALUE_IN_QUOTES = /^([A-Za-z0-9_.]*)=\s?("|')(.*)(\2)$/
12
+ const IS_KEY_VALUE_START_WITH_QUOTE = /^([A-Za-z0-9_.]*)=\s?("|')?(.*)/
13
+ const IS_INTEGER = /^-?\d*(\.(?=\d))?\d*$/
14
+
15
+ /*
16
+ node cli.js hahaha -y -rx yoyo nono=haha --stage prod -nice=true word = bond -ab
17
+
18
+ [
19
+ _ ['hahaha'],
20
+ 'y': true,
21
+ 'r': true,
22
+ 'x': true,
23
+ 'yoyo': true,
24
+ 'nono: haha',
25
+ 'stage': 'prod',
26
+ 'nice': true,
27
+ 'word': 'bond',
28
+ 'a': true,
29
+ 'b': true,
30
+ ]
31
+ */
32
+
33
+ function isNegativeNumber(input) {
34
+ return /^-\d/g.test(input)
35
+ }
36
+
37
+ function uxParse(_rawArgv = [], opts = {}) {
38
+ /* Trim empty strings */
39
+ const { stripSingleDashOptions = true } = opts
40
+ const rawArgv = _rawArgv.filter(x => x !== '')
41
+ let mriOptionsOriginal = mri(rawArgv)
42
+
43
+ const yargsParsed = 'not enabled' //yargs(_rawArgv).parse()
44
+ let extraParse = {}
45
+ let globGroups = []
46
+ const mriOptionsClean = Object.assign({}, mriOptionsOriginal)
47
+
48
+ const _singleDashOptions = findSingleDashStrings(rawArgv)
49
+ console.log('_singleDashOptions', _singleDashOptions)
50
+
51
+ // Get all leading CLI commands that are strings that are not options. Stop if option is found
52
+ const leadingCommands = []
53
+ for (let i = 0; i < rawArgv.length; i++) {
54
+ const curr = rawArgv[i]
55
+ if (typeof curr === 'string' && !curr.match(/^-/) && !curr.match(IS_KEY_VALUE_START_WITH_QUOTE)) {
56
+ leadingCommands.push(curr)
57
+ } else {
58
+ break
59
+ }
60
+ }
61
+ console.log('leadingCommands', leadingCommands)
62
+
63
+ if (!rawArgv || !rawArgv.length) {
64
+ return {
65
+ leadingCommands,
66
+ extraParse,
67
+ mriOptionsOriginal,
68
+ globGroups
69
+ }
70
+ }
71
+ //*
72
+ console.log('rawArgv', rawArgv)
73
+ console.log('mri mriOptionsOriginal', mriOptionsOriginal)
74
+ const counter = {}
75
+ /** */
76
+ /* If raw args found, process them further */
77
+ if (
78
+ rawArgv.length
79
+ // && (mriOptionsOriginal._ && mriOptionsOriginal._.length || (mriOptionsOriginal.file || mriOptionsOriginal.files))
80
+ ) {
81
+ // if (isGlob(rawArgv[0])) {
82
+ // console.log('glob', rawArgv[0])
83
+ // mriOptionsOriginal.glob = rawArgv[0]
84
+ // }
85
+ const globParse = getGlobGroupsFromArgs(rawArgv, {
86
+ /* CLI args that should be glob keys */
87
+ globKeys: ['files', 'file']
88
+ })
89
+ globGroups = globParse.globGroups
90
+ const { otherOpts } = globParse
91
+ console.log('globGroups', globGroups)
92
+ /*
93
+
94
+ console.log('globParse', globParse)
95
+ // deepLog(globParse)
96
+ process.exit(1)
97
+ /** */
98
+ /* Parse for weird CLI inputs */
99
+
100
+ /* Handle -- and - flags */
101
+
102
+ console.log('left over otherOpts after globParse', otherOpts)
103
+ let newArray = []
104
+ let dashDashSet = false
105
+ let rest = []
106
+ for (let i = 0; i < otherOpts.length; i++) {
107
+ const curr = otherOpts[i].trim()
108
+ const currIsNegativeNumber = isNegativeNumber(curr)
109
+ const prev = newArray[i - 1]
110
+ const next = otherOpts[i + 1] || ''
111
+ const nextNext = otherOpts[i + 2] || ''
112
+ const isLast = otherOpts.length === i + 1
113
+ const isFirst = i === 0
114
+ const opt = cleanOption(curr)
115
+ console.log('--------------------------------')
116
+ console.log(`current "${curr}"`)
117
+ console.log(`cleaned "${opt}"`)
118
+ console.log(`previous "${opt}"`, prev)
119
+ console.log(`next for "${opt}"`, next)
120
+ console.log(`nextNext "${opt}"`, nextNext)
121
+ console.log('--------------------------------')
122
+
123
+ if (dashDashSet) {
124
+ rest.push(curr)
125
+ continue
126
+ }
127
+
128
+ if (curr === '--') {
129
+ dashDashSet = true
130
+ continue
131
+ }
132
+
133
+ let currentIsSingleDashOption = false
134
+ if (curr.match(/^-[^-]/) && !currIsNegativeNumber) {
135
+ console.log('single dash option', curr)
136
+ currentIsSingleDashOption = true
137
+ }
138
+ if (currIsNegativeNumber) {
139
+ newArray.push(curr)
140
+ continue
141
+ }
142
+ // console.log('prev', prev)
143
+ if (curr.match(/^-+/)) {
144
+ const cleanX = !isNegativeNumber(curr) ? curr.replace(/^-+/, '') : curr
145
+
146
+ const nextItemIsOption = next.match(/^-+/)
147
+
148
+ if (currentIsSingleDashOption && nextItemIsOption && cleanX.length < 4) {
149
+ continue
150
+ }
151
+ console.log('cleanX', cleanX)
152
+ if (nextItemIsOption || isLast) {
153
+ if (cleanX.match(IS_KEY_VALUE_IN_QUOTES) || cleanX.match(IS_KEY_VALUE_START_WITH_QUOTE)) {
154
+ newArray.push(cleanX + ' ')
155
+ } else {
156
+ newArray.push(cleanX + '=true ')
157
+ }
158
+ continue
159
+ }
160
+
161
+ // If the current option is the last option, don't add an equal sign
162
+ let equal = ' '
163
+ if (cleanX.indexOf('=') === -1 && !next.match(/=/)) {
164
+ equal = '='
165
+ }
166
+ if (isLast) {
167
+ equal = ' '
168
+ }
169
+ if (
170
+ nextNext.trim() === '='
171
+ // || nextNext.match(/=/)
172
+ ) {
173
+ equal = ' '
174
+ }
175
+ const final = cleanX + equal
176
+ newArray.push(final)
177
+ continue
178
+ }
179
+ if (prev && prev.match(/=\s?$/) && (curr.match(/^\s?=/) || curr.trim() === '=')) {
180
+ continue
181
+ }
182
+
183
+ const trailingSpace = curr === '=' ? '' : ' '
184
+ newArray.push(curr + trailingSpace)
185
+ }
186
+ console.log('newArray', newArray)
187
+ console.log('rest', rest)
188
+ const optString = newArray.join('')
189
+ console.log('optString', optString)
190
+ extraParse = parse(optString)
191
+ const optParts = splitOutsideQuotes(optString)
192
+ console.log('optParts', optParts)
193
+ const optStringPieces = optParts
194
+ .filter(Boolean)
195
+ .map((x) => {
196
+ console.log('x', x)
197
+ return parse(x)
198
+ })
199
+ console.log('optStringPieces', optStringPieces)
200
+
201
+ for (let i = 0; i < optStringPieces.length; i++) {
202
+ const curr = optStringPieces[i]
203
+ const key = Object.keys(curr)[0]
204
+ if (counter[key]) {
205
+ counter[key]++
206
+ } else {
207
+ counter[key] = 1
208
+ }
209
+ }
210
+ console.log('optStringPieces counter', counter)
211
+
212
+ const propertiesSetMoreThanOnce = Object.keys(counter).filter((x) => counter[x] > 1)
213
+ console.log('propertiesSetMoreThanOnce', propertiesSetMoreThanOnce)
214
+ const singleDashStrings = findSingleDashStrings(otherOpts).map((x) => x.replace(/^-+/, ''))
215
+ console.log('singleDashStrings', singleDashStrings)
216
+ // find any duplicate values in singleDashStrings. means its an array
217
+ const duplicateValues = singleDashStrings.filter((x, index) => singleDashStrings.indexOf(x) !== index)
218
+ console.log('duplicateValues', duplicateValues)
219
+ if (propertiesSetMoreThanOnce.length) {
220
+ console.log('duplicateValues', propertiesSetMoreThanOnce)
221
+ for (let i = 0; i < propertiesSetMoreThanOnce.length; i++) {
222
+ const curr = propertiesSetMoreThanOnce[i]
223
+ console.log('curr', curr)
224
+ // get all array items with key of curr
225
+ const arrayItems = optStringPieces.filter((x) => Object.keys(x)[0] === curr)
226
+ console.log('arrayItems', arrayItems)
227
+ // Set to extraParse as an array
228
+ extraParse[curr] = arrayItems.map((x) => x[curr])
229
+ // Make the array items unique
230
+ extraParse[curr] = [...new Set(extraParse[curr])]
231
+ }
232
+ }
233
+
234
+ console.log('before mriOptionsOriginal', mriOptionsOriginal)
235
+ console.log('before extraParse', extraParse)
236
+
237
+
238
+ console.log('stripSingleDashOptions', stripSingleDashOptions)
239
+ // const STRIP_SINGLE_DASH_OPTIONS = stripSingleDashOptions
240
+ const STRIP_SINGLE_DASH_OPTIONS = false
241
+
242
+ if (singleDashStrings.length) {
243
+ for (let i = 0; i < singleDashStrings.length; i++) {
244
+ const curr = singleDashStrings[i]
245
+ const parts = curr.split('=')
246
+ const word = parts[0]
247
+ const value = parts[1]
248
+ const shouldStrip = (value) ? true : STRIP_SINGLE_DASH_OPTIONS
249
+ // Loop over all letters of single dash mriOptionsOriginal -word and remove any corresponding letter: true
250
+ for (let j = 0; j < word.length; j++) {
251
+ const letter = word[j]
252
+ // console.log('letter', letter)
253
+ if (shouldStrip && mriOptionsClean[letter]) {
254
+ delete mriOptionsClean[letter]
255
+ }
256
+ if (mriOptionsClean[letter] === extraParse[word]) {
257
+ delete mriOptionsClean[letter]
258
+ }
259
+ }
260
+ }
261
+ console.log('after mriOptionsOriginal', mriOptionsClean)
262
+ }
263
+
264
+ /*
265
+ console.log('optStringArr', newArray)
266
+ console.log('optString', optString)
267
+ console.log('otherOpts strings', otherOpts)
268
+ console.log('nicely handed CLI args')
269
+ console.log('extraParse', extraParse)
270
+ process.exit(1)
271
+ /** */
272
+ }
273
+
274
+ const mergedOptions = {
275
+ ...mriOptionsClean,
276
+ ...extraParse,
277
+ }
278
+ delete mergedOptions._
279
+
280
+ // @TODO hoist this bc we are do it earlier
281
+ Object.keys(mergedOptions).forEach((key) => {
282
+ const count = counter[key]
283
+ if (count && key.length > 1 && count > 1) {
284
+ console.log(`key "${key}" count ${count}`)
285
+ const value = Array.isArray(extraParse[key]) ? extraParse[key] : mriOptionsOriginal[key]
286
+ console.log('value', value)
287
+ if (Array.isArray(value)) {
288
+ mergedOptions[key] = value
289
+ }
290
+ }
291
+ })
292
+
293
+ return {
294
+ rawArgv: rawArgv.join(' '),
295
+ leadingCommands,
296
+ globGroups,
297
+ extraParse,
298
+ mriOptionsOriginal,
299
+ mriOptionsClean,
300
+ mriDiff: mriOptionsOriginal !== mriOptionsClean,
301
+ yargsParsed,
302
+ mergedOptions
303
+ }
304
+ }
305
+
306
+ function cleanOption(option = '') {
307
+ return option.replace(/^-+/, '').trim()
308
+ }
309
+
310
+ function findSingleDashStrings(arr) {
311
+ return arr.filter(str => str.match(/^-[^-]/))
312
+ }
313
+
314
+
315
+ module.exports = {
316
+ uxParse,
317
+ }