@platecms/delta-cast-util-to-plaintext 0.13.0 → 1.2.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/package.json +3 -3
- package/src/lib/index.spec.ts +16 -0
- package/src/lib/index.ts +10 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platecms/delta-cast-util-to-plaintext",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Utility to convert CAST trees to plaintext.",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"publishConfig": {
|
|
@@ -17,8 +17,8 @@
|
|
|
17
17
|
"src/**/*"
|
|
18
18
|
],
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@platecms/delta-cast": "
|
|
21
|
-
"@platecms/delta-plate-resource-notation": "
|
|
20
|
+
"@platecms/delta-cast": "1.2.0",
|
|
21
|
+
"@platecms/delta-plate-resource-notation": "1.2.0",
|
|
22
22
|
"class-transformer": "0.5.1",
|
|
23
23
|
"reflect-metadata": "0.2.2",
|
|
24
24
|
"tslib": "2.8.1",
|
package/src/lib/index.spec.ts
CHANGED
|
@@ -106,6 +106,22 @@ describe("single nodes", () => {
|
|
|
106
106
|
// Assert
|
|
107
107
|
expect(expected).toEqual("Google");
|
|
108
108
|
});
|
|
109
|
+
|
|
110
|
+
it("should convert a content value suggestion from cast to plaintext", () => {
|
|
111
|
+
// Arrange
|
|
112
|
+
const cast: Root = c("root", [
|
|
113
|
+
c("contentValueSuggestion", {
|
|
114
|
+
prn: "prn:content-value:content-value",
|
|
115
|
+
value: c("root", [c("paragraph", "Hello, world!")]),
|
|
116
|
+
}),
|
|
117
|
+
]);
|
|
118
|
+
|
|
119
|
+
// Act
|
|
120
|
+
const expected = toPlaintext(cast);
|
|
121
|
+
|
|
122
|
+
// Assert
|
|
123
|
+
expect(expected).toEqual("Hello, world!");
|
|
124
|
+
});
|
|
109
125
|
});
|
|
110
126
|
|
|
111
127
|
describe("nested nodes", () => {
|
package/src/lib/index.ts
CHANGED
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
Root as CastRoot,
|
|
5
5
|
Code,
|
|
6
6
|
Content,
|
|
7
|
+
ContentValueSuggestion,
|
|
7
8
|
ExternalLink,
|
|
8
9
|
Heading,
|
|
9
10
|
InternalLink,
|
|
@@ -16,14 +17,15 @@ import {
|
|
|
16
17
|
Text,
|
|
17
18
|
Underline,
|
|
18
19
|
} from "@platecms/delta-cast";
|
|
20
|
+
import { isArray } from "lodash";
|
|
19
21
|
import { zwitch } from "zwitch";
|
|
20
22
|
|
|
21
23
|
// default separator for joining nested nodes, avoids the default of joining with a comma
|
|
22
24
|
const separator = " ";
|
|
23
25
|
|
|
24
|
-
export function toPlaintext(value: CastRoot): string {
|
|
26
|
+
export function toPlaintext(value: CastRoot | Content[]): string {
|
|
25
27
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
26
|
-
return all(value.children, {}).join(separator);
|
|
28
|
+
return all(isArray(value) ? value : value.children, {}).join(separator);
|
|
27
29
|
}
|
|
28
30
|
|
|
29
31
|
const one = zwitch("type", {
|
|
@@ -43,6 +45,8 @@ const one = zwitch("type", {
|
|
|
43
45
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
44
46
|
contentValue,
|
|
45
47
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
48
|
+
contentValueSuggestion,
|
|
49
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
46
50
|
bold,
|
|
47
51
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
48
52
|
italic,
|
|
@@ -95,6 +99,10 @@ function contentValue(node: InterpolatedContentValue): string {
|
|
|
95
99
|
return node.value ? toPlaintext(node.value) : "";
|
|
96
100
|
}
|
|
97
101
|
|
|
102
|
+
function contentValueSuggestion(node: ContentValueSuggestion): string {
|
|
103
|
+
return node.value ? toPlaintext(node.value) : "";
|
|
104
|
+
}
|
|
105
|
+
|
|
98
106
|
function paragraph(node: Paragraph, state: object = {}): string {
|
|
99
107
|
return all(node.children, state).join(separator);
|
|
100
108
|
}
|