@tryghost/url-utils 5.0.0-rc.1 → 5.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.
Files changed (92) hide show
  1. package/lib/UrlUtils.d.ts +147 -0
  2. package/lib/UrlUtils.js +270 -220
  3. package/lib/index.d.ts +2 -0
  4. package/lib/index.js +6 -0
  5. package/lib/utils/absolute-to-relative.d.ts +19 -0
  6. package/lib/utils/absolute-to-relative.js +16 -19
  7. package/lib/utils/absolute-to-transform-ready.d.ts +13 -0
  8. package/lib/utils/absolute-to-transform-ready.js +39 -94
  9. package/lib/utils/build-early-exit-match.d.ts +24 -0
  10. package/lib/utils/build-early-exit-match.js +39 -0
  11. package/lib/utils/deduplicate-double-slashes.d.ts +2 -0
  12. package/lib/utils/deduplicate-double-slashes.js +7 -3
  13. package/lib/utils/deduplicate-subdirectory.d.ts +9 -0
  14. package/lib/utils/deduplicate-subdirectory.js +5 -9
  15. package/lib/utils/html-absolute-to-relative.d.ts +3 -0
  16. package/lib/utils/html-absolute-to-relative.js +12 -12
  17. package/lib/utils/html-absolute-to-transform-ready.d.ts +3 -0
  18. package/lib/utils/html-absolute-to-transform-ready.js +18 -38
  19. package/lib/utils/html-relative-to-absolute.d.ts +3 -0
  20. package/lib/utils/html-relative-to-absolute.js +11 -10
  21. package/lib/utils/html-relative-to-transform-ready.d.ts +3 -0
  22. package/lib/utils/html-relative-to-transform-ready.js +18 -13
  23. package/lib/utils/html-to-transform-ready.d.ts +3 -0
  24. package/lib/utils/html-to-transform-ready.js +18 -10
  25. package/lib/utils/html-transform.d.ts +3 -0
  26. package/lib/utils/html-transform.js +26 -47
  27. package/lib/utils/index.d.ts +76 -0
  28. package/lib/utils/index.js +116 -37
  29. package/lib/utils/is-ssl.d.ts +2 -0
  30. package/lib/utils/is-ssl.js +5 -5
  31. package/lib/utils/lexical-absolute-to-relative.d.ts +3 -0
  32. package/lib/utils/lexical-absolute-to-relative.js +12 -11
  33. package/lib/utils/lexical-absolute-to-transform-ready.d.ts +3 -0
  34. package/lib/utils/lexical-absolute-to-transform-ready.js +13 -12
  35. package/lib/utils/lexical-relative-to-absolute.d.ts +3 -0
  36. package/lib/utils/lexical-relative-to-absolute.js +11 -9
  37. package/lib/utils/lexical-relative-to-transform-ready.d.ts +3 -0
  38. package/lib/utils/lexical-relative-to-transform-ready.js +11 -9
  39. package/lib/utils/lexical-to-transform-ready.d.ts +3 -0
  40. package/lib/utils/lexical-to-transform-ready.js +18 -10
  41. package/lib/utils/lexical-transform.d.ts +3 -0
  42. package/lib/utils/lexical-transform.js +25 -36
  43. package/lib/utils/markdown-absolute-to-relative.d.ts +3 -0
  44. package/lib/utils/markdown-absolute-to-relative.js +13 -13
  45. package/lib/utils/markdown-absolute-to-transform-ready.d.ts +3 -0
  46. package/lib/utils/markdown-absolute-to-transform-ready.js +19 -15
  47. package/lib/utils/markdown-relative-to-absolute.d.ts +3 -0
  48. package/lib/utils/markdown-relative-to-absolute.js +13 -13
  49. package/lib/utils/markdown-relative-to-transform-ready.d.ts +3 -0
  50. package/lib/utils/markdown-relative-to-transform-ready.js +13 -13
  51. package/lib/utils/markdown-to-transform-ready.d.ts +3 -0
  52. package/lib/utils/markdown-to-transform-ready.js +18 -10
  53. package/lib/utils/markdown-transform.d.ts +3 -0
  54. package/lib/utils/markdown-transform.js +15 -30
  55. package/lib/utils/mobiledoc-absolute-to-relative.d.ts +3 -0
  56. package/lib/utils/mobiledoc-absolute-to-relative.js +12 -11
  57. package/lib/utils/mobiledoc-absolute-to-transform-ready.d.ts +3 -0
  58. package/lib/utils/mobiledoc-absolute-to-transform-ready.js +13 -12
  59. package/lib/utils/mobiledoc-relative-to-absolute.d.ts +3 -0
  60. package/lib/utils/mobiledoc-relative-to-absolute.js +11 -9
  61. package/lib/utils/mobiledoc-relative-to-transform-ready.d.ts +3 -0
  62. package/lib/utils/mobiledoc-relative-to-transform-ready.js +11 -9
  63. package/lib/utils/mobiledoc-to-transform-ready.d.ts +3 -0
  64. package/lib/utils/mobiledoc-to-transform-ready.js +18 -10
  65. package/lib/utils/mobiledoc-transform.d.ts +3 -0
  66. package/lib/utils/mobiledoc-transform.js +8 -13
  67. package/lib/utils/plaintext-absolute-to-transform-ready.d.ts +5 -0
  68. package/lib/utils/plaintext-absolute-to-transform-ready.js +29 -16
  69. package/lib/utils/plaintext-relative-to-transform-ready.d.ts +3 -0
  70. package/lib/utils/plaintext-relative-to-transform-ready.js +16 -9
  71. package/lib/utils/plaintext-to-transform-ready.d.ts +5 -0
  72. package/lib/utils/plaintext-to-transform-ready.js +18 -10
  73. package/lib/utils/relative-to-absolute.d.ts +15 -0
  74. package/lib/utils/relative-to-absolute.js +28 -33
  75. package/lib/utils/relative-to-transform-ready.d.ts +8 -0
  76. package/lib/utils/relative-to-transform-ready.js +23 -18
  77. package/lib/utils/replace-permalink.d.ts +16 -0
  78. package/lib/utils/replace-permalink.js +45 -10
  79. package/lib/utils/strip-subdirectory-from-path.d.ts +9 -0
  80. package/lib/utils/strip-subdirectory-from-path.js +7 -11
  81. package/lib/utils/to-transform-ready.d.ts +5 -0
  82. package/lib/utils/to-transform-ready.js +18 -10
  83. package/lib/utils/transform-ready-to-absolute.d.ts +9 -0
  84. package/lib/utils/transform-ready-to-absolute.js +10 -28
  85. package/lib/utils/transform-ready-to-relative.d.ts +3 -0
  86. package/lib/utils/transform-ready-to-relative.js +5 -8
  87. package/lib/utils/types.d.ts +92 -0
  88. package/lib/utils/types.js +2 -0
  89. package/lib/utils/url-join.d.ts +12 -0
  90. package/lib/utils/url-join.js +12 -12
  91. package/package.json +20 -12
  92. package/index.js +0 -1
