markdown-magic 3.0.10 → 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.
@@ -1,9 +1,9 @@
1
1
  const fs = require('fs')
2
2
  const path = require('path')
3
- const remoteRequest = require('../../utils/remoteRequest')
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
  \`\`\``
@@ -183,7 +188,7 @@ async function resolveRemoteContent(options, settings, srcPath) {
183
188
 
184
189
  // Try initial remote request if public url
185
190
  if (!remoteContent) {
186
- remoteContent = remoteRequest(src, settings, srcPath)
191
+ remoteContent = await remoteRequest(src, settings, srcPath)
187
192
  }
188
193
 
189
194
  return remoteContent
@@ -1,11 +1,11 @@
1
- const remoteRequest = require('../utils/remoteRequest')
1
+ const { remoteRequest } = require('../utils/remoteRequest')
2
2
 
3
- module.exports = function REMOTE(api) {
3
+ module.exports = async function REMOTE(api) {
4
4
  // console.log('REMOTE api', api)
5
5
  const { options, content, settings } = api
6
6
  const { regex } = settings
7
7
  // console.log('MAKE REMOTE REQUEST')
8
- const remoteContent = remoteRequest(options.url, settings, api.srcPath)
8
+ const remoteContent = await remoteRequest(options.url, settings, api.srcPath)
9
9
  if (!remoteContent) {
10
10
  return content
11
11
  }
@@ -1,8 +1,13 @@
1
+ const fetch = require('node-fetch')
1
2
  const request = require('sync-request')
2
3
 
3
- module.exports = function remoteRequest(url, settings = {}, srcPath) {
4
+ function formatUrl(url = '') {
5
+ return url.match(/^https?:\/\//) ? url : `https://${url}`
6
+ }
7
+
8
+ function remoteRequestSync(url, settings = {}, srcPath) {
4
9
  let body
5
- const finalUrl = (url.match(/^https?:\/\//)) ? url : `https://${url}`
10
+ const finalUrl = formatUrl(url)
6
11
  try {
7
12
  // @ts-expect-error
8
13
  const res = request('GET', finalUrl)
@@ -18,6 +23,28 @@ module.exports = function remoteRequest(url, settings = {}, srcPath) {
18
23
  return body
19
24
  }
20
25
 
26
+ async function remoteRequest(url, settings = {}, srcPath) {
27
+ let body
28
+ const finalUrl = formatUrl(url)
29
+ try {
30
+ const res = await fetch(finalUrl)
31
+ body = await res.text()
32
+ } catch (e) {
33
+ console.log(`⚠️ WARNING: REMOTE URL "${finalUrl}" NOT FOUND`)
34
+ const msg = (e.message || '').split('\n')[0] + `\nFix "${url}" value in ${srcPath}`
35
+ console.log(msg)
36
+ if (settings.failOnMissingRemote) {
37
+ throw new Error(msg)
38
+ }
39
+ }
40
+ return body
41
+ }
42
+
43
+ module.exports = {
44
+ remoteRequestSync,
45
+ remoteRequest
46
+ }
47
+
21
48
  /*
22
49
  TODO add file caching?
23
50
  */
@@ -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 stripAllComments(block) {
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
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "markdown-magic",
3
- "version": "3.0.10",
3
+ "version": "3.2.0",
4
4
  "description": "Automatically update markdown files with content from external sources",
5
5
  "main": "lib/index.js",
6
6
  "bin": {
@@ -53,6 +53,7 @@
53
53
  "is-valid-path": "^0.1.1",
54
54
  "micro-mdx-parser": "^1.1.0",
55
55
  "mri": "^1.2.0",
56
+ "node-fetch": "^2.7.0",
56
57
  "oparser": "^3.0.13",
57
58
  "smart-glob": "^1.0.2",
58
59
  "sync-request": "^6.1.0"