marked-base-url 1.0.0 → 1.0.2

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/index.cjs CHANGED
@@ -3,13 +3,37 @@
3
3
  function baseUrl(base) {
4
4
  // extension code here
5
5
 
6
+ base = base.trim().replace(/\/+$/, '/'); // if multiple '/' at the end, just keep one
7
+ const isBaseAbsolute = /^[\w+]+:\/\//.test(base);
8
+ const dummyUrl = 'http://__dummy__';
9
+ const dummyBaseUrl = new URL(base, dummyUrl);
10
+ const dummyUrlLength = dummyUrl.length + (base.startsWith('/') ? 0 : 1);
11
+
6
12
  return {
7
13
  walkTokens(token) {
8
14
  if (!['link', 'image'].includes(token.type)) {
9
15
  return;
10
16
  }
11
17
 
12
- token.href = new URL(token.href, base).href;
18
+ if (isBaseAbsolute) {
19
+ try {
20
+ token.href = new URL(token.href, base).href;
21
+ } catch (e) {
22
+ // ignore
23
+ }
24
+ } else {
25
+ // base is not absolute
26
+ if (token.href.startsWith('/')) {
27
+ // the URL is from root
28
+ return;
29
+ }
30
+ try {
31
+ const temp = new URL(token.href, dummyBaseUrl).href;
32
+ token.href = temp.slice(dummyUrlLength);
33
+ } catch (e) {
34
+ // ignore
35
+ }
36
+ }
13
37
  }
14
38
  };
15
39
  }
package/lib/index.umd.js CHANGED
@@ -7,13 +7,37 @@
7
7
  function baseUrl(base) {
8
8
  // extension code here
9
9
 
10
+ base = base.trim().replace(/\/+$/, '/'); // if multiple '/' at the end, just keep one
11
+ const isBaseAbsolute = /^[\w+]+:\/\//.test(base);
12
+ const dummyUrl = 'http://__dummy__';
13
+ const dummyBaseUrl = new URL(base, dummyUrl);
14
+ const dummyUrlLength = dummyUrl.length + (base.startsWith('/') ? 0 : 1);
15
+
10
16
  return {
11
17
  walkTokens(token) {
12
18
  if (!['link', 'image'].includes(token.type)) {
13
19
  return;
14
20
  }
15
21
 
16
- token.href = new URL(token.href, base).href;
22
+ if (isBaseAbsolute) {
23
+ try {
24
+ token.href = new URL(token.href, base).href;
25
+ } catch (e) {
26
+ // ignore
27
+ }
28
+ } else {
29
+ // base is not absolute
30
+ if (token.href.startsWith('/')) {
31
+ // the URL is from root
32
+ return;
33
+ }
34
+ try {
35
+ const temp = new URL(token.href, dummyBaseUrl).href;
36
+ token.href = temp.slice(dummyUrlLength);
37
+ } catch (e) {
38
+ // ignore
39
+ }
40
+ }
17
41
  }
18
42
  };
19
43
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "marked-base-url",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "marked base url",
5
5
  "main": "./lib/index.cjs",
6
6
  "module": "./src/index.js",
@@ -39,26 +39,26 @@
39
39
  },
40
40
  "homepage": "https://github.com/markedjs/marked-base-url#readme",
41
41
  "peerDependencies": {
42
- "marked": "^4.3.0"
42
+ "marked": "^4 || ^5"
43
43
  },
44
44
  "devDependencies": {
45
- "@babel/core": "^7.21.4",
46
- "@babel/preset-env": "^7.21.4",
45
+ "@babel/core": "^7.22.8",
46
+ "@babel/preset-env": "^7.22.7",
47
47
  "@semantic-release/changelog": "^6.0.3",
48
- "@semantic-release/commit-analyzer": "^9.0.2",
48
+ "@semantic-release/commit-analyzer": "^10.0.1",
49
49
  "@semantic-release/git": "^10.0.1",
50
- "@semantic-release/github": "^8.0.7",
51
- "@semantic-release/npm": "^10.0.2",
52
- "@semantic-release/release-notes-generator": "^10.0.3",
53
- "babel-jest": "^29.5.0",
54
- "eslint": "^8.37.0",
55
- "eslint-config-standard": "^17.0.0",
50
+ "@semantic-release/github": "^9.0.3",
51
+ "@semantic-release/npm": "^10.0.4",
52
+ "@semantic-release/release-notes-generator": "^11.0.4",
53
+ "babel-jest": "^29.6.1",
54
+ "eslint": "^8.44.0",
55
+ "eslint-config-standard": "^17.1.0",
56
56
  "eslint-plugin-import": "^2.27.5",
57
- "eslint-plugin-n": "^15.7.0",
57
+ "eslint-plugin-n": "^16.0.1",
58
58
  "eslint-plugin-promise": "^6.1.1",
59
- "jest-cli": "^29.5.0",
60
- "marked": "^4.3.0",
61
- "rollup": "^3.20.2",
62
- "semantic-release": "^21.0.1"
59
+ "jest-cli": "^29.6.1",
60
+ "marked": "^5.1.0",
61
+ "rollup": "^3.26.0",
62
+ "semantic-release": "^21.0.7"
63
63
  }
64
64
  }
package/src/index.js CHANGED
@@ -1,13 +1,37 @@
1
1
  export function baseUrl(base) {
2
2
  // extension code here
3
3
 
4
+ base = base.trim().replace(/\/+$/, '/'); // if multiple '/' at the end, just keep one
5
+ const isBaseAbsolute = /^[\w+]+:\/\//.test(base);
6
+ const dummyUrl = 'http://__dummy__';
7
+ const dummyBaseUrl = new URL(base, dummyUrl);
8
+ const dummyUrlLength = dummyUrl.length + (base.startsWith('/') ? 0 : 1);
9
+
4
10
  return {
5
11
  walkTokens(token) {
6
12
  if (!['link', 'image'].includes(token.type)) {
7
13
  return;
8
14
  }
9
15
 
10
- token.href = new URL(token.href, base).href;
16
+ if (isBaseAbsolute) {
17
+ try {
18
+ token.href = new URL(token.href, base).href;
19
+ } catch (e) {
20
+ // ignore
21
+ }
22
+ } else {
23
+ // base is not absolute
24
+ if (token.href.startsWith('/')) {
25
+ // the URL is from root
26
+ return;
27
+ }
28
+ try {
29
+ const temp = new URL(token.href, dummyBaseUrl).href;
30
+ token.href = temp.slice(dummyUrlLength);
31
+ } catch (e) {
32
+ // ignore
33
+ }
34
+ }
11
35
  }
12
36
  };
13
37
  }