@platecms/delta-cast-util-to-hast 1.17.0 → 1.19.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/elements/text.ts +20 -6
- package/src/lib/index.spec.ts +78 -0
- package/src/lib/index.ts +6 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@platecms/delta-cast-util-to-hast",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.19.0",
|
|
4
4
|
"description": "Utility to convert CAST to HAST syntax tree.",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"publishConfig": {
|
|
@@ -17,8 +17,8 @@
|
|
|
17
17
|
"src/**/*"
|
|
18
18
|
],
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@platecms/delta-cast": "1.
|
|
21
|
-
"@platecms/delta-plate-resource-notation": "1.
|
|
20
|
+
"@platecms/delta-cast": "1.19.0",
|
|
21
|
+
"@platecms/delta-plate-resource-notation": "1.19.0",
|
|
22
22
|
"class-transformer": "0.5.1",
|
|
23
23
|
"hast": "1.0.0",
|
|
24
24
|
"reflect-metadata": "0.2.2",
|
package/src/lib/elements/text.ts
CHANGED
|
@@ -1,9 +1,23 @@
|
|
|
1
1
|
import { Text } from "@platecms/delta-cast";
|
|
2
|
-
import { Text as HastText } from "hast";
|
|
2
|
+
import { Element as HastElement, Text as HastText } from "hast";
|
|
3
3
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
/**
|
|
5
|
+
* A text node's value may contain soft returns ("\n", inserted with shift+enter in the
|
|
6
|
+
* editor). HTML collapses raw newlines in running text, so each one is rendered as an
|
|
7
|
+
* explicit <br> to keep the line break visible on the published page.
|
|
8
|
+
*/
|
|
9
|
+
export function text(node: Text): (HastElement | HastText)[] {
|
|
10
|
+
const lines = node.value.split("\n");
|
|
11
|
+
const lineBreak: HastElement = { type: "element", tagName: "br", properties: {}, children: [] };
|
|
12
|
+
|
|
13
|
+
return lines.flatMap((line, index): (HastElement | HastText)[] => {
|
|
14
|
+
const isLastLine = index === lines.length - 1;
|
|
15
|
+
|
|
16
|
+
if (line.length === 0) {
|
|
17
|
+
return isLastLine ? [{ type: "text", value: "" }] : [lineBreak];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const textNode: HastText = { type: "text", value: line };
|
|
21
|
+
return isLastLine ? [textNode] : [textNode, lineBreak];
|
|
22
|
+
});
|
|
9
23
|
}
|
package/src/lib/index.spec.ts
CHANGED
|
@@ -29,6 +29,84 @@ describe("single nodes", () => {
|
|
|
29
29
|
});
|
|
30
30
|
});
|
|
31
31
|
|
|
32
|
+
it("should convert a soft return within a paragraph into a <br> element", () => {
|
|
33
|
+
// Arrange
|
|
34
|
+
const cast: Root = c("root", [c("paragraph", "Line one\nLine two")]);
|
|
35
|
+
|
|
36
|
+
// Act
|
|
37
|
+
const expected = toHast(cast);
|
|
38
|
+
|
|
39
|
+
// Assert
|
|
40
|
+
expect(expected).toEqual({
|
|
41
|
+
type: "root",
|
|
42
|
+
children: [
|
|
43
|
+
{
|
|
44
|
+
type: "element",
|
|
45
|
+
tagName: "p",
|
|
46
|
+
properties: {},
|
|
47
|
+
children: [
|
|
48
|
+
{
|
|
49
|
+
type: "text",
|
|
50
|
+
value: "Line one",
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
type: "element",
|
|
54
|
+
tagName: "br",
|
|
55
|
+
properties: {},
|
|
56
|
+
children: [],
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
type: "text",
|
|
60
|
+
value: "Line two",
|
|
61
|
+
},
|
|
62
|
+
],
|
|
63
|
+
},
|
|
64
|
+
],
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("should convert multiple consecutive soft returns into consecutive <br> elements", () => {
|
|
69
|
+
// Arrange
|
|
70
|
+
const cast: Root = c("root", [c("paragraph", "Line one\n\nLine two")]);
|
|
71
|
+
|
|
72
|
+
// Act
|
|
73
|
+
const expected = toHast(cast);
|
|
74
|
+
|
|
75
|
+
// Assert
|
|
76
|
+
expect(expected).toEqual({
|
|
77
|
+
type: "root",
|
|
78
|
+
children: [
|
|
79
|
+
{
|
|
80
|
+
type: "element",
|
|
81
|
+
tagName: "p",
|
|
82
|
+
properties: {},
|
|
83
|
+
children: [
|
|
84
|
+
{
|
|
85
|
+
type: "text",
|
|
86
|
+
value: "Line one",
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
type: "element",
|
|
90
|
+
tagName: "br",
|
|
91
|
+
properties: {},
|
|
92
|
+
children: [],
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
type: "element",
|
|
96
|
+
tagName: "br",
|
|
97
|
+
properties: {},
|
|
98
|
+
children: [],
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
type: "text",
|
|
102
|
+
value: "Line two",
|
|
103
|
+
},
|
|
104
|
+
],
|
|
105
|
+
},
|
|
106
|
+
],
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
|
|
32
110
|
it("should convert a heading from cast to hast", () => {
|
|
33
111
|
// Arrange
|
|
34
112
|
const cast: Root = c("root", [c("heading", "Hello, world!", { level: 1 })]);
|
package/src/lib/index.ts
CHANGED
|
@@ -55,11 +55,15 @@ const one = zwitch("type", {
|
|
|
55
55
|
|
|
56
56
|
export function all(nodes: Content[], state: object = {}, transform: NodeTransformers = {}): HastElement[] {
|
|
57
57
|
let index = -1;
|
|
58
|
-
const results = [];
|
|
58
|
+
const results: HastElement[] = [];
|
|
59
59
|
|
|
60
60
|
while (++index < nodes.length) {
|
|
61
61
|
const result = one(nodes[index], state, transform);
|
|
62
|
-
|
|
62
|
+
if (Array.isArray(result)) {
|
|
63
|
+
results.push(...result);
|
|
64
|
+
} else {
|
|
65
|
+
results.push(result);
|
|
66
|
+
}
|
|
63
67
|
}
|
|
64
68
|
|
|
65
69
|
return results;
|