@pallasoft/ps-utilities 2.16.0 → 2.17.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/dist/common/constants/index.d.ts +1 -0
- package/dist/common/constants/index.js +1 -0
- package/dist/common/constants/portable-text-config.d.ts +19 -0
- package/dist/common/constants/portable-text-config.js +20 -0
- package/dist/common/index.d.ts +1 -0
- package/dist/common/index.js +1 -0
- package/dist/common/portable-text-service.d.ts +9 -0
- package/dist/common/portable-text-service.js +110 -0
- package/package.json +3 -1
|
@@ -32,3 +32,4 @@ __exportStar(require("./http-header"), exports);
|
|
|
32
32
|
__exportStar(require("./http-method"), exports);
|
|
33
33
|
__exportStar(require("./relative-time-config"), exports);
|
|
34
34
|
__exportStar(require("./request-client-config"), exports);
|
|
35
|
+
__exportStar(require("./portable-text-config"), exports);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export declare const PortableTextConfig: {
|
|
2
|
+
readonly linkRel: "noopener noreferrer";
|
|
3
|
+
readonly linkTarget: "_blank";
|
|
4
|
+
readonly paragraphSeparator: "\n\n";
|
|
5
|
+
readonly lineSeparator: "\n";
|
|
6
|
+
readonly paragraphSplitPattern: RegExp;
|
|
7
|
+
readonly lineBreakPattern: RegExp;
|
|
8
|
+
readonly bulletPrefix: "- ";
|
|
9
|
+
readonly quotePrefix: "> ";
|
|
10
|
+
readonly numberSuffix: ". ";
|
|
11
|
+
readonly keyPrefix: {
|
|
12
|
+
readonly block: "b";
|
|
13
|
+
readonly span: "s";
|
|
14
|
+
};
|
|
15
|
+
readonly normalStyle: "normal";
|
|
16
|
+
readonly quoteStyle: "blockquote";
|
|
17
|
+
readonly bulletItem: "bullet";
|
|
18
|
+
readonly numberItem: "number";
|
|
19
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PortableTextConfig = void 0;
|
|
4
|
+
var ps_models_1 = require("@pallasoft/ps-models");
|
|
5
|
+
exports.PortableTextConfig = {
|
|
6
|
+
linkRel: 'noopener noreferrer',
|
|
7
|
+
linkTarget: '_blank',
|
|
8
|
+
paragraphSeparator: '\n\n',
|
|
9
|
+
lineSeparator: '\n',
|
|
10
|
+
paragraphSplitPattern: /\n[ \t]*\n/,
|
|
11
|
+
lineBreakPattern: /\r\n?/g,
|
|
12
|
+
bulletPrefix: '- ',
|
|
13
|
+
quotePrefix: '> ',
|
|
14
|
+
numberSuffix: '. ',
|
|
15
|
+
keyPrefix: { block: 'b', span: 's' },
|
|
16
|
+
normalStyle: ps_models_1.Common.PortableTextConfig.styles[0],
|
|
17
|
+
quoteStyle: ps_models_1.Common.PortableTextConfig.styles[1],
|
|
18
|
+
bulletItem: ps_models_1.Common.PortableTextConfig.listItems[0],
|
|
19
|
+
numberItem: ps_models_1.Common.PortableTextConfig.listItems[1],
|
|
20
|
+
};
|
package/dist/common/index.d.ts
CHANGED
package/dist/common/index.js
CHANGED
|
@@ -40,3 +40,4 @@ __exportStar(require("./error-response-service"), exports);
|
|
|
40
40
|
__exportStar(require("./relative-time-service"), exports);
|
|
41
41
|
__exportStar(require("./request-client-service"), exports);
|
|
42
42
|
__exportStar(require("./json-service"), exports);
|
|
43
|
+
__exportStar(require("./portable-text-service"), exports);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { PortableTextBlock } from '@portabletext/types';
|
|
2
|
+
export declare class PortableTextService {
|
|
3
|
+
static toPlainText(blocks: PortableTextBlock[]): string;
|
|
4
|
+
static fromPlainText(text: string): PortableTextBlock[];
|
|
5
|
+
static isEmpty(blocks: PortableTextBlock[] | null | undefined): boolean;
|
|
6
|
+
static toHtml(blocks: PortableTextBlock[]): string;
|
|
7
|
+
private static blockText;
|
|
8
|
+
private static isSafeLink;
|
|
9
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PortableTextService = void 0;
|
|
4
|
+
var ps_models_1 = require("@pallasoft/ps-models");
|
|
5
|
+
var to_html_1 = require("@portabletext/to-html");
|
|
6
|
+
var portable_text_config_1 = require("./constants/portable-text-config");
|
|
7
|
+
var PortableTextService = /** @class */ (function () {
|
|
8
|
+
function PortableTextService() {
|
|
9
|
+
}
|
|
10
|
+
PortableTextService.toPlainText = function (blocks) {
|
|
11
|
+
var _this = this;
|
|
12
|
+
var lines = [];
|
|
13
|
+
var counters = {};
|
|
14
|
+
var previousWasList = false;
|
|
15
|
+
blocks.forEach(function (block) {
|
|
16
|
+
if (block._type !== ps_models_1.Common.PortableTextConfig.blockType)
|
|
17
|
+
return;
|
|
18
|
+
var text = _this.blockText(block);
|
|
19
|
+
var level = typeof block.level === 'number' ? block.level : 1;
|
|
20
|
+
var isNumber = block.listItem === portable_text_config_1.PortableTextConfig.numberItem;
|
|
21
|
+
var isBullet = block.listItem === portable_text_config_1.PortableTextConfig.bulletItem;
|
|
22
|
+
var isList = isNumber || isBullet;
|
|
23
|
+
if (!isList) {
|
|
24
|
+
counters[level] = 0;
|
|
25
|
+
if (lines.length > 0)
|
|
26
|
+
lines.push(portable_text_config_1.PortableTextConfig.paragraphSeparator);
|
|
27
|
+
lines.push(block.style === portable_text_config_1.PortableTextConfig.quoteStyle
|
|
28
|
+
? portable_text_config_1.PortableTextConfig.quotePrefix + text
|
|
29
|
+
: text);
|
|
30
|
+
previousWasList = false;
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
counters[level] = isNumber
|
|
34
|
+
? (typeof counters[level] === 'number' ? counters[level] : 0) + 1
|
|
35
|
+
: 0;
|
|
36
|
+
var prefix = isNumber
|
|
37
|
+
? "".concat(counters[level]).concat(portable_text_config_1.PortableTextConfig.numberSuffix)
|
|
38
|
+
: portable_text_config_1.PortableTextConfig.bulletPrefix;
|
|
39
|
+
var indent = ' '.repeat(level - 1);
|
|
40
|
+
if (lines.length > 0)
|
|
41
|
+
lines.push(previousWasList ? portable_text_config_1.PortableTextConfig.lineSeparator : portable_text_config_1.PortableTextConfig.paragraphSeparator);
|
|
42
|
+
lines.push(indent + prefix + text);
|
|
43
|
+
previousWasList = true;
|
|
44
|
+
});
|
|
45
|
+
return lines.join('');
|
|
46
|
+
};
|
|
47
|
+
PortableTextService.fromPlainText = function (text) {
|
|
48
|
+
return text
|
|
49
|
+
.replace(portable_text_config_1.PortableTextConfig.lineBreakPattern, portable_text_config_1.PortableTextConfig.lineSeparator)
|
|
50
|
+
.split(portable_text_config_1.PortableTextConfig.paragraphSplitPattern)
|
|
51
|
+
.map(function (paragraph) { return paragraph.trim(); })
|
|
52
|
+
.filter(function (paragraph) { return paragraph.length > 0; })
|
|
53
|
+
.map(function (paragraph, index) { return ({
|
|
54
|
+
_type: ps_models_1.Common.PortableTextConfig.blockType,
|
|
55
|
+
_key: "".concat(portable_text_config_1.PortableTextConfig.keyPrefix.block).concat(index),
|
|
56
|
+
style: portable_text_config_1.PortableTextConfig.normalStyle,
|
|
57
|
+
markDefs: [],
|
|
58
|
+
children: [
|
|
59
|
+
{
|
|
60
|
+
_type: ps_models_1.Common.PortableTextConfig.spanType,
|
|
61
|
+
_key: "".concat(portable_text_config_1.PortableTextConfig.keyPrefix.span).concat(index),
|
|
62
|
+
text: paragraph,
|
|
63
|
+
marks: [],
|
|
64
|
+
},
|
|
65
|
+
],
|
|
66
|
+
}); });
|
|
67
|
+
};
|
|
68
|
+
PortableTextService.isEmpty = function (blocks) {
|
|
69
|
+
var _this = this;
|
|
70
|
+
return (!Array.isArray(blocks) || blocks.every(function (block) { return _this.blockText(block).trim().length === 0; }));
|
|
71
|
+
};
|
|
72
|
+
PortableTextService.toHtml = function (blocks) {
|
|
73
|
+
var _this = this;
|
|
74
|
+
return (0, to_html_1.toHTML)(blocks, {
|
|
75
|
+
onMissingComponent: false,
|
|
76
|
+
components: {
|
|
77
|
+
marks: {
|
|
78
|
+
link: function (_a) {
|
|
79
|
+
var children = _a.children, value = _a.value;
|
|
80
|
+
var href = typeof (value === null || value === void 0 ? void 0 : value.href) === 'string' ? value.href : '';
|
|
81
|
+
return _this.isSafeLink(href)
|
|
82
|
+
? "<a href=\"".concat((0, to_html_1.escapeHTML)(href), "\" rel=\"").concat(portable_text_config_1.PortableTextConfig.linkRel, "\" target=\"").concat(portable_text_config_1.PortableTextConfig.linkTarget, "\">").concat(children, "</a>")
|
|
83
|
+
: children;
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
PortableTextService.blockText = function (block) {
|
|
90
|
+
return Array.isArray(block.children)
|
|
91
|
+
? block.children
|
|
92
|
+
.map(function (child) {
|
|
93
|
+
return child._type === ps_models_1.Common.PortableTextConfig.spanType && typeof child.text === 'string'
|
|
94
|
+
? child.text
|
|
95
|
+
: '';
|
|
96
|
+
})
|
|
97
|
+
.join('')
|
|
98
|
+
: '';
|
|
99
|
+
};
|
|
100
|
+
PortableTextService.isSafeLink = function (href) {
|
|
101
|
+
try {
|
|
102
|
+
return ps_models_1.Common.PortableTextConfig.linkProtocols.includes(new URL(href).protocol);
|
|
103
|
+
}
|
|
104
|
+
catch (_a) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
return PortableTextService;
|
|
109
|
+
}());
|
|
110
|
+
exports.PortableTextService = PortableTextService;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pallasoft/ps-utilities",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.17.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -77,6 +77,8 @@
|
|
|
77
77
|
},
|
|
78
78
|
"dependencies": {
|
|
79
79
|
"@esbuild-plugins/tsconfig-paths": "^0.1.2",
|
|
80
|
+
"@pallasoft/ps-models": "^1.4.0",
|
|
81
|
+
"@portabletext/to-html": "^4.0.1",
|
|
80
82
|
"dayjs": "^1.11.19",
|
|
81
83
|
"esbuild": "^0.27.0",
|
|
82
84
|
"libphonenumber-js": "^1.13.14"
|