@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
@@ -0,0 +1,147 @@
1
+ import utils from './utils';
2
+ import type { HtmlTransformOptionsInput, MarkdownTransformOptionsInput, MobiledocTransformOptionsInput, LexicalTransformOptionsInput, MobiledocCardTransformer } from './utils/types';
3
+ import type { AbsoluteToRelativeOptionsInput } from './utils/absolute-to-relative';
4
+ import type { RelativeToAbsoluteOptionsInput } from './utils/relative-to-absolute';
5
+ import type { AbsoluteToTransformReadyOptionsInput as AbsoluteToTransformReadyOptionsInputType } from './utils/absolute-to-transform-ready';
6
+ import type { RelativeToTransformReadyOptionsInput as RelativeToTransformReadyOptionsInputType } from './utils/relative-to-transform-ready';
7
+ import type { ToTransformReadyOptions } from './utils/to-transform-ready';
8
+ import type { TransformReadyToAbsoluteOptionsInput } from './utils/transform-ready-to-absolute';
9
+ import type { TransformReadyReplacementOptionsInput as TransformReadyToRelativeOptionsInput } from './utils/types';
10
+ interface ExpressResponse {
11
+ set(headers: Record<string, string>): void;
12
+ redirect(status: number, url: string): void;
13
+ redirect(url: string): void;
14
+ }
15
+ interface UrlUtilsOptions {
16
+ getSubdir?: () => string;
17
+ getSiteUrl?: () => string;
18
+ getAdminUrl?: () => string;
19
+ baseApiPath?: string;
20
+ defaultApiType?: 'content' | 'admin';
21
+ slugs?: {
22
+ reserved?: string[];
23
+ protected?: string[];
24
+ } | null;
25
+ redirectCacheMaxAge?: number | null;
26
+ staticImageUrlPrefix?: string;
27
+ staticFilesUrlPrefix?: string;
28
+ staticMediaUrlPrefix?: string;
29
+ assetBaseUrls?: {
30
+ image?: string | null;
31
+ files?: string | null;
32
+ media?: string | null;
33
+ };
34
+ cardTransformers?: MobiledocCardTransformer[];
35
+ }
36
+ export default class UrlUtils {
37
+ private _config;
38
+ private _assetBaseUrls;
39
+ getSubdir: () => string;
40
+ getSiteUrl: () => string;
41
+ getAdminUrl: () => string;
42
+ /**
43
+ * Initialization method to pass in URL configurations
44
+ * @param {Object} options
45
+ * @param {Function} options.getSubdir
46
+ * @param {Function} options.getSiteUrl
47
+ * @param {Function} options.getAdminUrl Ghost instance admin URL
48
+ * @param {String} [options.baseApiPath='/ghost/api'] static prefix for serving API. Should not te passed in, unless the API is being run under custom URL
49
+ * @param {('content' | 'admin')} [options.defaultApiType='content'] default API type to be used
50
+ * @param {Object} [options.slugs] object with 2 properties reserved and protected containing arrays of special case slugs
51
+ * @param {Number} [options.redirectCacheMaxAge]
52
+ * @param {String} [options.staticImageUrlPrefix='content/images'] static prefix for serving images. Should not be passed in, unless customizing ghost instance image storage
53
+ * @param {String} [options.staticFilesUrlPrefix='content/files'] static prefix for serving files. Should not be passed in, unless customizing ghost instance file storage
54
+ * @param {String} [options.staticMediaUrlPrefix='content/media'] static prefix for serving media. Should not be passed in, unless customizing ghost instance media storage
55
+ * @param {object} [options.assetBaseUrls] asset CDN base URLs
56
+ * @param {string} [options.assetBaseUrls.image] image asset CDN base URL
57
+ * @param {string} [options.assetBaseUrls.files] files asset CDN base URL
58
+ * @param {string} [options.assetBaseUrls.media] media asset CDN base URL
59
+ */
60
+ constructor(options?: UrlUtilsOptions);
61
+ private _assetOptionDefaults;
62
+ private _buildAssetOptions;
63
+ getProtectedSlugs(): string[];
64
+ /** urlJoin
65
+ * Returns a URL/path for internal use in Ghost.
66
+ * @param {string} arguments takes arguments and concats those to a valid path/URL.
67
+ * @return {string} URL concatinated URL/path of arguments.
68
+ */
69
+ urlJoin(...parts: string[]): string;
70
+ createUrl(urlPath?: string, absolute?: boolean, trailingSlash?: boolean): string;
71
+ urlFor(context: string | {
72
+ relativeUrl: string;
73
+ } | {
74
+ image?: string;
75
+ } | {
76
+ nav?: {
77
+ url: string;
78
+ };
79
+ }, data?: Record<string, unknown> | boolean | {
80
+ trailingSlash?: boolean;
81
+ type?: 'admin' | 'content';
82
+ } | null, absolute?: boolean): string;
83
+ redirect301(res: ExpressResponse, redirectUrl: string): void;
84
+ redirectToAdmin(status: number, res: ExpressResponse, adminPath: string): void;
85
+ absoluteToRelative(url: string, options?: AbsoluteToRelativeOptionsInput): string;
86
+ relativeToAbsolute(url: string, options?: RelativeToAbsoluteOptionsInput): string;
87
+ toTransformReady(url: string, itemPath: string | null | ToTransformReadyOptions, options?: ToTransformReadyOptions): string;
88
+ absoluteToTransformReady(url: string, options?: AbsoluteToTransformReadyOptionsInputType): string;
89
+ relativeToTransformReady(url: string, options?: RelativeToTransformReadyOptionsInputType): string;
90
+ transformReadyToAbsolute(url: string, options?: TransformReadyToAbsoluteOptionsInput): string;
91
+ transformReadyToRelative(url: string, options?: TransformReadyToRelativeOptionsInput): string;
92
+ htmlToTransformReady(html: string, itemPath: string | null | HtmlTransformOptionsInput, options?: HtmlTransformOptionsInput): string;
93
+ /**
94
+ * Convert relative URLs in html into absolute URLs
95
+ * @param {string} html
96
+ * @param {string} itemPath (path of current context)
97
+ * @param {Object} options
98
+ * @returns {object} htmlContent
99
+ * @description Takes html, blog url and item path and converts relative url into
100
+ * absolute urls. Returns an object. The html string can be accessed by calling `html()` on
101
+ * the variable that takes the result of this function
102
+ */
103
+ htmlRelativeToAbsolute(html: string, itemPath: string | null | HtmlTransformOptionsInput, options?: HtmlTransformOptionsInput): string;
104
+ htmlRelativeToTransformReady(html: string, itemPath: string | null | RelativeToTransformReadyOptionsInputType, options?: RelativeToTransformReadyOptionsInputType): string;
105
+ htmlAbsoluteToRelative(html: string, options?: HtmlTransformOptionsInput): string;
106
+ htmlAbsoluteToTransformReady(html: string, options?: AbsoluteToTransformReadyOptionsInputType): string;
107
+ markdownToTransformReady(markdown: string, itemPath: string | null | MarkdownTransformOptionsInput, options?: MarkdownTransformOptionsInput): string;
108
+ markdownRelativeToAbsolute(markdown: string, itemPath: string | null | MarkdownTransformOptionsInput, options?: MarkdownTransformOptionsInput): string;
109
+ markdownRelativeToTransformReady(markdown: string, itemPath: string | null | MarkdownTransformOptionsInput, options?: MarkdownTransformOptionsInput): string;
110
+ markdownAbsoluteToRelative(markdown: string, options?: MarkdownTransformOptionsInput): string;
111
+ markdownAbsoluteToTransformReady(markdown: string, options?: AbsoluteToTransformReadyOptionsInputType): string;
112
+ mobiledocToTransformReady(serializedMobiledoc: string, itemPath: string | null | MobiledocTransformOptionsInput, options?: MobiledocTransformOptionsInput): string;
113
+ mobiledocRelativeToAbsolute(serializedMobiledoc: string, itemPath: string | null | MobiledocTransformOptionsInput, options?: MobiledocTransformOptionsInput): string;
114
+ mobiledocRelativeToTransformReady(serializedMobiledoc: string, itemPath: string | null | MobiledocTransformOptionsInput, options?: MobiledocTransformOptionsInput): string;
115
+ mobiledocAbsoluteToRelative(serializedMobiledoc: string, options?: MobiledocTransformOptionsInput): string;
116
+ mobiledocAbsoluteToTransformReady(serializedMobiledoc: string, options?: MobiledocTransformOptionsInput): string;
117
+ lexicalToTransformReady(serializedLexical: string, itemPath: string | null | LexicalTransformOptionsInput, options?: LexicalTransformOptionsInput): string;
118
+ lexicalRelativeToAbsolute(serializedLexical: string, itemPath: string | null | LexicalTransformOptionsInput, options?: LexicalTransformOptionsInput): string;
119
+ lexicalRelativeToTransformReady(serializedLexical: string, itemPath: string | null | LexicalTransformOptionsInput, options?: LexicalTransformOptionsInput): string;
120
+ lexicalAbsoluteToRelative(serializedLexical: string, options?: LexicalTransformOptionsInput): string;
121
+ lexicalAbsoluteToTransformReady(serializedLexical: string, options?: LexicalTransformOptionsInput): string;
122
+ plaintextToTransformReady(plaintext: string, options?: Record<string, unknown>): string;
123
+ /**
124
+ * Return whether the provided URL is part of the site (checks if same domain and within subdirectory)
125
+ * @param {URL} url
126
+ * @param {string} [context] describing the context for which you need to check a url
127
+ * @returns {boolean}
128
+ */
129
+ isSiteUrl(url: URL, context?: string): boolean;
130
+ get isSSL(): typeof utils.isSSL;
131
+ get replacePermalink(): typeof utils.replacePermalink;
132
+ get deduplicateDoubleSlashes(): typeof utils.deduplicateDoubleSlashes;
133
+ /**
134
+ * If you request **any** image in Ghost, it get's served via
135
+ * http://your-blog.com/content/images/2017/01/02/author.png
136
+ *
137
+ * /content/images/ is a static prefix for serving images!
138
+ *
139
+ * But internally the image is located for example in your custom content path:
140
+ * my-content/another-dir/images/2017/01/02/author.png
141
+ */
142
+ get STATIC_IMAGE_URL_PREFIX(): string;
143
+ get STATIC_FILES_URL_PREFIX(): string;
144
+ get STATIC_MEDIA_URL_PREFIX(): string;
145
+ get _utils(): typeof utils;
146
+ }
147
+ export {};