markdown-magic 3.0.10 → 3.1.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,6 +1,6 @@
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
6
  const { getLineCount, getTextBetweenLines } = require('../../utils/text')
@@ -183,7 +183,7 @@ async function resolveRemoteContent(options, settings, srcPath) {
183
183
 
184
184
  // Try initial remote request if public url
185
185
  if (!remoteContent) {
186
- remoteContent = remoteRequest(src, settings, srcPath)
186
+ remoteContent = await remoteRequest(src, settings, srcPath)
187
187
  }
188
188
 
189
189
  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
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "markdown-magic",
3
- "version": "3.0.10",
3
+ "version": "3.1.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"