markdown-magic 3.7.0 → 4.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.
package/src/utils/fs.js CHANGED
@@ -9,10 +9,21 @@ const { readdir, stat, readFile } = fs
9
9
 
10
10
  const IS_HIDDEN_FILE = /(^|[\\\/])\.[^\\\/\.]/g
11
11
 
12
+ /**
13
+ * Check if value is a RegExp
14
+ * @param {any} thing - Value to check
15
+ * @returns {boolean} True if value is RegExp
16
+ */
12
17
  function isRegex(thing) {
13
18
  return (thing instanceof RegExp)
14
19
  }
15
20
 
21
+ /**
22
+ * Write file with directory creation if needed
23
+ * @param {string} filePath - File path to write to
24
+ * @param {string} content - Content to write
25
+ * @returns {Promise<void>}
26
+ */
16
27
  async function writeFile(filePath, content) {
17
28
  try {
18
29
  await fs.writeFile(filePath, content)
@@ -69,6 +80,12 @@ async function escalade(start, callback) {
69
80
 
70
81
  // }
71
82
 
83
+ /**
84
+ * Convert absolute path to relative path
85
+ * @param {string} file - Absolute file path
86
+ * @param {string} cwd - Current working directory
87
+ * @returns {string} Relative path
88
+ */
72
89
  function convertToRelativePath(file, cwd) {
73
90
  return file.replace(cwd, '').replace(/^\//, '')
74
91
  }
@@ -165,6 +182,11 @@ function depth(string) {
165
182
  return path.normalize(string).split(path.sep).length - 1;
166
183
  }
167
184
 
185
+ /**
186
+ * Check if path is local (not remote)
187
+ * @param {string} filePath - File path to check
188
+ * @returns {boolean} True if path is local
189
+ */
168
190
  function isLocalPath(filePath) {
169
191
  if (filePath.startsWith('github.com/') || filePath.startsWith('raw.githubusercontent.com/')) return false
170
192
  return _isLocalPath(filePath)
@@ -26,8 +26,8 @@ test('Finds file from dir', async () => {
26
26
  })
27
27
 
28
28
  test('getGitignoreContents', async () => {
29
- const files = await getGitignoreContents()
30
- /*
29
+ const files = await getGitignoreContents(path.resolve(__dirname, '../../', '.gitignore'))
30
+ //*
31
31
  console.log('files', files)
32
32
  /** */
33
33
  assert.is(Array.isArray(files), true)
@@ -1,12 +1,33 @@
1
1
 
2
+ /**
3
+ * Filter function to get only unique values
4
+ * @param {any} value - Current value
5
+ * @param {number} index - Current index
6
+ * @param {any[]} self - Original array
7
+ * @returns {boolean} True if value is unique
8
+ */
2
9
  function onlyUnique(value, index, self) {
3
10
  return self.indexOf(value) === index
4
11
  }
5
12
 
13
+ /**
14
+ * Get code location string in format path:line:column
15
+ * @param {string} srcPath - Source file path
16
+ * @param {string|number} line - Line number
17
+ * @param {string} [column='0'] - Column number
18
+ * @returns {string} Location string
19
+ */
6
20
  function getCodeLocation(srcPath, line, column = '0') {
7
21
  return `${srcPath}:${line}:${column}`
8
22
  }
9
23
 
24
+ /**
25
+ * Pluralize word based on count
26
+ * @param {any[]|number} thing - Array or number to count
27
+ * @param {string} [single=''] - Singular form
28
+ * @param {string} [plural=''] - Plural form
29
+ * @returns {string} Singular or plural form
30
+ */
10
31
  function pluralize(thing, single = '', plural = '') {
11
32
  const count = Array.isArray(thing) ? thing.length : Number(thing)
12
33
  return count === 1 ? single : plural
@@ -1,7 +1,7 @@
1
1
  // https://stackoverflow.com/questions/38859506/cancel-regex-match-if-timeout
2
2
  const util = require('util')
3
3
  const vm = require('vm')
4
- const { getBlockRegex } = require('../block-parser')
4
+ const { getBlockRegex } = require('comment-block-parser')
5
5
  const { getSyntaxInfo } = require('./syntax')
6
6
 
7
7
  const goodString = `
package/src/utils/text.js CHANGED
@@ -1,13 +1,29 @@
1
1
  const { syntaxMap } = require('./syntax')
2
2
 
3
+ /**
4
+ * Split string into lines
5
+ * @param {string} str - Input string
6
+ * @returns {string[]} Array of lines
7
+ */
3
8
  function getLines(str = '') {
4
9
  return str.split(/\r\n|\r|\n/)
5
10
  }
6
11
 
12
+ /**
13
+ * Get line count from string
14
+ * @param {string} str - Input string
15
+ * @returns {number} Number of lines
16
+ */
7
17
  function getLineCount(str = '') {
8
18
  return getLines(str).length
9
19
  }
10
20
 
21
+ /**
22
+ * Get row and column position from character index
23
+ * @param {string} input - Input string
24
+ * @param {number} indexToFind - Character index to find
25
+ * @returns {{row: number, col: number}} Row and column position
26
+ */
11
27
  function getRowAndColumnFromCharPos(input, indexToFind) {
12
28
  const preChunk = input.substr(0, indexToFind);
13
29
  const row = preChunk.split('\n').length - 1
@@ -16,23 +32,50 @@ function getRowAndColumnFromCharPos(input, indexToFind) {
16
32
  return { row, col }
17
33
  };
18
34
 
35
+ /**
36
+ * Get word count from string
37
+ * @param {string} str - Input string
38
+ * @returns {number} Word count
39
+ */
19
40
  function getWordCount(str = '') {
20
41
  return str.trim().split(/\s+/).length
21
42
  }
22
43
 
44
+ /**
45
+ * Get first character from string
46
+ * @param {string} str - Input string
47
+ * @returns {string} First character
48
+ */
23
49
  function getFirstCharacter(str) {
24
50
  return str.charAt(0)
25
51
  }
26
52
 
53
+ /**
54
+ * Get last character from string
55
+ * @param {string} str - Input string
56
+ * @returns {string} Last character
57
+ */
27
58
  function getLastCharacter(str) {
28
59
  return str.substr(-1)
29
60
  }
30
61
 
62
+ /**
63
+ * Get leading spaces from text
64
+ * @param {string} text - Input text
65
+ * @returns {string} Leading spaces
66
+ */
31
67
  function getLeadingSpaces(text) {
32
68
  const matches = text.match(/^\s*/)
33
69
  return (matches && matches[0]) ? matches[0] : ''
34
70
  }
35
71
 
72
+ /**
73
+ * Get text between character positions
74
+ * @param {string} text - Input text
75
+ * @param {number} start - Start position
76
+ * @param {number} end - End position
77
+ * @returns {string} Text between positions
78
+ */
36
79
  function getTextBetweenChars(text, start, end) {
37
80
  return text.slice(start, end)
38
81
  }
@@ -56,6 +99,14 @@ function getTextBetweenWords(s, prefix, suffix) {
56
99
  return s
57
100
  }
58
101
 
102
+ /**
103
+ * Replace text between character positions
104
+ * @param {string} str - Input string
105
+ * @param {number} start - Start position
106
+ * @param {number} end - End position
107
+ * @param {string} newStr - Replacement string
108
+ * @returns {string} Modified string
109
+ */
59
110
  function replaceTextBetweenChars(str = '', start, end, newStr) {
60
111
  return str.substring(0, start) + newStr + str.substring(end)
61
112
  }
@@ -77,22 +128,39 @@ function getTextBetweenLines(content, startLine, endLine) {
77
128
  if (startDefined && !endDefined) {
78
129
  return lines.slice(startLine - 1, startLine).join('')
79
130
  }
80
- if (startLine && endLine && parseInt(startLine, 10) <= parseInt(endLine, 10)) {
131
+ // @ts-ignore
132
+ if (startLine && endLine && Number(startLine) <= Number(endLine)) {
81
133
  return lines.slice(startLine - 1, endLine).join('\n')
82
134
  }
83
135
  }
84
136
 
137
+ /**
138
+ * Check if string is uppercase
139
+ * @param {string} str - Input string
140
+ * @returns {boolean} True if uppercase
141
+ */
85
142
  function isUpperCase(str) {
86
143
  return str === str.toUpperCase()
87
144
  }
88
145
 
89
146
  // https://github.com/jamiebuilds/min-indent/blob/master/index.js
147
+ /**
148
+ * Find minimum indentation in string
149
+ * @param {string} string - Input string
150
+ * @returns {number} Minimum indentation level
151
+ */
90
152
  function findMinIndent(string) {
91
153
  const match = string.match(/^[ \t]*(?=\S)/gm)
92
154
  if (!match) return 0
93
155
  return match.reduce((r, a) => Math.min(r, a.length), Infinity)
94
156
  }
95
157
 
158
+ /**
159
+ * Strip indentation from string
160
+ * @param {string} string - Input string
161
+ * @param {number} [indentation] - Indentation level to strip
162
+ * @returns {string} String with indentation stripped
163
+ */
96
164
  function stripIndent(string, indentation) {
97
165
  const indent = typeof indentation !== 'undefined' ? indentation : findMinIndent(string);
98
166
  if (indent === 0) {
@@ -105,7 +173,7 @@ function stripIndent(string, indentation) {
105
173
  /**
106
174
  * Trim leading & trailing spaces/line breaks in code and keeps the indentation of the first non-empty line
107
175
  * @param {string|number} str
108
- * @returns string
176
+ * @returns {string}
109
177
  */
110
178
  function trimString(str = '') {
111
179
  let content = (typeof str === 'number') ? str.toString() : str
@@ -113,6 +181,15 @@ function stripIndent(string, indentation) {
113
181
  return content.replace(/^(?:[\t ]*(?:\r?\n|\r))+|\s+$/g, '')
114
182
  }
115
183
 
184
+ /**
185
+ * Add indentation to string
186
+ * @param {string} string - Input string
187
+ * @param {number} [count=1] - Number of indentations to add
188
+ * @param {object} [options={}] - Options for indentation
189
+ * @param {string} [options.indent=' '] - Character(s) to use for indentation
190
+ * @param {boolean} [options.includeEmptyLines=false] - Whether to indent empty lines
191
+ * @returns {string} Indented string
192
+ */
116
193
  function indentString(string, count = 1, options = {}) {
117
194
  const {
118
195
  indent = ' ',
@@ -130,8 +207,8 @@ function indentString(string, count = 1, options = {}) {
130
207
  /**
131
208
  * Removes the indentation of multiline strings
132
209
  * @link https://github.com/victornpb/tiny-dedent/
133
- * @param {string} str A template literal string
134
- * @return {string} A string without the indentation
210
+ * @param {string} str - A template literal string
211
+ * @returns {string} A string without the indentation
135
212
  */
136
213
  function dedentString(str) {
137
214
  str = str.replace(/^[ \t]*\r?\n/, ''); // remove leading blank line
@@ -180,7 +257,7 @@ function stripCommentBlockOld(str, syntax = 'md') {
180
257
  /**
181
258
  * Strip out comment blocks
182
259
  * @param {string} str
183
- * @param {typeof import('../types')['syntaxType']} syntax
260
+ * @param {import('../types').SyntaxType} syntax
184
261
  * @returns {string} clean comment-less string
185
262
  */
186
263
  function stripComments(str, syntax = 'md') {
@@ -298,33 +375,48 @@ function convertCommentSyntax(str, { from, to }) {
298
375
  }
299
376
 
300
377
  /**
301
- * capitalize first letter
302
- * @param {string} str
303
- * @returns
378
+ * Capitalize first letter
379
+ * @param {string} str - Input string
380
+ * @returns {string} String with first letter capitalized
304
381
  */
305
382
  function capitalizeFirstLetter(str) {
306
383
  return capitalize(str.charAt(0)) + str.slice(1)
307
384
  }
308
385
 
309
386
  /**
310
- * capitalize string
311
- * @param {string} str
312
- * @returns
387
+ * Capitalize string
388
+ * @param {string} str - Input string
389
+ * @returns {string} Capitalized string
313
390
  */
314
391
  function capitalize(str = '') {
315
392
  return str.toUpperCase()
316
393
  }
317
394
 
395
+ /**
396
+ * Convert string to camelCase
397
+ * @param {string} str - Input string
398
+ * @returns {string} camelCase string
399
+ */
318
400
  function camelCase(str = '') {
319
401
  return str.replace(/[-_ ](\w)/g, (_, c) => c.toUpperCase())
320
402
  }
321
403
 
404
+ /**
405
+ * Convert string to kebab-case
406
+ * @param {string} str - Input string
407
+ * @returns {string} kebab-case string
408
+ */
322
409
  function kebabCase(str = '') {
323
410
  return str.replace(/\B([A-Z])/g, '-$1').toLowerCase()
324
411
  }
325
412
 
326
413
  const smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs?\.?|via)$/i;
327
414
 
415
+ /**
416
+ * Convert string to Title Case
417
+ * @param {string} str - Input string
418
+ * @returns {string} Title Case string
419
+ */
328
420
  function toTitleCase(str = '') {
329
421
  return str.replace(/[A-Za-z0-9\u00C0-\u00FF]+[^\s-]*/g, (match, index, title) => {
330
422
  if (index > 0