@@ -1,14 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
1
3
  function escapeRegExp(string) {
2
4
  return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
3
5
  }
4
-
5
- function trimTrailingSlash(url) {
6
- if (!url) {
7
- return url;
8
- }
9
- return url.replace(/\/+$/, '');
10
- }
11
-
12
6
  const transformReadyToAbsolute = function (str = '', root, _options = {}) {
13
7
  const defaultOptions = {
14
8
  replacementStr: '__GHOST_URL__',
@@ -20,34 +14,22 @@ const transformReadyToAbsolute = function (str = '', root, _options = {}) {
20
14
  mediaBaseUrl: null
21
15
  };
22
16
  const options = Object.assign({}, defaultOptions, _options);
23
-
24
17
  if (!str || str.indexOf(options.replacementStr) === -1) {
25
18
  return str;
26
19
  }
27
-
28
20
  const replacementRegex = new RegExp(escapeRegExp(options.replacementStr), 'g');
29
- const fallbackBase = trimTrailingSlash(options.imageBaseUrl || root);
30
- const mediaBase = trimTrailingSlash(options.mediaBaseUrl);
31
- const filesBase = trimTrailingSlash(options.filesBaseUrl);
32
- const imageBase = trimTrailingSlash(options.imageBaseUrl) || fallbackBase;
33
-
34
21
  return str.replace(replacementRegex, (match, offset) => {
35
22
  const remainder = str.slice(offset + match.length);
36
-
37
- if (options.staticMediaUrlPrefix && remainder.startsWith(`/${options.staticMediaUrlPrefix}`) && mediaBase) {
38
- return mediaBase;
23
+ if (remainder.startsWith(`/${options.staticMediaUrlPrefix}`) && options.mediaBaseUrl) {
24
+ return options.mediaBaseUrl.replace(/\/$/, '');
39
25
  }
40
-
41
- if (options.staticFilesUrlPrefix && remainder.startsWith(`/${options.staticFilesUrlPrefix}`) && filesBase) {
42
- return filesBase;
26
+ if (remainder.startsWith(`/${options.staticFilesUrlPrefix}`) && options.filesBaseUrl) {
27
+ return options.filesBaseUrl.replace(/\/$/, '');
43
28
  }
44
-
45
- if (options.staticImageUrlPrefix && remainder.startsWith(`/${options.staticImageUrlPrefix}`) && imageBase) {
46
- return imageBase;
29
+ if (remainder.startsWith(`/${options.staticImageUrlPrefix}`) && options.imageBaseUrl) {
30
+ return options.imageBaseUrl.replace(/\/$/, '');
47
31
  }
48
-
49
- return fallbackBase;
32
+ return root.replace(/\/$/, '');
50
33
  });
51
34
  };
52
-
53
- module.exports = transformReadyToAbsolute;
35
+ exports.default = transformReadyToAbsolute;
@@ -0,0 +1,3 @@
1
+ import type { TransformReadyReplacementOptionsInput } from './types';
2
+ declare const transformReadyToRelative: (str: string | undefined, root: string, _options?: TransformReadyReplacementOptionsInput) => string;
3
+ export default transformReadyToRelative;
@@ -1,24 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const url_1 = require("url");
1
4
  function escapeRegExp(string) {
2
5
  return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
3
6
  }
4
-
5
7
  const transformReadyToRelative = function (str = '', root, _options = {}) {
6
8
  const defaultOptions = {
7
9
  replacementStr: '__GHOST_URL__'
8
10
  };
9
11
  const options = Object.assign({}, defaultOptions, _options);
10
-
11
12
  if (!str || str.indexOf(options.replacementStr) === -1) {
12
13
  return str;
13
14
  }
14
-
15
- const rootURL = new URL(root);
15
+ const rootURL = new url_1.URL(root);
16
16
  // subdir with no trailing slash because we'll always have a trailing slash after the magic string
17
17
  const subdir = rootURL.pathname.replace(/\/$/, '');
18
-
19
18
  const replacementRegex = new RegExp(escapeRegExp(options.replacementStr), 'g');
20
-
21
19
  return str.replace(replacementRegex, subdir);
22
20
  };
23
-
24
- module.exports = transformReadyToRelative;
21
+ exports.default = transformReadyToRelative;
@@ -0,0 +1,92 @@
1
+ export type UnknownRecord = Record<string, unknown>;
2
+ export interface AssetAwareOptions extends UnknownRecord {
3
+ assetsOnly: boolean;
4
+ staticImageUrlPrefix?: string;
5
+ }
6
+ export type AssetAwareOptionsInput = Partial<AssetAwareOptions>;
7
+ export interface SecureOptions extends AssetAwareOptions {
8
+ secure?: boolean;
9
+ }
10
+ export type SecureOptionsInput = Partial<SecureOptions>;
11
+ export interface TransformReadyReplacementOptions extends UnknownRecord {
12
+ replacementStr: string;
13
+ withoutSubdirectory?: boolean;
14
+ }
15
+ export type TransformReadyReplacementOptionsInput = Partial<TransformReadyReplacementOptions>;
16
+ export interface BaseUrlOptions extends UnknownRecord {
17
+ imageBaseUrl?: string | null;
18
+ filesBaseUrl?: string | null;
19
+ mediaBaseUrl?: string | null;
20
+ }
21
+ export type BaseUrlOptionsInput = Partial<BaseUrlOptions>;
22
+ export interface EarlyExitOptions extends UnknownRecord {
23
+ earlyExitMatchStr?: string;
24
+ }
25
+ export type EarlyExitOptionsInput = Partial<EarlyExitOptions>;
26
+ export interface AbsoluteToTransformReadyOptions extends TransformReadyReplacementOptions, BaseUrlOptions, AssetAwareOptions {
27
+ staticFilesUrlPrefix?: string;
28
+ staticMediaUrlPrefix?: string;
29
+ }
30
+ export type AbsoluteToTransformReadyOptionsInput = Partial<AbsoluteToTransformReadyOptions>;
31
+ export type UrlTransformFunction = (url: string, siteUrl: string, itemPath: string | null, options: UnknownRecord) => string;
32
+ export type TransformType = 'relativeToAbsolute' | 'absoluteToRelative' | 'toTransformReady';
33
+ export interface HtmlTransformOptions extends SecureOptions {
34
+ earlyExitMatchStr?: string;
35
+ }
36
+ export type HtmlTransformOptionsInput = SecureOptionsInput & {
37
+ earlyExitMatchStr?: string;
38
+ };
39
+ export interface MarkdownTransformOptions extends AssetAwareOptions {
40
+ ignoreProtocol: boolean;
41
+ earlyExitMatchStr?: string;
42
+ }
43
+ export type MarkdownTransformOptionsInput = AssetAwareOptionsInput & {
44
+ ignoreProtocol?: boolean;
45
+ earlyExitMatchStr?: string;
46
+ };
47
+ export interface MarkdownTransformFunctions {
48
+ html: UrlTransformFunction;
49
+ url: UrlTransformFunction;
50
+ }
51
+ export type CardTransformer = (payload: unknown, options: UnknownRecord) => unknown;
52
+ export interface MobiledocCardTransformer {
53
+ name: string;
54
+ relativeToAbsolute?: CardTransformer;
55
+ absoluteToRelative?: CardTransformer;
56
+ toTransformReady?: CardTransformer;
57
+ }
58
+ export interface MobiledocTransformOptions extends SecureOptions {
59
+ cardTransformers: MobiledocCardTransformer[];
60
+ siteUrl: string;
61
+ itemPath: string | null;
62
+ transformType: TransformType;
63
+ }
64
+ export type MobiledocTransformOptionsInput = Partial<Omit<MobiledocTransformOptions, 'cardTransformers' | 'siteUrl' | 'itemPath' | 'transformType'>> & {
65
+ cardTransformers?: MobiledocCardTransformer[];
66
+ siteUrl?: string;
67
+ itemPath?: string | null;
68
+ transformType?: TransformType;
69
+ };
70
+ export type LexicalTransformFunction = (value: string) => string;
71
+ export interface LexicalUrlTransformMap {
72
+ [key: string]: string | LexicalUrlTransformMap;
73
+ }
74
+ export interface LexicalNodeConfig {
75
+ getType(): string;
76
+ urlTransformMap?: LexicalUrlTransformMap;
77
+ }
78
+ export type LexicalTransformRegistry = Partial<Record<TransformType, Record<string, LexicalTransformFunction>>>;
79
+ export interface LexicalTransformOptions extends SecureOptions {
80
+ nodes: LexicalNodeConfig[];
81
+ transformMap: LexicalTransformRegistry;
82
+ transformType: TransformType;
83
+ siteUrl: string;
84
+ itemPath: string | null;
85
+ }
86
+ export type LexicalTransformOptionsInput = Partial<Omit<LexicalTransformOptions, 'nodes' | 'transformMap' | 'siteUrl' | 'itemPath' | 'transformType'>> & {
87
+ nodes?: LexicalNodeConfig[];
88
+ transformMap?: LexicalTransformRegistry;
89
+ siteUrl?: string;
90
+ itemPath?: string | null;
91
+ transformType?: TransformType;
92
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,12 @@
1
+ interface UrlJoinOptions {
2
+ rootUrl: string;
3
+ }
4
+ /** urlJoin
5
+ * Returns a URL/path for internal use in Ghost.
6
+ * @param {string[]} parts takes parts and concats those to a valid path/URL.
7
+ * @param {Object} options
8
+ * @param {string} options.rootUrl used for deduplicating any subdirectories
9
+ * @return {string} URL concatinated URL/path of arguments.
10
+ */
11
+ declare function urlJoin(parts: string[], options?: UrlJoinOptions): string;
12
+ export default urlJoin;
@@ -1,5 +1,9 @@
1
- const deduplicateSubdirectory = require('./deduplicate-subdirectory');
2
-
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const deduplicate_subdirectory_1 = __importDefault(require("./deduplicate-subdirectory"));
3
7
  /** urlJoin
4
8
  * Returns a URL/path for internal use in Ghost.
5
9
  * @param {string[]} parts takes parts and concats those to a valid path/URL.
@@ -7,31 +11,27 @@ const deduplicateSubdirectory = require('./deduplicate-subdirectory');
7
11
  * @param {string} options.rootUrl used for deduplicating any subdirectories
8
12
  * @return {string} URL concatinated URL/path of arguments.
9
13
  */
10
- function urlJoin(parts, options) {
14
+ function urlJoin(parts, options = { rootUrl: '' }) {
11
15
  let prefixDoubleSlash = false;
12
-
13
16
  // Remove empty item at the beginning
14
17
  if (parts[0] === '') {
15
18
  parts.shift();
16
19
  }
17
-
18
20
  // Handle schemeless protocols
19
21
  if (parts[0].indexOf('//') === 0) {
20
22
  prefixDoubleSlash = true;
21
23
  }
22
-
23
24
  // join the elements using a slash
24
25
  let url = parts.join('/');
25
-
26
26
  // Fix multiple slashes
27
27
  url = url.replace(/(^|[^:])\/\/+/g, '$1/');
28
-
29
28
  // Put the double slash back at the beginning if this was a schemeless protocol
30
29
  if (prefixDoubleSlash) {
31
30
  url = url.replace(/^\//, '//');
32
31
  }
33
-
34
- return deduplicateSubdirectory(url, options.rootUrl);
32
+ if (!options.rootUrl) {
33
+ return url;
34
+ }
35
+ return (0, deduplicate_subdirectory_1.default)(url, options.rootUrl);
35
36
  }
36
-
37
- module.exports = urlJoin;
37
+ exports.default = urlJoin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryghost/url-utils",
3
- "version": "5.0.0-rc.1",
3
+ "version": "5.1.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/TryGhost/SDK.git",
@@ -8,35 +8,43 @@
8
8
  },
9
9
  "author": "Ghost Foundation",
10
10
  "license": "MIT",
11
- "main": "index.js",
11
+ "main": "lib/index.js",
12
+ "types": "lib/index.d.ts",
12
13
  "scripts": {
13
14
  "dev": "echo \"Implement me!\"",
14
- "test": "NODE_ENV=testing c8 --all --reporter text --reporter cobertura --reporter html mocha './test/**/*.test.js'",
15
- "lint": "eslint . --ext .js --cache",
15
+ "pretest": "yarn build",
16
+ "test": "NODE_ENV=testing c8 --src lib --all --reporter text --reporter cobertura --reporter html mocha './test/**/*.test.js'",
17
+ "build": "tsc -p tsconfig.json",
18
+ "lint": "eslint src test --ext .js,.ts --cache",
19
+ "prepare": "NODE_ENV=production yarn build",
16
20
  "posttest": "yarn lint"
17
21
  },
18
22
  "files": [
19
- "lib/",
20
- "index.js"
23
+ "lib/"
21
24
  ],
22
25
  "publishConfig": {
23
26
  "access": "public"
24
27
  },
25
28
  "devDependencies": {
26
- "@tryghost/config-url-helpers": "^1.0.17",
29
+ "@tryghost/config-url-helpers": "^1.0.19",
30
+ "@types/cheerio": "^0.22.31",
31
+ "@types/lodash": "^4.14.202",
32
+ "@types/node": "^20.0.0",
27
33
  "c8": "10.1.3",
28
- "mocha": "11.7.4",
34
+ "mocha": "11.7.5",
29
35
  "rewire": "9.0.1",
30
36
  "should": "13.2.3",
31
- "sinon": "21.0.0"
37
+ "sinon": "21.0.0",
38
+ "typescript": "5.9.3"
32
39
  },
33
40
  "dependencies": {
34
41
  "cheerio": "^0.22.0",
35
42
  "lodash": "^4.17.21",
36
43
  "moment": "^2.27.0",
37
44
  "moment-timezone": "^0.5.31",
38
- "remark": "^11.0.2",
39
- "remark-footnotes": "^1.0.0",
45
+ "remark": "11.0.2",
46
+ "remark-footnotes": "1.0.0",
40
47
  "unist-util-visit": "^2.0.0"
41
- }
48
+ },
49
+ "gitHead": "a97d783ddb64ae80592ec03468b221a4ec37948a"
42
50
  }
package/index.js DELETED
@@ -1 +0,0 @@
1
- module.exports = require('./lib/UrlUtils');