@tryghost/url-utils 5.0.0-rc.1 → 5.0.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/index.js +1 -0
- package/lib/UrlUtils.d.ts +3 -0
- package/lib/UrlUtils.js +45 -111
- package/lib/utils/absolute-to-relative.d.ts +13 -0
- package/lib/utils/absolute-to-relative.js +5 -13
- package/lib/utils/absolute-to-transform-ready.d.ts +4 -0
- package/lib/utils/absolute-to-transform-ready.js +31 -91
- package/lib/utils/build-early-exit-match.d.ts +14 -0
- package/lib/utils/build-early-exit-match.js +39 -0
- package/lib/utils/deduplicate-double-slashes.d.ts +1 -0
- package/lib/utils/deduplicate-double-slashes.js +6 -2
- package/lib/utils/deduplicate-subdirectory.d.ts +9 -0
- package/lib/utils/deduplicate-subdirectory.js +3 -7
- package/lib/utils/html-absolute-to-relative.d.ts +3 -0
- package/lib/utils/html-absolute-to-relative.js +3 -6
- package/lib/utils/html-absolute-to-transform-ready.d.ts +4 -0
- package/lib/utils/html-absolute-to-transform-ready.js +4 -31
- package/lib/utils/html-relative-to-absolute.d.ts +3 -0
- package/lib/utils/html-relative-to-absolute.js +3 -5
- package/lib/utils/html-relative-to-transform-ready.d.ts +3 -0
- package/lib/utils/html-relative-to-transform-ready.js +2 -5
- package/lib/utils/html-to-transform-ready.d.ts +3 -0
- package/lib/utils/html-to-transform-ready.js +2 -2
- package/lib/utils/html-transform.d.ts +4 -0
- package/lib/utils/html-transform.js +10 -30
- package/lib/utils/index.d.ts +0 -0
- package/lib/utils/index.js +2 -0
- package/lib/utils/is-ssl.d.ts +2 -0
- package/lib/utils/is-ssl.js +4 -4
- package/lib/utils/lexical-absolute-to-relative.d.ts +2 -0
- package/lib/utils/lexical-absolute-to-relative.js +4 -6
- package/lib/utils/lexical-absolute-to-transform-ready.d.ts +2 -0
- package/lib/utils/lexical-absolute-to-transform-ready.js +4 -6
- package/lib/utils/lexical-relative-to-absolute.d.ts +3 -0
- package/lib/utils/lexical-relative-to-absolute.js +4 -5
- package/lib/utils/lexical-relative-to-transform-ready.d.ts +3 -0
- package/lib/utils/lexical-relative-to-transform-ready.js +4 -5
- package/lib/utils/lexical-to-transform-ready.d.ts +3 -0
- package/lib/utils/lexical-to-transform-ready.js +2 -2
- package/lib/utils/lexical-transform.d.ts +2 -0
- package/lib/utils/lexical-transform.js +6 -19
- package/lib/utils/markdown-absolute-to-relative.d.ts +4 -0
- package/lib/utils/markdown-absolute-to-relative.js +3 -6
- package/lib/utils/markdown-absolute-to-transform-ready.d.ts +5 -0
- package/lib/utils/markdown-absolute-to-transform-ready.js +5 -8
- package/lib/utils/markdown-relative-to-absolute.d.ts +4 -0
- package/lib/utils/markdown-relative-to-absolute.js +3 -6
- package/lib/utils/markdown-relative-to-transform-ready.d.ts +4 -0
- package/lib/utils/markdown-relative-to-transform-ready.js +3 -6
- package/lib/utils/markdown-to-transform-ready.d.ts +3 -0
- package/lib/utils/markdown-to-transform-ready.js +2 -2
- package/lib/utils/markdown-transform.d.ts +5 -0
- package/lib/utils/markdown-transform.js +7 -25
- package/lib/utils/mobiledoc-absolute-to-relative.d.ts +2 -0
- package/lib/utils/mobiledoc-absolute-to-relative.js +4 -6
- package/lib/utils/mobiledoc-absolute-to-transform-ready.d.ts +2 -0
- package/lib/utils/mobiledoc-absolute-to-transform-ready.js +4 -6
- package/lib/utils/mobiledoc-relative-to-absolute.d.ts +3 -0
- package/lib/utils/mobiledoc-relative-to-absolute.js +4 -5
- package/lib/utils/mobiledoc-relative-to-transform-ready.d.ts +3 -0
- package/lib/utils/mobiledoc-relative-to-transform-ready.js +4 -5
- package/lib/utils/mobiledoc-to-transform-ready.d.ts +3 -0
- package/lib/utils/mobiledoc-to-transform-ready.js +2 -2
- package/lib/utils/mobiledoc-transform.d.ts +1 -0
- package/lib/utils/mobiledoc-transform.js +4 -10
- package/lib/utils/plaintext-absolute-to-transform-ready.d.ts +4 -0
- package/lib/utils/plaintext-absolute-to-transform-ready.js +18 -10
- package/lib/utils/plaintext-relative-to-transform-ready.d.ts +2 -0
- package/lib/utils/plaintext-relative-to-transform-ready.js +2 -3
- package/lib/utils/plaintext-to-transform-ready.d.ts +3 -0
- package/lib/utils/plaintext-to-transform-ready.js +2 -2
- package/lib/utils/relative-to-absolute.d.ts +13 -0
- package/lib/utils/relative-to-absolute.js +6 -18
- package/lib/utils/relative-to-transform-ready.d.ts +2 -0
- package/lib/utils/relative-to-transform-ready.js +2 -9
- package/lib/utils/replace-permalink.d.ts +5 -0
- package/lib/utils/replace-permalink.js +2 -3
- package/lib/utils/strip-subdirectory-from-path.d.ts +9 -0
- package/lib/utils/strip-subdirectory-from-path.js +5 -9
- package/lib/utils/to-transform-ready.d.ts +3 -0
- package/lib/utils/to-transform-ready.js +2 -2
- package/lib/utils/transform-ready-to-absolute.d.ts +1 -0
- package/lib/utils/transform-ready-to-absolute.js +9 -27
- package/lib/utils/transform-ready-to-relative.d.ts +1 -0
- package/lib/utils/transform-ready-to-relative.js +2 -6
- package/lib/utils/url-join.d.ts +9 -0
- package/lib/utils/url-join.js +2 -8
- package/package.json +16 -7
|
@@ -1,12 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// @ts-nocheck
|
|
1
3
|
const relativeToAbsolute = require('./relative-to-absolute');
|
|
2
4
|
const lexicalTransform = require('./lexical-transform');
|
|
3
|
-
|
|
4
5
|
function lexicalRelativeToAbsolute(serializedLexical, siteUrl, itemPath, _options = {}) {
|
|
5
|
-
const defaultOptions = {assetsOnly: false, secure: false, nodes: [], transformMap: {}};
|
|
6
|
-
const overrideOptions = {siteUrl, itemPath, transformType: 'relativeToAbsolute'};
|
|
6
|
+
const defaultOptions = { assetsOnly: false, secure: false, nodes: [], transformMap: {} };
|
|
7
|
+
const overrideOptions = { siteUrl, itemPath, transformType: 'relativeToAbsolute' };
|
|
7
8
|
const options = Object.assign({}, defaultOptions, _options, overrideOptions);
|
|
8
|
-
|
|
9
9
|
return lexicalTransform(serializedLexical, siteUrl, relativeToAbsolute, itemPath, options);
|
|
10
10
|
}
|
|
11
|
-
|
|
12
11
|
module.exports = lexicalRelativeToAbsolute;
|
|
@@ -1,12 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// @ts-nocheck
|
|
1
3
|
const relativeToTransformReady = require('./relative-to-transform-ready');
|
|
2
4
|
const lexicalTransform = require('./lexical-transform');
|
|
3
|
-
|
|
4
5
|
function lexicalRelativeToTransformReady(serializedLexical, siteUrl, itemPath, _options = {}) {
|
|
5
|
-
const defaultOptions = {assetsOnly: false, secure: false, nodes: [], transformMap: {}};
|
|
6
|
-
const overrideOptions = {siteUrl, transformType: 'toTransformReady'};
|
|
6
|
+
const defaultOptions = { assetsOnly: false, secure: false, nodes: [], transformMap: {} };
|
|
7
|
+
const overrideOptions = { siteUrl, transformType: 'toTransformReady' };
|
|
7
8
|
const options = Object.assign({}, defaultOptions, _options, overrideOptions);
|
|
8
|
-
|
|
9
9
|
return lexicalTransform(serializedLexical, siteUrl, relativeToTransformReady, itemPath, options);
|
|
10
10
|
}
|
|
11
|
-
|
|
12
11
|
module.exports = lexicalRelativeToTransformReady;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// @ts-nocheck
|
|
1
3
|
const lexicalRelativeToAbsolute = require('./lexical-relative-to-absolute');
|
|
2
4
|
const lexicalAbsoluteToTransformReady = require('./lexical-absolute-to-transform-ready');
|
|
3
|
-
|
|
4
5
|
function lexicalToTransformReady(lexical, siteUrl, itemPath, options) {
|
|
5
6
|
if (typeof itemPath === 'object' && !options) {
|
|
6
7
|
options = itemPath;
|
|
@@ -9,5 +10,4 @@ function lexicalToTransformReady(lexical, siteUrl, itemPath, options) {
|
|
|
9
10
|
const absolute = lexicalRelativeToAbsolute(lexical, siteUrl, itemPath, options);
|
|
10
11
|
return lexicalAbsoluteToTransformReady(absolute, siteUrl, options);
|
|
11
12
|
}
|
|
12
|
-
|
|
13
13
|
module.exports = lexicalToTransformReady;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// @ts-nocheck
|
|
1
3
|
const _ = require('lodash');
|
|
2
|
-
|
|
3
4
|
// options.transformMap = {
|
|
4
5
|
// relativeToAbsolute: {
|
|
5
6
|
// url: (url, siteUrl, itemPath, options) => 'transformedUrl',
|
|
@@ -7,31 +8,24 @@ const _ = require('lodash');
|
|
|
7
8
|
// }
|
|
8
9
|
// }
|
|
9
10
|
// options.transformType = 'relativeToAbsolute'
|
|
10
|
-
|
|
11
11
|
function lexicalTransform(serializedLexical, siteUrl, transformFunction, itemPath, _options = {}) {
|
|
12
|
-
const defaultOptions = {assetsOnly: false, secure: false, nodes: [], transformMap: {}};
|
|
13
|
-
const options = Object.assign({}, defaultOptions, _options, {siteUrl, itemPath});
|
|
14
|
-
|
|
12
|
+
const defaultOptions = { assetsOnly: false, secure: false, nodes: [], transformMap: {} };
|
|
13
|
+
const options = Object.assign({}, defaultOptions, _options, { siteUrl, itemPath });
|
|
15
14
|
if (!serializedLexical) {
|
|
16
15
|
return serializedLexical;
|
|
17
16
|
}
|
|
18
|
-
|
|
19
17
|
// function only accepts serialized lexical so there's no chance of accidentally
|
|
20
18
|
// modifying pass-by-reference objects
|
|
21
19
|
const lexical = JSON.parse(serializedLexical);
|
|
22
|
-
|
|
23
20
|
if (!lexical?.root?.children) {
|
|
24
21
|
return serializedLexical;
|
|
25
22
|
}
|
|
26
|
-
|
|
27
23
|
// create a map of node types to urlTransformMap objects
|
|
28
24
|
// e.g. {'image': {src: 'url', caption: 'html'}
|
|
29
25
|
const nodeMap = new Map();
|
|
30
26
|
options.nodes.forEach(node => node.urlTransformMap && nodeMap.set(node.getType(), node.urlTransformMap));
|
|
31
|
-
|
|
32
27
|
const transformProperty = function (obj, propertyPath, transform) {
|
|
33
28
|
const propertyValue = _.get(obj, propertyPath);
|
|
34
|
-
|
|
35
29
|
if (Array.isArray(propertyValue)) {
|
|
36
30
|
propertyValue.forEach((item) => {
|
|
37
31
|
// arrays of objects need to be defined as a nested object in the urlTransformMap
|
|
@@ -40,38 +34,31 @@ function lexicalTransform(serializedLexical, siteUrl, transformFunction, itemPat
|
|
|
40
34
|
transformProperty(item, itemPropertyPath, itemTransform);
|
|
41
35
|
});
|
|
42
36
|
});
|
|
43
|
-
|
|
44
37
|
return;
|
|
45
38
|
}
|
|
46
|
-
|
|
47
39
|
if (propertyValue) {
|
|
48
40
|
_.set(obj, propertyPath, options.transformMap[options.transformType][transform](propertyValue));
|
|
49
41
|
}
|
|
50
42
|
};
|
|
51
|
-
|
|
52
43
|
// recursively walk the Lexical node tree transforming any card data properties and links
|
|
53
44
|
const transformChildren = function (children) {
|
|
54
45
|
for (const child of children) {
|
|
55
46
|
const isCard = child.type && nodeMap.has(child.type);
|
|
56
47
|
const isLink = !!child.url;
|
|
57
|
-
|
|
58
48
|
if (isCard) {
|
|
59
49
|
Object.entries(nodeMap.get(child.type)).forEach(([propertyPath, transform]) => {
|
|
60
50
|
transformProperty(child, propertyPath, transform);
|
|
61
51
|
});
|
|
62
|
-
}
|
|
52
|
+
}
|
|
53
|
+
else if (isLink) {
|
|
63
54
|
child.url = transformFunction(child.url, siteUrl, itemPath, options);
|
|
64
55
|
}
|
|
65
|
-
|
|
66
56
|
if (child.children) {
|
|
67
57
|
transformChildren(child.children);
|
|
68
58
|
}
|
|
69
59
|
}
|
|
70
60
|
};
|
|
71
|
-
|
|
72
61
|
transformChildren(lexical.root.children);
|
|
73
|
-
|
|
74
62
|
return JSON.stringify(lexical);
|
|
75
63
|
}
|
|
76
|
-
|
|
77
64
|
module.exports = lexicalTransform;
|
|
@@ -1,14 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// @ts-nocheck
|
|
1
3
|
const markdownTransform = require('./markdown-transform');
|
|
2
4
|
const absoluteToRelative = require('./absolute-to-relative');
|
|
3
5
|
const htmlAbsoluteToRelative = require('./html-absolute-to-relative');
|
|
4
|
-
|
|
5
6
|
function markdownAbsoluteToRelative(markdown = '', siteUrl, _options = {}) {
|
|
6
|
-
const defaultOptions = {assetsOnly: false, ignoreProtocol: true};
|
|
7
|
+
const defaultOptions = { assetsOnly: false, ignoreProtocol: true };
|
|
7
8
|
const options = Object.assign({}, defaultOptions, _options);
|
|
8
|
-
|
|
9
9
|
options.earlyExitMatchStr = options.ignoreProtocol ? siteUrl.replace(/http:|https:/, '') : siteUrl;
|
|
10
10
|
options.earlyExitMatchStr = options.earlyExitMatchStr.replace(/\/$/, '');
|
|
11
|
-
|
|
12
11
|
// need to ignore itemPath because absoluteToRelative functions doen't take that option
|
|
13
12
|
const transformFunctions = {
|
|
14
13
|
html(_url, _siteUrl, _itemPath, __options) {
|
|
@@ -18,8 +17,6 @@ function markdownAbsoluteToRelative(markdown = '', siteUrl, _options = {}) {
|
|
|
18
17
|
return absoluteToRelative(_url, _siteUrl, __options);
|
|
19
18
|
}
|
|
20
19
|
};
|
|
21
|
-
|
|
22
20
|
return markdownTransform(markdown, siteUrl, transformFunctions, '', options);
|
|
23
21
|
}
|
|
24
|
-
|
|
25
22
|
module.exports = markdownAbsoluteToRelative;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
declare const markdownTransform: any;
|
|
2
|
+
declare const absoluteToTransformReady: any;
|
|
3
|
+
declare const htmlAbsoluteToTransformReady: any;
|
|
4
|
+
declare const buildEarlyExitMatch: any;
|
|
5
|
+
declare function markdownAbsoluteToTransformReady(markdown: string | undefined, siteUrl: any, _options?: {}): any;
|
|
@@ -1,14 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// @ts-nocheck
|
|
1
3
|
const markdownTransform = require('./markdown-transform');
|
|
2
4
|
const absoluteToTransformReady = require('./absolute-to-transform-ready');
|
|
3
5
|
const htmlAbsoluteToTransformReady = require('./html-absolute-to-transform-ready');
|
|
4
|
-
|
|
6
|
+
const { buildEarlyExitMatch } = require('./build-early-exit-match');
|
|
5
7
|
function markdownAbsoluteToTransformReady(markdown = '', siteUrl, _options = {}) {
|
|
6
|
-
const defaultOptions = {assetsOnly: false, ignoreProtocol: true};
|
|
8
|
+
const defaultOptions = { assetsOnly: false, ignoreProtocol: true };
|
|
7
9
|
const options = Object.assign({}, defaultOptions, _options);
|
|
8
|
-
|
|
9
|
-
options.earlyExitMatchStr = options.ignoreProtocol ? siteUrl.replace(/http:|https:/, '') : siteUrl;
|
|
10
|
-
options.earlyExitMatchStr = options.earlyExitMatchStr.replace(/\/$/, '');
|
|
11
|
-
|
|
10
|
+
options.earlyExitMatchStr = buildEarlyExitMatch(siteUrl, options);
|
|
12
11
|
// need to ignore itemPath because absoluteToTransformReady functions doen't take that option
|
|
13
12
|
const transformFunctions = {
|
|
14
13
|
html(_url, _siteUrl, _itemPath, __options) {
|
|
@@ -18,8 +17,6 @@ function markdownAbsoluteToTransformReady(markdown = '', siteUrl, _options = {})
|
|
|
18
17
|
return absoluteToTransformReady(_url, _siteUrl, __options);
|
|
19
18
|
}
|
|
20
19
|
};
|
|
21
|
-
|
|
22
20
|
return markdownTransform(markdown, siteUrl, transformFunctions, '', options);
|
|
23
21
|
}
|
|
24
|
-
|
|
25
22
|
module.exports = markdownAbsoluteToTransformReady;
|
|
@@ -1,22 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// @ts-nocheck
|
|
1
3
|
const markdownTransform = require('./markdown-transform');
|
|
2
4
|
const htmlRelativeToAbsolute = require('./html-relative-to-absolute');
|
|
3
5
|
const relativeToAbsolute = require('./relative-to-absolute');
|
|
4
|
-
|
|
5
6
|
function markdownRelativeToAbsolute(markdown = '', siteUrl, itemPath, _options = {}) {
|
|
6
|
-
const defaultOptions = {assetsOnly: false};
|
|
7
|
+
const defaultOptions = { assetsOnly: false };
|
|
7
8
|
const options = Object.assign({}, defaultOptions, _options);
|
|
8
|
-
|
|
9
9
|
options.earlyExitMatchStr = '\\]\\([^\\s\\)]|href=|src=|srcset=';
|
|
10
10
|
if (options.assetsOnly) {
|
|
11
11
|
options.earlyExitMatchStr = options.staticImageUrlPrefix;
|
|
12
12
|
}
|
|
13
|
-
|
|
14
13
|
const transformFunctions = {
|
|
15
14
|
html: htmlRelativeToAbsolute,
|
|
16
15
|
url: relativeToAbsolute
|
|
17
16
|
};
|
|
18
|
-
|
|
19
17
|
return markdownTransform(markdown, siteUrl, transformFunctions, itemPath, options);
|
|
20
18
|
}
|
|
21
|
-
|
|
22
19
|
module.exports = markdownRelativeToAbsolute;
|
|
@@ -1,22 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// @ts-nocheck
|
|
1
3
|
const markdownTransform = require('./markdown-transform');
|
|
2
4
|
const htmlRelativeToTransformReady = require('./html-relative-to-transform-ready');
|
|
3
5
|
const relativeToTransformReady = require('./relative-to-transform-ready');
|
|
4
|
-
|
|
5
6
|
function markdownRelativeToTransformReady(markdown = '', siteUrl, itemPath, _options = {}) {
|
|
6
|
-
const defaultOptions = {assetsOnly: false};
|
|
7
|
+
const defaultOptions = { assetsOnly: false };
|
|
7
8
|
const options = Object.assign({}, defaultOptions, _options);
|
|
8
|
-
|
|
9
9
|
options.earlyExitMatchStr = '\\]\\([^\\s\\)]|href=|src=|srcset=';
|
|
10
10
|
if (options.assetsOnly) {
|
|
11
11
|
options.earlyExitMatchStr = options.staticImageUrlPrefix;
|
|
12
12
|
}
|
|
13
|
-
|
|
14
13
|
const transformFunctions = {
|
|
15
14
|
html: htmlRelativeToTransformReady,
|
|
16
15
|
url: relativeToTransformReady
|
|
17
16
|
};
|
|
18
|
-
|
|
19
17
|
return markdownTransform(markdown, siteUrl, transformFunctions, itemPath, options);
|
|
20
18
|
}
|
|
21
|
-
|
|
22
19
|
module.exports = markdownRelativeToTransformReady;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// @ts-nocheck
|
|
1
3
|
const markdownRelativeToAbsolute = require('./markdown-relative-to-absolute');
|
|
2
4
|
const markdownAbsoluteToTransformReady = require('./markdown-absolute-to-transform-ready');
|
|
3
|
-
|
|
4
5
|
function markdownToTransformReady(markdown, siteUrl, itemPath, options) {
|
|
5
6
|
if (typeof itemPath === 'object' && !options) {
|
|
6
7
|
options = itemPath;
|
|
@@ -9,5 +10,4 @@ function markdownToTransformReady(markdown, siteUrl, itemPath, options) {
|
|
|
9
10
|
const absolute = markdownRelativeToAbsolute(markdown, siteUrl, itemPath, options);
|
|
10
11
|
return markdownAbsoluteToTransformReady(absolute, siteUrl, options);
|
|
11
12
|
}
|
|
12
|
-
|
|
13
13
|
module.exports = markdownToTransformReady;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
declare let remark: any;
|
|
2
|
+
declare const footnotes: any;
|
|
3
|
+
declare const visit: any;
|
|
4
|
+
declare function replaceLast(find: any, replace: any, str: any): any;
|
|
5
|
+
declare function markdownTransform(markdown: string | undefined, siteUrl: any, transformFunctions: any, itemPath: any, _options?: {}): string;
|
|
@@ -1,44 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// @ts-nocheck
|
|
1
3
|
let remark;
|
|
2
4
|
const footnotes = require('remark-footnotes');
|
|
3
5
|
const visit = require('unist-util-visit');
|
|
4
|
-
|
|
5
6
|
function replaceLast(find, replace, str) {
|
|
6
7
|
const lastIndex = str.lastIndexOf(find);
|
|
7
|
-
|
|
8
8
|
if (lastIndex === -1) {
|
|
9
9
|
return str;
|
|
10
10
|
}
|
|
11
|
-
|
|
12
11
|
const begin = str.substring(0, lastIndex);
|
|
13
12
|
const end = str.substring(lastIndex + find.length);
|
|
14
|
-
|
|
15
13
|
return begin + replace + end;
|
|
16
14
|
}
|
|
17
|
-
|
|
18
15
|
function markdownTransform(markdown = '', siteUrl, transformFunctions, itemPath, _options = {}) {
|
|
19
|
-
const defaultOptions = {assetsOnly: false, ignoreProtocol: true};
|
|
16
|
+
const defaultOptions = { assetsOnly: false, ignoreProtocol: true };
|
|
20
17
|
const options = Object.assign({}, defaultOptions, _options);
|
|
21
|
-
|
|
22
18
|
if (!markdown || (options.earlyExitMatchStr && !markdown.match(new RegExp(options.earlyExitMatchStr)))) {
|
|
23
19
|
return markdown;
|
|
24
20
|
}
|
|
25
|
-
|
|
26
21
|
const replacements = [];
|
|
27
|
-
|
|
28
22
|
if (!remark) {
|
|
29
23
|
remark = require('remark');
|
|
30
24
|
}
|
|
31
|
-
|
|
32
25
|
const tree = remark()
|
|
33
|
-
.use({settings: {commonmark: true}})
|
|
34
|
-
.use(footnotes, {inlineNotes: true})
|
|
26
|
+
.use({ settings: { commonmark: true } })
|
|
27
|
+
.use(footnotes, { inlineNotes: true })
|
|
35
28
|
.parse(markdown);
|
|
36
|
-
|
|
37
29
|
visit(tree, ['link', 'image', 'html'], (node) => {
|
|
38
30
|
if (node.type === 'html' && node.value.match(/src|srcset|href/)) {
|
|
39
31
|
const oldValue = node.value;
|
|
40
32
|
const newValue = transformFunctions.html(node.value, siteUrl, itemPath, options);
|
|
41
|
-
|
|
42
33
|
if (newValue !== oldValue) {
|
|
43
34
|
replacements.push({
|
|
44
35
|
old: oldValue,
|
|
@@ -48,11 +39,9 @@ function markdownTransform(markdown = '', siteUrl, transformFunctions, itemPath,
|
|
|
48
39
|
});
|
|
49
40
|
}
|
|
50
41
|
}
|
|
51
|
-
|
|
52
42
|
if (node.type === 'link' || node.type === 'image') {
|
|
53
43
|
const oldValue = node.url;
|
|
54
44
|
const newValue = transformFunctions.url(node.url, siteUrl, itemPath, options);
|
|
55
|
-
|
|
56
45
|
if (newValue !== oldValue) {
|
|
57
46
|
replacements.push({
|
|
58
47
|
old: oldValue,
|
|
@@ -63,36 +52,29 @@ function markdownTransform(markdown = '', siteUrl, transformFunctions, itemPath,
|
|
|
63
52
|
}
|
|
64
53
|
}
|
|
65
54
|
});
|
|
66
|
-
|
|
67
55
|
let result = markdown;
|
|
68
56
|
let offsetAdjustment = 0;
|
|
69
57
|
let nestedAdjustment = 0;
|
|
70
|
-
|
|
71
58
|
replacements.forEach((replacement, i) => {
|
|
72
59
|
const original = markdown.slice(replacement.start, replacement.end);
|
|
73
60
|
// only transform last occurrence of the old string because markdown links and images
|
|
74
61
|
// have urls at the end and we see replacements for outermost nested nodes first
|
|
75
62
|
const transformed = replaceLast(replacement.old, replacement.new, original);
|
|
76
|
-
|
|
77
63
|
let before = result.slice(0, replacement.start + offsetAdjustment);
|
|
78
64
|
let after = result.slice(replacement.end + offsetAdjustment, result.length);
|
|
79
|
-
|
|
80
65
|
result = before + transformed + after;
|
|
81
|
-
|
|
82
66
|
// adjust offset according to new lengths
|
|
83
67
|
const nextReplacement = replacements[i + 1];
|
|
84
68
|
const adjustment = transformed.length - original.length;
|
|
85
|
-
|
|
86
69
|
if (nextReplacement && nextReplacement.start < replacement.end) {
|
|
87
70
|
// next replacement is nested, do not apply any offset adjustments until we're out of nesting
|
|
88
71
|
nestedAdjustment = nestedAdjustment + adjustment;
|
|
89
|
-
}
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
90
74
|
offsetAdjustment = offsetAdjustment + adjustment + nestedAdjustment;
|
|
91
75
|
nestedAdjustment = 0;
|
|
92
76
|
}
|
|
93
77
|
});
|
|
94
|
-
|
|
95
78
|
return result;
|
|
96
79
|
}
|
|
97
|
-
|
|
98
80
|
module.exports = markdownTransform;
|
|
@@ -1,16 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// @ts-nocheck
|
|
1
3
|
const absoluteToRelative = require('./absolute-to-relative');
|
|
2
4
|
const mobiledocTransform = require('./mobiledoc-transform');
|
|
3
|
-
|
|
4
5
|
function mobiledocAbsoluteToRelative(serializedMobiledoc, siteUrl, _options = {}) {
|
|
5
|
-
const defaultOptions = {assetsOnly: false, secure: false, cardTransformers: []};
|
|
6
|
-
const overrideOptions = {siteUrl, transformType: 'absoluteToRelative'};
|
|
6
|
+
const defaultOptions = { assetsOnly: false, secure: false, cardTransformers: [] };
|
|
7
|
+
const overrideOptions = { siteUrl, transformType: 'absoluteToRelative' };
|
|
7
8
|
const options = Object.assign({}, defaultOptions, _options, overrideOptions);
|
|
8
|
-
|
|
9
9
|
const transformFunction = function (_url, _siteUrl, _itemPath, __options) {
|
|
10
10
|
return absoluteToRelative(_url, _siteUrl, __options);
|
|
11
11
|
};
|
|
12
|
-
|
|
13
12
|
return mobiledocTransform(serializedMobiledoc, siteUrl, transformFunction, '', options);
|
|
14
13
|
}
|
|
15
|
-
|
|
16
14
|
module.exports = mobiledocAbsoluteToRelative;
|
|
@@ -1,16 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// @ts-nocheck
|
|
1
3
|
const absoluteToTransformReady = require('./absolute-to-transform-ready');
|
|
2
4
|
const mobiledocTransform = require('./mobiledoc-transform');
|
|
3
|
-
|
|
4
5
|
function mobiledocAbsoluteToRelative(serializedMobiledoc, siteUrl, _options = {}) {
|
|
5
|
-
const defaultOptions = {assetsOnly: false, secure: false, cardTransformers: []};
|
|
6
|
-
const overrideOptions = {siteUrl, transformType: 'toTransformReady'};
|
|
6
|
+
const defaultOptions = { assetsOnly: false, secure: false, cardTransformers: [] };
|
|
7
|
+
const overrideOptions = { siteUrl, transformType: 'toTransformReady' };
|
|
7
8
|
const options = Object.assign({}, defaultOptions, _options, overrideOptions);
|
|
8
|
-
|
|
9
9
|
const transformFunction = function (_url, _siteUrl, _itemPath, __options) {
|
|
10
10
|
return absoluteToTransformReady(_url, _siteUrl, __options);
|
|
11
11
|
};
|
|
12
|
-
|
|
13
12
|
return mobiledocTransform(serializedMobiledoc, siteUrl, transformFunction, '', options);
|
|
14
13
|
}
|
|
15
|
-
|
|
16
14
|
module.exports = mobiledocAbsoluteToRelative;
|
|
@@ -1,12 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// @ts-nocheck
|
|
1
3
|
const relativeToAbsolute = require('./relative-to-absolute');
|
|
2
4
|
const mobiledocTransform = require('./mobiledoc-transform');
|
|
3
|
-
|
|
4
5
|
function mobiledocRelativeToAbsolute(serializedMobiledoc, siteUrl, itemPath, _options = {}) {
|
|
5
|
-
const defaultOptions = {assetsOnly: false, secure: false, cardTransformers: []};
|
|
6
|
-
const overrideOptions = {siteUrl, itemPath, transformType: 'relativeToAbsolute'};
|
|
6
|
+
const defaultOptions = { assetsOnly: false, secure: false, cardTransformers: [] };
|
|
7
|
+
const overrideOptions = { siteUrl, itemPath, transformType: 'relativeToAbsolute' };
|
|
7
8
|
const options = Object.assign({}, defaultOptions, _options, overrideOptions);
|
|
8
|
-
|
|
9
9
|
return mobiledocTransform(serializedMobiledoc, siteUrl, relativeToAbsolute, itemPath, options);
|
|
10
10
|
}
|
|
11
|
-
|
|
12
11
|
module.exports = mobiledocRelativeToAbsolute;
|
|
@@ -1,12 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// @ts-nocheck
|
|
1
3
|
const relativeToTransformReady = require('./relative-to-transform-ready');
|
|
2
4
|
const mobiledocTransform = require('./mobiledoc-transform');
|
|
3
|
-
|
|
4
5
|
function mobiledocRelativeToTransformReady(serializedMobiledoc, siteUrl, itemPath, _options = {}) {
|
|
5
|
-
const defaultOptions = {assetsOnly: false, secure: false, cardTransformers: []};
|
|
6
|
-
const overrideOptions = {siteUrl, transformType: 'toTransformReady'};
|
|
6
|
+
const defaultOptions = { assetsOnly: false, secure: false, cardTransformers: [] };
|
|
7
|
+
const overrideOptions = { siteUrl, transformType: 'toTransformReady' };
|
|
7
8
|
const options = Object.assign({}, defaultOptions, _options, overrideOptions);
|
|
8
|
-
|
|
9
9
|
return mobiledocTransform(serializedMobiledoc, siteUrl, relativeToTransformReady, itemPath, options);
|
|
10
10
|
}
|
|
11
|
-
|
|
12
11
|
module.exports = mobiledocRelativeToTransformReady;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// @ts-nocheck
|
|
1
3
|
const mobiledocRelativeToAbsolute = require('./mobiledoc-relative-to-absolute');
|
|
2
4
|
const mobiledocAbsoluteToTransformReady = require('./mobiledoc-absolute-to-transform-ready');
|
|
3
|
-
|
|
4
5
|
function mobiledocToTransformReady(mobiledoc, siteUrl, itemPath, options) {
|
|
5
6
|
if (typeof itemPath === 'object' && !options) {
|
|
6
7
|
options = itemPath;
|
|
@@ -9,5 +10,4 @@ function mobiledocToTransformReady(mobiledoc, siteUrl, itemPath, options) {
|
|
|
9
10
|
const absolute = mobiledocRelativeToAbsolute(mobiledoc, siteUrl, itemPath, options);
|
|
10
11
|
return mobiledocAbsoluteToTransformReady(absolute, siteUrl, options);
|
|
11
12
|
}
|
|
12
|
-
|
|
13
13
|
module.exports = mobiledocToTransformReady;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
declare function mobiledocTransform(serializedMobiledoc: any, siteUrl: any, transformFunction: any, itemPath: any, _options?: {}): string;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// @ts-nocheck
|
|
1
3
|
function mobiledocTransform(serializedMobiledoc, siteUrl, transformFunction, itemPath, _options = {}) {
|
|
2
|
-
const defaultOptions = {assetsOnly: false, secure: false, cardTransformers: []};
|
|
3
|
-
const options = Object.assign({}, defaultOptions, _options, {siteUrl, itemPath});
|
|
4
|
-
|
|
4
|
+
const defaultOptions = { assetsOnly: false, secure: false, cardTransformers: [] };
|
|
5
|
+
const options = Object.assign({}, defaultOptions, _options, { siteUrl, itemPath });
|
|
5
6
|
// options.cardTransformers has an object for each card that has a name and multiple
|
|
6
7
|
// transformer functions. By collecting the functions we need into a named object it
|
|
7
8
|
// reduces the need to loop through and find the transformer for each card later on
|
|
@@ -10,24 +11,20 @@ function mobiledocTransform(serializedMobiledoc, siteUrl, transformFunction, ite
|
|
|
10
11
|
cardTransformers[cardTransformer.name] = cardTransformer[options.transformType];
|
|
11
12
|
});
|
|
12
13
|
delete options.cardTransformers;
|
|
13
|
-
|
|
14
14
|
// function only accepts serialized mobiledoc so there's no chance of accidentally
|
|
15
15
|
// modifying pass-by-reference objects
|
|
16
16
|
const mobiledoc = JSON.parse(serializedMobiledoc);
|
|
17
|
-
|
|
18
17
|
// any mobiledoc links will have an 'a' markup with an 'href' attribute
|
|
19
18
|
(mobiledoc.markups || []).forEach((markup) => {
|
|
20
19
|
if (markup[0] === 'a' && markup[1]) {
|
|
21
20
|
// mobiledoc markup attrs are in an array like ['key', 'value', 'key2', 'value2']
|
|
22
21
|
// we only care about the href attr so loop through and find it so we can get the idx of it's value
|
|
23
22
|
let hrefIndex = -1;
|
|
24
|
-
|
|
25
23
|
markup[1].forEach((attr, index) => {
|
|
26
24
|
if (attr === 'href') {
|
|
27
25
|
hrefIndex = index + 1;
|
|
28
26
|
}
|
|
29
27
|
});
|
|
30
|
-
|
|
31
28
|
if (hrefIndex !== -1) {
|
|
32
29
|
const transformedUrl = transformFunction(markup[1][hrefIndex], siteUrl, itemPath, options);
|
|
33
30
|
if (transformedUrl) {
|
|
@@ -36,7 +33,6 @@ function mobiledocTransform(serializedMobiledoc, siteUrl, transformFunction, ite
|
|
|
36
33
|
}
|
|
37
34
|
}
|
|
38
35
|
});
|
|
39
|
-
|
|
40
36
|
// any other urls will be within card payloads. We can't know what format
|
|
41
37
|
// cards may contain so we sub out to card-specific transform functions that
|
|
42
38
|
// are passed in as options from the consuming application.
|
|
@@ -48,8 +44,6 @@ function mobiledocTransform(serializedMobiledoc, siteUrl, transformFunction, ite
|
|
|
48
44
|
card[1] = transformedPayload;
|
|
49
45
|
}
|
|
50
46
|
});
|
|
51
|
-
|
|
52
47
|
return JSON.stringify(mobiledoc);
|
|
53
48
|
}
|
|
54
|
-
|
|
55
49
|
module.exports = mobiledocTransform;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
declare const absoluteToTransformReady: any;
|
|
2
|
+
declare const escapeRegExp: any;
|
|
3
|
+
declare function buildLinkRegex(rootUrl: any, options?: {}): RegExp | null;
|
|
4
|
+
declare const plaintextAbsoluteToTransformReady: (plaintext: any, rootUrl: any, itemPath: any, options: any) => any;
|
|
@@ -1,26 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// @ts-nocheck
|
|
1
3
|
const absoluteToTransformReady = require('./absolute-to-transform-ready');
|
|
2
|
-
|
|
3
|
-
function
|
|
4
|
-
|
|
4
|
+
const { escapeRegExp } = require('./build-early-exit-match');
|
|
5
|
+
function buildLinkRegex(rootUrl, options = {}) {
|
|
6
|
+
// Build a regex that matches links from ANY configured base URL (site + CDNs)
|
|
7
|
+
const baseUrls = [rootUrl, options.imageBaseUrl, options.filesBaseUrl, options.mediaBaseUrl]
|
|
8
|
+
.filter(Boolean);
|
|
9
|
+
const patterns = baseUrls.map((baseUrl) => {
|
|
10
|
+
const parsed = new URL(baseUrl);
|
|
11
|
+
const escapedUrl = escapeRegExp(`${parsed.hostname}${parsed.pathname.replace(/\/$/, '')}`);
|
|
12
|
+
return escapedUrl;
|
|
13
|
+
});
|
|
14
|
+
if (!patterns.length) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
const pattern = patterns.length === 1 ? patterns[0] : `(?:${patterns.join('|')})`;
|
|
18
|
+
return new RegExp(` \\[(https?://${pattern}.*?)\\]`, 'g');
|
|
5
19
|
}
|
|
6
|
-
|
|
7
20
|
const plaintextAbsoluteToTransformReady = function plaintextAbsoluteToTransformReady(plaintext, rootUrl, itemPath, options) {
|
|
8
21
|
// itemPath is optional, if it's an object may be the options param instead
|
|
9
22
|
if (typeof itemPath === 'object' && !options) {
|
|
10
23
|
options = itemPath;
|
|
11
24
|
itemPath = null;
|
|
12
25
|
}
|
|
13
|
-
|
|
14
26
|
// plaintext links look like "Link title [url]"
|
|
15
27
|
// those links are all we care about so we can do a fast regex here
|
|
16
|
-
const
|
|
17
|
-
const escapedRootUrl = escapeRegExp(`${rootURL.hostname}${rootURL.pathname.replace(/\/$/, '')}`);
|
|
18
|
-
const linkRegex = new RegExp(` \\[(https?://${escapedRootUrl}.*?)\\]`, 'g');
|
|
19
|
-
|
|
28
|
+
const linkRegex = buildLinkRegex(rootUrl, options);
|
|
20
29
|
return plaintext.replace(linkRegex, function (fullMatch, url) {
|
|
21
30
|
const newUrl = absoluteToTransformReady(`${url}`, rootUrl, options);
|
|
22
31
|
return ` [${newUrl}]`;
|
|
23
32
|
});
|
|
24
33
|
};
|
|
25
|
-
|
|
26
34
|
module.exports = plaintextAbsoluteToTransformReady;
|