@tryghost/url-utils 4.3.1 → 4.4.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.
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
const _ = require('lodash');
|
|
2
|
+
|
|
3
|
+
// options.transformMap = {
|
|
4
|
+
// relativeToAbsolute: {
|
|
5
|
+
// url: (url, siteUrl, itemPath, options) => 'transformedUrl',
|
|
6
|
+
// html: (html, siteUrl, itemPath, options) => 'transformedHtml',
|
|
7
|
+
// }
|
|
8
|
+
// }
|
|
9
|
+
// options.transformType = 'relativeToAbsolute'
|
|
10
|
+
|
|
1
11
|
function lexicalTransform(serializedLexical, siteUrl, transformFunction, itemPath, _options = {}) {
|
|
2
12
|
const defaultOptions = {assetsOnly: false, secure: false, nodes: [], transformMap: {}};
|
|
3
13
|
const options = Object.assign({}, defaultOptions, _options, {siteUrl, itemPath});
|
|
@@ -14,21 +24,42 @@ function lexicalTransform(serializedLexical, siteUrl, transformFunction, itemPat
|
|
|
14
24
|
return serializedLexical;
|
|
15
25
|
}
|
|
16
26
|
|
|
27
|
+
// create a map of node types to urlTransformMap objects
|
|
28
|
+
// e.g. {'image': {src: 'url', caption: 'html'}
|
|
17
29
|
const nodeMap = new Map();
|
|
18
30
|
options.nodes.forEach(node => node.urlTransformMap && nodeMap.set(node.getType(), node.urlTransformMap));
|
|
19
31
|
|
|
32
|
+
const transformProperty = function (obj, propertyPath, transform) {
|
|
33
|
+
const propertyValue = _.get(obj, propertyPath);
|
|
34
|
+
|
|
35
|
+
if (Array.isArray(propertyValue)) {
|
|
36
|
+
propertyValue.forEach((item) => {
|
|
37
|
+
// arrays of objects need to be defined as a nested object in the urlTransformMap
|
|
38
|
+
// so the `transform` value is that nested object
|
|
39
|
+
Object.entries(transform).forEach(([itemPropertyPath, itemTransform]) => {
|
|
40
|
+
transformProperty(item, itemPropertyPath, itemTransform);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (propertyValue) {
|
|
48
|
+
_.set(obj, propertyPath, options.transformMap[options.transformType][transform](propertyValue));
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
// recursively walk the Lexical node tree transforming any card data properties and links
|
|
20
53
|
const transformChildren = function (children) {
|
|
21
54
|
for (const child of children) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
55
|
+
const isCard = child.type && nodeMap.has(child.type);
|
|
56
|
+
const isLink = !!child.url;
|
|
57
|
+
|
|
58
|
+
if (isCard) {
|
|
59
|
+
Object.entries(nodeMap.get(child.type)).forEach(([propertyPath, transform]) => {
|
|
60
|
+
transformProperty(child, propertyPath, transform);
|
|
28
61
|
});
|
|
29
|
-
} else if (
|
|
30
|
-
// any lexical links will be a child object with a `url` attribute,
|
|
31
|
-
// recursively walk the tree transforming any `.url`s
|
|
62
|
+
} else if (isLink) {
|
|
32
63
|
child.url = transformFunction(child.url, siteUrl, itemPath, options);
|
|
33
64
|
}
|
|
34
65
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tryghost/url-utils",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.4.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.
|
|
22
|
+
"@tryghost/config-url-helpers": "^1.0.6",
|
|
23
23
|
"c8": "7.13.0",
|
|
24
24
|
"mocha": "10.2.0",
|
|
25
25
|
"rewire": "6.0.0",
|
|
26
26
|
"should": "13.2.3",
|
|
27
|
-
"sinon": "15.0.
|
|
27
|
+
"sinon": "15.0.2"
|
|
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": "
|
|
37
|
+
"gitHead": "75e59f16ab0206aac74eed1daac91c2d54f3c695"
|
|
38
38
|
}
|