@tryghost/url-utils 2.0.6 → 2.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.
package/lib/index.js CHANGED
@@ -19,12 +19,12 @@ module.exports = class UrlUtils {
19
19
  * @param {Function} options.getSiteUrl
20
20
  * @param {Function} options.getAdminUrl Ghost instance admin URL
21
21
  * @param {Object} options.apiVersions configuration object which has defined `all` property which is an array of keys for other available properties
22
- * @param {('v2' | 'v3' | 'v4' | 'canary')} [options.defaultApiVersion] default API version which is one of the values from options.apiVersions
23
- * @param {('content' | 'admin')} [options.defaultApiType] default API type to be used and is one of the values from options.apiVersions
22
+ * @param {('v2' | 'v3' | 'v4' | 'canary')} [options.defaultApiVersion='v4'] default API version which is one of the values from options.apiVersions
23
+ * @param {('content' | 'admin')} [options.defaultApiType='content'] default API type to be used and is one of the values from options.apiVersions
24
24
  * @param {Object} [options.slugs] object with 2 properties reserved and protected containing arrays of special case slugs
25
25
  * @param {Number} [options.redirectCacheMaxAge]
26
- * @param {String} [options.baseApiPath] static prefix for serving API. Should not te passed in, unless the API is being run under custom URL
27
- * @param {String} [options.staticImageUrlPrefix] static prefix for serving images. Should not be passed in, unless customizing ghost instance image storage
26
+ * @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
27
+ * @param {String} [options.staticImageUrlPrefix='content/images'] static prefix for serving images. Should not be passed in, unless customizing ghost instance image storage
28
28
  */
29
29
  constructor(options = {}) {
30
30
  const defaultOptions = {
@@ -8,6 +8,18 @@ function extractSrcsetUrls(srcset = '') {
8
8
  });
9
9
  }
10
10
 
11
+ function extractStyleUrls(style = '') {
12
+ const urls = [];
13
+ const regex = /url\(['|"]([^)]+)['|"]\)/g;
14
+ let match;
15
+
16
+ while ((match = regex.exec(style)) !== null) {
17
+ urls.push(match[1]);
18
+ }
19
+
20
+ return urls;
21
+ }
22
+
11
23
  function htmlTransform(html = '', siteUrl, transformFunction, itemPath, _options) {
12
24
  const defaultOptions = {assetsOnly: false, secure: false};
13
25
  const options = Object.assign({}, defaultOptions, _options || {});
@@ -42,7 +54,7 @@ function htmlTransform(html = '', siteUrl, transformFunction, itemPath, _options
42
54
  }
43
55
 
44
56
  // find all of the relative url attributes that we care about
45
- ['href', 'src', 'srcset'].forEach((attributeName) => {
57
+ ['href', 'src', 'srcset', 'style'].forEach((attributeName) => {
46
58
  htmlContent('[' + attributeName + ']').each((ix, el) => {
47
59
  // ignore <stream> elems and html inside of <code> elements
48
60
  if (el.name === 'stream' || htmlContent(el).closest('code').length) {
@@ -57,8 +69,14 @@ function htmlTransform(html = '', siteUrl, transformFunction, itemPath, _options
57
69
  el = htmlContent(el);
58
70
  const originalValue = el.attr(attributeName);
59
71
 
60
- if (attributeName === 'srcset') {
61
- const urls = extractSrcsetUrls(originalValue);
72
+ if (attributeName === 'srcset' || attributeName === 'style') {
73
+ let urls;
74
+
75
+ if (attributeName === 'srcset') {
76
+ urls = extractSrcsetUrls(originalValue);
77
+ } else {
78
+ urls = extractStyleUrls(originalValue);
79
+ }
62
80
  const absoluteUrls = urls.map(url => transformFunction(url, siteUrl, itemPath, options));
63
81
  let transformedValue = originalValue;
64
82
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryghost/url-utils",
3
- "version": "2.0.6",
3
+ "version": "2.1.0",
4
4
  "repository": "https://github.com/TryGhost/SDK/tree/master/packages/url-utils",
5
5
  "author": "Ghost Foundation",
6
6
  "license": "MIT",
@@ -18,7 +18,7 @@
18
18
  "access": "public"
19
19
  },
20
20
  "devDependencies": {
21
- "@tryghost/config-url-helpers": "0.1.4",
21
+ "@tryghost/config-url-helpers": "0.1.5",
22
22
  "c8": "7.11.0",
23
23
  "mocha": "7.2.0",
24
24
  "rewire": "5.0.0",
@@ -33,5 +33,5 @@
33
33
  "remark-footnotes": "^1.0.0",
34
34
  "unist-util-visit": "^2.0.0"
35
35
  },
36
- "gitHead": "f8813610f5ba4eed4fbcbd025eddf78795fd8aab"
36
+ "gitHead": "8bfb11a7576f3a09aa2a4d633933bb8f341d9329"
37
37
  }