markdown-magic 3.1.0 → 3.3.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/lib/transforms/code/index.js +13 -1
- package/lib/utils/syntax.js +6 -6
- package/lib/utils/text.js +21 -1
- package/package.json +1 -1
|
@@ -3,7 +3,7 @@ const path = require('path')
|
|
|
3
3
|
const { remoteRequest } = require('../../utils/remoteRequest')
|
|
4
4
|
const { isLocalPath } = require('../../utils/fs')
|
|
5
5
|
const { deepLog } = require('../../utils/logs')
|
|
6
|
-
const { getLineCount, getTextBetweenLines } = require('../../utils/text')
|
|
6
|
+
const { getLineCount, getTextBetweenLines, stripMultiLineDoubleDashComments, stripHTMLComments } = require('../../utils/text')
|
|
7
7
|
const { resolveGithubContents, isGithubLink } = require('./resolve-github-file')
|
|
8
8
|
|
|
9
9
|
const GITHUB_LINK = /https:\/\/github\.com\/([^/\s]*)\/([^/\s]*)\/blob\//
|
|
@@ -17,6 +17,7 @@ const RAW_URL_LIKE = /^([A-Za-z0-9_]*)\.([A-Za-z0-9_]*)\/(.*)/
|
|
|
17
17
|
* @property {string} [syntax] - The syntax of the code. If not specified, it will be inferred by fileType.
|
|
18
18
|
* @property {string} [header] - The header comment to add to the code snippet. Useful for pointing to relative source directory or adding live doc links.
|
|
19
19
|
* @property {string} [lines] - A range of lines of code to include from the file. The line range should be defined like "lines=22-44".
|
|
20
|
+
* @property {boolean} [trimDeadCode] - Remove multi-line comments that start with `//` from the code.
|
|
20
21
|
* @example
|
|
21
22
|
```md
|
|
22
23
|
<!-- doc-gen CODE src="./relative/path/to/code.js" -->
|
|
@@ -132,6 +133,9 @@ module.exports = async function CODE(api) {
|
|
|
132
133
|
}
|
|
133
134
|
}
|
|
134
135
|
|
|
136
|
+
/* Ensure lowercase on syntax */
|
|
137
|
+
syntax = (syntax || '').toLowerCase()
|
|
138
|
+
|
|
135
139
|
/* Check for Id */
|
|
136
140
|
if (id) {
|
|
137
141
|
const lines = code.split("\n")
|
|
@@ -162,6 +166,14 @@ module.exports = async function CODE(api) {
|
|
|
162
166
|
if (options.header) {
|
|
163
167
|
header = `\n${options.header}`
|
|
164
168
|
}
|
|
169
|
+
|
|
170
|
+
if (options.trimDeadCode) {
|
|
171
|
+
if (syntax === 'js' || syntax === 'ts' || syntax === 'javascript' || syntax === 'typescript') {
|
|
172
|
+
code = stripMultiLineDoubleDashComments(code)
|
|
173
|
+
} else if (syntax === 'html') {
|
|
174
|
+
code = stripHTMLComments(code, { multilineOnly: true })
|
|
175
|
+
}
|
|
176
|
+
}
|
|
165
177
|
|
|
166
178
|
return `\`\`\`${syntax}${header}
|
|
167
179
|
${code}
|
package/lib/utils/syntax.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
const html = {
|
|
2
2
|
tags: [
|
|
3
|
-
'<!--',
|
|
4
|
-
'-->'
|
|
3
|
+
'<!--', '-->'
|
|
5
4
|
],
|
|
6
5
|
pattern: [
|
|
7
6
|
'<!-{2,}',
|
|
@@ -13,7 +12,9 @@ const html = {
|
|
|
13
12
|
|
|
14
13
|
// JS https://regex101.com/r/XKHU18/5
|
|
15
14
|
const js = {
|
|
16
|
-
tags: [
|
|
15
|
+
tags: [
|
|
16
|
+
'/*', '*/'
|
|
17
|
+
],
|
|
17
18
|
pattern: [
|
|
18
19
|
'\/\\*+',
|
|
19
20
|
// Old ^ '\/\\*{1,}[\n\\*]*', // '\/\\*+', '\/\*[\*\n\s\t]+', //
|
|
@@ -27,8 +28,7 @@ const js = {
|
|
|
27
28
|
|
|
28
29
|
const jsx = {
|
|
29
30
|
tags: [
|
|
30
|
-
'{/*',
|
|
31
|
-
'*/}'
|
|
31
|
+
'{/*', '*/}'
|
|
32
32
|
],
|
|
33
33
|
pattern: [
|
|
34
34
|
'\{\/\\*+',
|
|
@@ -46,7 +46,7 @@ const yaml = {
|
|
|
46
46
|
singleLinePattern: '#+',
|
|
47
47
|
singleLine: '#.*$',
|
|
48
48
|
content: '[ \\t\\S]*?',
|
|
49
|
-
converter: (str) => {
|
|
49
|
+
converter: (str = '') => {
|
|
50
50
|
return str.split('\n').map((line) => {
|
|
51
51
|
return line[0] === '#' ? line : `#${line}`
|
|
52
52
|
}).join()
|
package/lib/utils/text.js
CHANGED
|
@@ -191,8 +191,22 @@ function stripComments(str, syntax = 'md') {
|
|
|
191
191
|
return str.replace(/\s?[ \t]*\/\*[\s\S]*?\*\/|\s?[ \t]*\/\/.*$|\/\*{1,}[\n\*]*(\s?[\s\S]*?)?\*+\//gm, '')
|
|
192
192
|
}
|
|
193
193
|
|
|
194
|
+
|
|
195
|
+
// https://regex101.com/r/nCnt2J/1
|
|
196
|
+
function stripMultiLineDoubleDashComments(str = '') {
|
|
197
|
+
return str.replace(/(?:\n\s*\/\/.*){2,}/g, '')
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// https://regex101.com/r/plpXmr/1
|
|
201
|
+
function stripOutMultilineJsComments(str = '') {
|
|
202
|
+
return str.replace(/\/\*[\s\S]+?\*\/\s*\n?/g, (match) => {
|
|
203
|
+
return match.trim().includes('\n') ? '' : match
|
|
204
|
+
})
|
|
205
|
+
}
|
|
206
|
+
|
|
194
207
|
// @TODO export as util to import into CODE
|
|
195
|
-
function
|
|
208
|
+
function stripHTMLComments(block, opts) {
|
|
209
|
+
const options = opts || {}
|
|
196
210
|
// ([^\s]*)?([ \t]*)?(\/\*{1,}[\n\*]*(\s?[\s\S]*?)?\*\/)([^\s<]*)?(\n{1,2})?
|
|
197
211
|
// https://regex101.com/r/WSioZ7/1
|
|
198
212
|
const pattern = new RegExp(`([^\\s]*)?([ \\t]*)?(<!-{2,}(\\s?[\\s\\S]*?)?-{2,}>)([^\\s<]*)?(\n{1,2})?`, 'gi')
|
|
@@ -219,6 +233,10 @@ function stripAllComments(block) {
|
|
|
219
233
|
const newLineCount = (trailingNewLine || '').length
|
|
220
234
|
const trailing = (!trailingText && newLineCount > 1) ? `${trailingNewLine || ''}` : ''
|
|
221
235
|
const leading = (leadingSpace) ? leadingSpace.slice(1) : ''
|
|
236
|
+
|
|
237
|
+
if (options.multilineOnly && comment.indexOf('\n') === -1) {
|
|
238
|
+
continue
|
|
239
|
+
}
|
|
222
240
|
remove.push(`${leading}${comment}${trailing}`)
|
|
223
241
|
}
|
|
224
242
|
return remove.reduce((acc, curr) => {
|
|
@@ -328,6 +346,8 @@ module.exports = {
|
|
|
328
346
|
dedentString,
|
|
329
347
|
stripComments,
|
|
330
348
|
convertCommentSyntax,
|
|
349
|
+
stripMultiLineDoubleDashComments,
|
|
350
|
+
stripHTMLComments,
|
|
331
351
|
// stripCommentBlockJS,
|
|
332
352
|
trimString,
|
|
333
353
|
// future https://github.com/junfengliang/autowrap
|