@tryghost/url-utils 4.1.0 → 4.3.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/url-utils.js CHANGED
@@ -495,6 +495,23 @@ module.exports = class UrlUtils {
495
495
  return utils.plaintextToTransformReady(plaintext, this.getSiteUrl(), _options);
496
496
  }
497
497
 
498
+ /**
499
+ * Return whether the provided URL is part of the site (checks if same domain and within subdirectory)
500
+ * @param {URL} url
501
+ * @param {string} [context] describing the context for which you need to check a url
502
+ * @returns {boolean}
503
+ */
504
+ isSiteUrl(url, context = 'home') {
505
+ const siteUrl = new URL(this.urlFor(context, true));
506
+ if (siteUrl.host === url.host) {
507
+ if (url.pathname.startsWith(siteUrl.pathname)) {
508
+ return true;
509
+ }
510
+ return false;
511
+ }
512
+ return false;
513
+ }
514
+
498
515
  get isSSL() {
499
516
  return utils.isSSL;
500
517
  }
@@ -1,5 +1,5 @@
1
1
  function lexicalTransform(serializedLexical, siteUrl, transformFunction, itemPath, _options = {}) {
2
- const defaultOptions = {assetsOnly: false, secure: false};
2
+ const defaultOptions = {assetsOnly: false, secure: false, nodes: [], transformMap: {}};
3
3
  const options = Object.assign({}, defaultOptions, _options, {siteUrl, itemPath});
4
4
 
5
5
  if (!serializedLexical) {
@@ -14,11 +14,21 @@ function lexicalTransform(serializedLexical, siteUrl, transformFunction, itemPat
14
14
  return serializedLexical;
15
15
  }
16
16
 
17
- // any lexical links will be a child object with a `url` attribute,
18
- // recursively walk the tree transforming any `.url`s
17
+ const nodeMap = new Map();
18
+ options.nodes.forEach(node => node.urlTransformMap && nodeMap.set(node.getType(), node.urlTransformMap));
19
+
19
20
  const transformChildren = function (children) {
20
21
  for (const child of children) {
21
- if (child.url) {
22
+ // cards (nodes) have a `type` attribute in their node data
23
+ if (child.type && nodeMap.has(child.type)) {
24
+ Object.entries(nodeMap.get(child.type)).forEach(([property, transform]) => {
25
+ if (child[property]) {
26
+ child[property] = options.transformMap[options.transformType][transform](child[property]);
27
+ }
28
+ });
29
+ } else if (child.url) {
30
+ // any lexical links will be a child object with a `url` attribute,
31
+ // recursively walk the tree transforming any `.url`s
22
32
  child.url = transformFunction(child.url, siteUrl, itemPath, options);
23
33
  }
24
34
 
@@ -2,7 +2,7 @@ const absoluteToRelative = require('./absolute-to-relative');
2
2
  const lexicalTransform = require('./_lexical-transform');
3
3
 
4
4
  function lexicalAbsoluteToRelative(serializedLexical, siteUrl, _options = {}) {
5
- const defaultOptions = {assetsOnly: false, secure: false, cardTransformers: []};
5
+ const defaultOptions = {assetsOnly: false, secure: false, nodes: [], transformMap: {}};
6
6
  const overrideOptions = {siteUrl, transformType: 'absoluteToRelative'};
7
7
  const options = Object.assign({}, defaultOptions, _options, overrideOptions);
8
8
 
@@ -2,7 +2,7 @@ const absoluteToTransformReady = require('./absolute-to-transform-ready');
2
2
  const lexicalTransform = require('./_lexical-transform');
3
3
 
4
4
  function lexicalAbsoluteToRelative(serializedLexical, siteUrl, _options = {}) {
5
- const defaultOptions = {assetsOnly: false, secure: false, cardTransformers: []};
5
+ const defaultOptions = {assetsOnly: false, secure: false, nodes: [], transformMap: {}};
6
6
  const overrideOptions = {siteUrl, transformType: 'toTransformReady'};
7
7
  const options = Object.assign({}, defaultOptions, _options, overrideOptions);
8
8
 
@@ -2,7 +2,7 @@ const relativeToAbsolute = require('./relative-to-absolute');
2
2
  const lexicalTransform = require('./_lexical-transform');
3
3
 
4
4
  function lexicalRelativeToAbsolute(serializedLexical, siteUrl, itemPath, _options = {}) {
5
- const defaultOptions = {assetsOnly: false, secure: false, cardTransformers: []};
5
+ const defaultOptions = {assetsOnly: false, secure: false, nodes: [], transformMap: {}};
6
6
  const overrideOptions = {siteUrl, itemPath, transformType: 'relativeToAbsolute'};
7
7
  const options = Object.assign({}, defaultOptions, _options, overrideOptions);
8
8
 
@@ -2,7 +2,7 @@ const relativeToTransformReady = require('./relative-to-transform-ready');
2
2
  const lexicalTransform = require('./_lexical-transform');
3
3
 
4
4
  function lexicalRelativeToTransformReady(serializedLexical, siteUrl, itemPath, _options = {}) {
5
- const defaultOptions = {assetsOnly: false, secure: false, cardTransformers: []};
5
+ const defaultOptions = {assetsOnly: false, secure: false, nodes: [], transformMap: {}};
6
6
  const overrideOptions = {siteUrl, transformType: 'toTransformReady'};
7
7
  const options = Object.assign({}, defaultOptions, _options, overrideOptions);
8
8
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tryghost/url-utils",
3
- "version": "4.1.0",
3
+ "version": "4.3.0",
4
4
  "repository": "https://github.com/TryGhost/SDK/tree/master/packages/url-utils",
5
5
  "author": "Ghost Foundation",
6
6
  "license": "MIT",
@@ -19,12 +19,12 @@
19
19
  "access": "public"
20
20
  },
21
21
  "devDependencies": {
22
- "@tryghost/config-url-helpers": "^1.0.3",
22
+ "@tryghost/config-url-helpers": "^1.0.4",
23
23
  "c8": "7.12.0",
24
- "mocha": "10.0.0",
24
+ "mocha": "10.1.0",
25
25
  "rewire": "6.0.0",
26
26
  "should": "13.2.3",
27
- "sinon": "14.0.0"
27
+ "sinon": "15.0.0"
28
28
  },
29
29
  "dependencies": {
30
30
  "cheerio": "^0.22.0",
@@ -34,5 +34,5 @@
34
34
  "remark-footnotes": "^1.0.0",
35
35
  "unist-util-visit": "^2.0.0"
36
36
  },
37
- "gitHead": "c4bc4d5ee2c7fea2eb9fc8de6cb8fd479ea039e3"
37
+ "gitHead": "bc7ef4154981d09195c25acd6eda0531b1cffa32"
38
38
  }