@platecms/delta-cast-util-to-plaintext 0.4.1 → 0.7.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@platecms/delta-cast-util-to-plaintext",
3
- "version": "0.4.1",
3
+ "version": "0.7.0",
4
4
  "description": "Utility to convert CAST trees to plaintext.",
5
5
  "license": "UNLICENSED",
6
6
  "publishConfig": {
@@ -17,13 +17,12 @@
17
17
  "src/**/*"
18
18
  ],
19
19
  "dependencies": {
20
- "@platecms/delta-cast": "0.4.1",
21
- "@platecms/delta-plate-resource-notation": "0.4.1",
20
+ "@platecms/delta-cast": "0.7.0",
21
+ "@platecms/delta-plate-resource-notation": "0.7.0",
22
22
  "class-transformer": "0.5.1",
23
23
  "reflect-metadata": "0.2.2",
24
24
  "tslib": "2.8.1",
25
25
  "zwitch": "2.0.4",
26
26
  "lodash": "4.17.21"
27
- },
28
- "type": "commonjs"
27
+ }
29
28
  }
@@ -0,0 +1,132 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { toPlaintext } from "./index";
3
+ import { c } from "@platecms/delta-castscript";
4
+ import { Root } from "@platecms/delta-cast";
5
+
6
+ describe("single nodes", () => {
7
+ it("should convert a paragraph from cast to plaintext", () => {
8
+ // Arrange
9
+ const cast: Root = c("root", [c("paragraph", "Hello, world!")]);
10
+
11
+ // Act
12
+ const expected = toPlaintext(cast);
13
+
14
+ // Assert
15
+ expect(expected).toEqual("Hello, world!");
16
+ });
17
+
18
+ it("should convert a heading from cast to plaintext", () => {
19
+ // Arrange
20
+ const cast: Root = c("root", [c("heading", "HELLO, WORLD!", { level: 1 })]);
21
+
22
+ // Act
23
+ const expected = toPlaintext(cast);
24
+
25
+ // Assert
26
+ expect(expected).toEqual("HELLO, WORLD!");
27
+ });
28
+
29
+ it("should convert a list from cast to plaintext", () => {
30
+ // Arrange
31
+ const cast: Root = c("root", [
32
+ c("list", { ordered: true }, [c("listItem", "First item"), c("listItem", "Second item")]),
33
+ ]);
34
+
35
+ // Act
36
+ const expected = toPlaintext(cast);
37
+
38
+ // Assert
39
+ expect(expected).toEqual("First item Second item");
40
+ });
41
+
42
+ it("should convert a blockquote from from cast to plaintext", () => {
43
+ // Arrange
44
+ const cast: Root = c("root", [c("blockquote", "Block quote")]);
45
+
46
+ // Act
47
+ const expected = toPlaintext(cast);
48
+
49
+ // Assert
50
+ expect(expected).toEqual("Block quote");
51
+ });
52
+
53
+ it("should convert code from cast to plaintext", () => {
54
+ // Arrange
55
+ const cast: Root = c("root", [c("code", [c("text", "Code block")])]);
56
+
57
+ // Act
58
+ const expected = toPlaintext(cast);
59
+
60
+ // Assert
61
+ expect(expected).toEqual("Code block");
62
+ });
63
+
64
+ it("should convert a content value from cast to plaintext", () => {
65
+ // Arrange
66
+ const cast: Root = c("root", [
67
+ c(
68
+ "contentValue",
69
+ {
70
+ prn: "prn:content-value",
71
+ value: c("root", [c("paragraph", "Content Value")]),
72
+ },
73
+ [c("text", "")],
74
+ ),
75
+ ]);
76
+
77
+ // Act
78
+ const expected = toPlaintext(cast);
79
+
80
+ // Assert
81
+ expect(expected).toEqual("Content Value");
82
+ });
83
+
84
+ it("should convert a link from cast to plaintext", () => {
85
+ // Arrange
86
+ const cast: Root = c("root", [c("externalLink", { url: "https://www.google.com" }, [c("text", "Google")])]);
87
+
88
+ // Act
89
+ const expected = toPlaintext(cast);
90
+
91
+ // Assert
92
+ expect(expected).toEqual("Google");
93
+ });
94
+
95
+ it("should convert an internal link from cast to plaintext", () => {
96
+ // Arrange
97
+ const cast: Root = c("root", [
98
+ c("internalLink", { url: "https://www.google.com#anchor-name", prn: "prn:grid-placement:grid-placement" }, [
99
+ c("text", "Google"),
100
+ ]),
101
+ ]);
102
+
103
+ // Act
104
+ const expected = toPlaintext(cast);
105
+
106
+ // Assert
107
+ expect(expected).toEqual("Google");
108
+ });
109
+ });
110
+
111
+ describe("nested nodes", () => {
112
+ it("should convert a nested tree from cast to plaintext", () => {
113
+ // Arrange
114
+ const tree: Root = c("root", [
115
+ c("list", { ordered: false }, [c("listItem", [c("bold", "List item")])]),
116
+ c("paragraph", [
117
+ c("text", "Hello, world!"),
118
+ c("contentValue", {
119
+ prn: "prn:content-value:content-value",
120
+ value: c("root", [c("paragraph", "Content Value")]),
121
+ }),
122
+ c("externalLink", { url: "https://www.google.com" }, [c("text", "Google")]),
123
+ ]),
124
+ ]);
125
+
126
+ // Act
127
+ const expected = toPlaintext(tree);
128
+
129
+ // Assert
130
+ expect(expected).toEqual("List item Hello, world! Content Value Google");
131
+ });
132
+ });
@@ -0,0 +1,132 @@
1
+ import {
2
+ Blockquote,
3
+ Bold,
4
+ Root as CastRoot,
5
+ Code,
6
+ Content,
7
+ ExternalLink,
8
+ Heading,
9
+ InternalLink,
10
+ InterpolatedContentValue,
11
+ Italic,
12
+ List,
13
+ ListItem,
14
+ Paragraph,
15
+ Strikethrough,
16
+ Text,
17
+ Underline,
18
+ } from "@platecms/delta-cast";
19
+ import { zwitch } from "zwitch";
20
+
21
+ // default separator for joining nested nodes, avoids the default of joining with a comma
22
+ const separator = " ";
23
+
24
+ export function toPlaintext(value: CastRoot): string {
25
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
26
+ return all(value.children, {}).join(separator);
27
+ }
28
+
29
+ const one = zwitch("type", {
30
+ handlers: {
31
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
32
+ heading,
33
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
34
+ list,
35
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
36
+ listItem,
37
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
38
+ blockquote,
39
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
40
+ code,
41
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
42
+ paragraph,
43
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
44
+ contentValue,
45
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
46
+ bold,
47
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
48
+ italic,
49
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
50
+ underline,
51
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
52
+ strikethrough,
53
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
54
+ text,
55
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
56
+ externalLink: link,
57
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
58
+ internalLink: link,
59
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
60
+ internalAnchorLink: link,
61
+ },
62
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
63
+ unknown: (value: unknown, _: object = {}) => defaultHandler(value as Content),
64
+ });
65
+
66
+ function all(nodes: Content[], state: object = {}): string[] {
67
+ let index = -1;
68
+ const results = [];
69
+
70
+ while (++index < nodes.length) {
71
+ const result = one(nodes[index], state);
72
+ results.push(result);
73
+ }
74
+
75
+ return results;
76
+ }
77
+
78
+ function heading(node: Heading, state: object = {}): string {
79
+ return all(node.children, state).join(separator);
80
+ }
81
+
82
+ function list(node: List, state: object = {}): string {
83
+ return all(node.children, state).join(separator);
84
+ }
85
+
86
+ function listItem(node: ListItem, state: object = {}): string {
87
+ return all(node.children, state).join(separator);
88
+ }
89
+
90
+ function code(node: Code, state: object = {}): string {
91
+ return all((node as Code & { children: Text[] }).children, state).join(separator);
92
+ }
93
+
94
+ function contentValue(node: InterpolatedContentValue): string {
95
+ return node.value ? toPlaintext(node.value) : "";
96
+ }
97
+
98
+ function paragraph(node: Paragraph, state: object = {}): string {
99
+ return all(node.children, state).join(separator);
100
+ }
101
+
102
+ function bold(node: Bold, state: object = {}): string {
103
+ return all(node.children, state).join(separator);
104
+ }
105
+
106
+ function italic(node: Italic, state: object = {}): string {
107
+ return all(node.children, state).join(separator);
108
+ }
109
+
110
+ function underline(node: Underline, state: object = {}): string {
111
+ return all(node.children, state).join(separator);
112
+ }
113
+
114
+ function strikethrough(node: Strikethrough, state: object = {}): string {
115
+ return all(node.children, state).join(separator);
116
+ }
117
+
118
+ function text(node: Text): string {
119
+ return node.value;
120
+ }
121
+
122
+ function blockquote(node: Blockquote): string {
123
+ return all(node.children).join(separator);
124
+ }
125
+
126
+ function link(node: ExternalLink | InternalLink): string {
127
+ return all(node.children).join(separator);
128
+ }
129
+
130
+ function defaultHandler(node: Content): string {
131
+ return `Unknown node type (${node.type})`;
132
+ }
package/src/index.js DELETED
@@ -1,5 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const tslib_1 = require("tslib");
4
- tslib_1.__exportStar(require("./lib/index"), exports);
5
- //# sourceMappingURL=index.js.map
package/src/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../packages/cast-util-to-plaintext/src/index.ts"],"names":[],"mappings":";;;AAAA,sDAA4B"}
@@ -1,2 +0,0 @@
1
- import { Root as CastRoot } from "@platecms/delta-cast";
2
- export declare function toPlaintext(value: CastRoot): string;
package/src/lib/index.js DELETED
@@ -1,80 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toPlaintext = toPlaintext;
4
- const zwitch_1 = require("zwitch");
5
- const separator = " ";
6
- function toPlaintext(value) {
7
- return all(value.children, {}).join(separator);
8
- }
9
- const one = (0, zwitch_1.zwitch)("type", {
10
- handlers: {
11
- heading,
12
- list,
13
- listItem,
14
- blockquote,
15
- code,
16
- paragraph,
17
- contentValue,
18
- bold,
19
- italic,
20
- underline,
21
- strikethrough,
22
- text,
23
- externalLink: link,
24
- internalLink: link,
25
- internalAnchorLink: link,
26
- },
27
- unknown: (value, _ = {}) => defaultHandler(value),
28
- });
29
- function all(nodes, state = {}) {
30
- let index = -1;
31
- const results = [];
32
- while (++index < nodes.length) {
33
- const result = one(nodes[index], state);
34
- results.push(result);
35
- }
36
- return results;
37
- }
38
- function heading(node, state = {}) {
39
- return all(node.children, state).join(separator);
40
- }
41
- function list(node, state = {}) {
42
- return all(node.children, state).join(separator);
43
- }
44
- function listItem(node, state = {}) {
45
- return all(node.children, state).join(separator);
46
- }
47
- function code(node, state = {}) {
48
- return all(node.children, state).join(separator);
49
- }
50
- function contentValue(node) {
51
- return node.value ? toPlaintext(node.value) : "";
52
- }
53
- function paragraph(node, state = {}) {
54
- return all(node.children, state).join(separator);
55
- }
56
- function bold(node, state = {}) {
57
- return all(node.children, state).join(separator);
58
- }
59
- function italic(node, state = {}) {
60
- return all(node.children, state).join(separator);
61
- }
62
- function underline(node, state = {}) {
63
- return all(node.children, state).join(separator);
64
- }
65
- function strikethrough(node, state = {}) {
66
- return all(node.children, state).join(separator);
67
- }
68
- function text(node) {
69
- return node.value;
70
- }
71
- function blockquote(node) {
72
- return all(node.children).join(separator);
73
- }
74
- function link(node) {
75
- return all(node.children).join(separator);
76
- }
77
- function defaultHandler(node) {
78
- return `Unknown node type (${node.type})`;
79
- }
80
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../packages/cast-util-to-plaintext/src/lib/index.ts"],"names":[],"mappings":";;AAuBA,kCAGC;AARD,mCAAgC;AAGhC,MAAM,SAAS,GAAG,GAAG,CAAC;AAEtB,SAAgB,WAAW,CAAC,KAAe;IAEzC,OAAO,GAAG,CAAC,KAAK,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;AACjD,CAAC;AAED,MAAM,GAAG,GAAG,IAAA,eAAM,EAAC,MAAM,EAAE;IACzB,QAAQ,EAAE;QAER,OAAO;QAEP,IAAI;QAEJ,QAAQ;QAER,UAAU;QAEV,IAAI;QAEJ,SAAS;QAET,YAAY;QAEZ,IAAI;QAEJ,MAAM;QAEN,SAAS;QAET,aAAa;QAEb,IAAI;QAEJ,YAAY,EAAE,IAAI;QAElB,YAAY,EAAE,IAAI;QAElB,kBAAkB,E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