markdown-magic 3.1.0 → 3.2.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 +6 -1
- package/lib/utils/syntax.js +6 -6
- package/lib/utils/text.js +15 -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, stripOutMultiLineDoubleDashComments } = 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" -->
|
|
@@ -163,6 +164,10 @@ module.exports = async function CODE(api) {
|
|
|
163
164
|
header = `\n${options.header}`
|
|
164
165
|
}
|
|
165
166
|
|
|
167
|
+
if (options.trimDeadCode) {
|
|
168
|
+
code = stripOutMultiLineDoubleDashComments(code)
|
|
169
|
+
}
|
|
170
|
+
|
|
166
171
|
return `\`\`\`${syntax}${header}
|
|
167
172
|
${code}
|
|
168
173
|
\`\`\``
|
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,21 @@ 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 stripOutMultiLineDoubleDashComments(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 stripAllHTMLComments(block) {
|
|
196
209
|
// ([^\s]*)?([ \t]*)?(\/\*{1,}[\n\*]*(\s?[\s\S]*?)?\*\/)([^\s<]*)?(\n{1,2})?
|
|
197
210
|
// https://regex101.com/r/WSioZ7/1
|
|
198
211
|
const pattern = new RegExp(`([^\\s]*)?([ \\t]*)?(<!-{2,}(\\s?[\\s\\S]*?)?-{2,}>)([^\\s<]*)?(\n{1,2})?`, 'gi')
|
|
@@ -328,6 +341,7 @@ module.exports = {
|
|
|
328
341
|
dedentString,
|
|
329
342
|
stripComments,
|
|
330
343
|
convertCommentSyntax,
|
|
344
|
+
stripOutMultiLineDoubleDashComments,
|
|
331
345
|
// stripCommentBlockJS,
|
|
332
346
|
trimString,
|
|
333
347
|
// future https://github.com/junfengliang/autowrap
|