@platecms/delta-cast-util-to-hast 1.18.0 → 1.20.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/README.md +9 -2
- package/package.json +3 -3
- package/src/lib/elements/contentValue.ts +2 -1
- package/src/lib/elements/highlight.ts +13 -0
- package/src/lib/elements/index.ts +6 -2
- package/src/lib/elements/inlineCode.ts +14 -0
- package/src/lib/elements/{strikehrough.ts → strikethrough.ts} +1 -1
- package/src/lib/index.spec.ts +62 -3
- package/src/lib/index.ts +6 -2
package/README.md
CHANGED
|
@@ -114,7 +114,9 @@ yields (hast):
|
|
|
114
114
|
{
|
|
115
115
|
"type": "element",
|
|
116
116
|
"tagName": "span",
|
|
117
|
-
"properties": {
|
|
117
|
+
"properties": {
|
|
118
|
+
"dataPrn": "prn:plate-dev:a7734468-b894-4191-83b1-7d218f8c9922:cms:content-value:b2119c94-24e6-4358-b557-c705dcd19d1d"
|
|
119
|
+
},
|
|
118
120
|
"children": [
|
|
119
121
|
{
|
|
120
122
|
"type": "text",
|
|
@@ -130,5 +132,10 @@ yields (hast):
|
|
|
130
132
|
|
|
131
133
|
or yields (html):
|
|
132
134
|
```html
|
|
133
|
-
<p>My first <span>content</span></p>
|
|
135
|
+
<p>My first <span data-prn="prn:plate-dev:a7734468-b894-4191-83b1-7d218f8c9922:cms:content-value:b2119c94-24e6-4358-b557-c705dcd19d1d">content</span></p>
|
|
134
136
|
```
|
|
137
|
+
|
|
138
|
+
The `data-prn` attribute marks the span as an interpolated content value, so
|
|
139
|
+
[cast-util-from-hast](../cast-util-from-hast/README.md) can turn it back into a content value reference.
|
|
140
|
+
[cast-util-to-html](../cast-util-to-html/README.md) sanitizes its output, which removes the attribute, so published
|
|
141
|
+
HTML does not expose PRNs: it yields `<p>My first <span>content</span></p>`.
|
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.20.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.20.0",
|
|
21
|
+
"@platecms/delta-plate-resource-notation": "1.20.0",
|
|
22
22
|
"class-transformer": "0.5.1",
|
|
23
23
|
"hast": "1.0.0",
|
|
24
24
|
"reflect-metadata": "0.2.2",
|
|
@@ -9,7 +9,8 @@ export function contentValue(node: InterpolatedContentValue, state: object, tran
|
|
|
9
9
|
return {
|
|
10
10
|
type: "element",
|
|
11
11
|
tagName: "span",
|
|
12
|
-
|
|
12
|
+
// Marks the span as an interpolated content value, so cast-util-from-hast can turn it back into one
|
|
13
|
+
properties: { dataPrn: node.prn },
|
|
13
14
|
children: transformedNode.value ? all(transformedNode.value.children, state, transform) : [],
|
|
14
15
|
};
|
|
15
16
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Highlight } from "@platecms/delta-cast";
|
|
2
|
+
import { Element as HastElement } from "hast";
|
|
3
|
+
import { all } from "..";
|
|
4
|
+
import { NodeTransformers } from "../transformers";
|
|
5
|
+
|
|
6
|
+
export function highlight(node: Highlight, state: object = {}, transform: NodeTransformers = {}): HastElement {
|
|
7
|
+
return {
|
|
8
|
+
type: "element",
|
|
9
|
+
tagName: "mark",
|
|
10
|
+
properties: {},
|
|
11
|
+
children: all(node.children, state, transform),
|
|
12
|
+
};
|
|
13
|
+
}
|
|
@@ -6,7 +6,9 @@ import { paragraph } from "./paragraph";
|
|
|
6
6
|
import { bold } from "./bold";
|
|
7
7
|
import { italic } from "./italic";
|
|
8
8
|
import { underline } from "./underline";
|
|
9
|
-
import {
|
|
9
|
+
import { strikethrough } from "./strikethrough";
|
|
10
|
+
import { highlight } from "./highlight";
|
|
11
|
+
import { inlineCode } from "./inlineCode";
|
|
10
12
|
import { text } from "./text";
|
|
11
13
|
import { blockquote } from "./blockquote";
|
|
12
14
|
import { link } from "./link";
|
|
@@ -22,7 +24,9 @@ export {
|
|
|
22
24
|
bold,
|
|
23
25
|
italic,
|
|
24
26
|
underline,
|
|
25
|
-
|
|
27
|
+
strikethrough,
|
|
28
|
+
highlight,
|
|
29
|
+
inlineCode,
|
|
26
30
|
text,
|
|
27
31
|
blockquote,
|
|
28
32
|
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { InlineCode } from "@platecms/delta-cast";
|
|
2
|
+
import { Element as HastElement } from "hast";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Inline code keeps its value as one text node: unlike running text, its newlines are not turned into <br>.
|
|
6
|
+
*/
|
|
7
|
+
export function inlineCode(node: InlineCode): HastElement {
|
|
8
|
+
return {
|
|
9
|
+
type: "element",
|
|
10
|
+
tagName: "code",
|
|
11
|
+
properties: {},
|
|
12
|
+
children: [{ type: "text", value: node.value }],
|
|
13
|
+
};
|
|
14
|
+
}
|
|
@@ -3,7 +3,7 @@ import { Element as HastElement } from "hast";
|
|
|
3
3
|
import { all } from "..";
|
|
4
4
|
import { NodeTransformers } from "../transformers";
|
|
5
5
|
|
|
6
|
-
export function
|
|
6
|
+
export function strikethrough(node: Strikethrough, state: object = {}, transform: NodeTransformers = {}): HastElement {
|
|
7
7
|
return {
|
|
8
8
|
type: "element",
|
|
9
9
|
tagName: "s",
|
package/src/lib/index.spec.ts
CHANGED
|
@@ -249,7 +249,7 @@ describe("single nodes", () => {
|
|
|
249
249
|
children: [
|
|
250
250
|
{
|
|
251
251
|
type: "element",
|
|
252
|
-
properties: {},
|
|
252
|
+
properties: { dataPrn: "prn:content-value" },
|
|
253
253
|
tagName: "span",
|
|
254
254
|
children: [
|
|
255
255
|
{
|
|
@@ -262,6 +262,65 @@ describe("single nodes", () => {
|
|
|
262
262
|
});
|
|
263
263
|
});
|
|
264
264
|
|
|
265
|
+
it.each([
|
|
266
|
+
["strikethrough", "s"],
|
|
267
|
+
["highlight", "mark"],
|
|
268
|
+
] as const)("should convert %s from cast to hast as %s", (type, tagName) => {
|
|
269
|
+
// Arrange
|
|
270
|
+
const cast: Root = c("root", [c("paragraph", [c(type, "Hello, world!")])]);
|
|
271
|
+
|
|
272
|
+
// Act
|
|
273
|
+
const result = toHast(cast);
|
|
274
|
+
|
|
275
|
+
// Assert
|
|
276
|
+
expect(result).toEqual({
|
|
277
|
+
type: "root",
|
|
278
|
+
children: [
|
|
279
|
+
{
|
|
280
|
+
type: "element",
|
|
281
|
+
tagName: "p",
|
|
282
|
+
properties: {},
|
|
283
|
+
children: [
|
|
284
|
+
{
|
|
285
|
+
type: "element",
|
|
286
|
+
tagName,
|
|
287
|
+
properties: {},
|
|
288
|
+
children: [{ type: "text", value: "Hello, world!" }],
|
|
289
|
+
},
|
|
290
|
+
],
|
|
291
|
+
},
|
|
292
|
+
],
|
|
293
|
+
});
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
it("should convert inline code from cast to hast, keeping newlines as text", () => {
|
|
297
|
+
// Arrange
|
|
298
|
+
const cast: Root = c("root", [c("paragraph", [c("inlineCode", { value: "a\nb" })])]);
|
|
299
|
+
|
|
300
|
+
// Act
|
|
301
|
+
const result = toHast(cast);
|
|
302
|
+
|
|
303
|
+
// Assert
|
|
304
|
+
expect(result).toEqual({
|
|
305
|
+
type: "root",
|
|
306
|
+
children: [
|
|
307
|
+
{
|
|
308
|
+
type: "element",
|
|
309
|
+
tagName: "p",
|
|
310
|
+
properties: {},
|
|
311
|
+
children: [
|
|
312
|
+
{
|
|
313
|
+
type: "element",
|
|
314
|
+
tagName: "code",
|
|
315
|
+
properties: {},
|
|
316
|
+
children: [{ type: "text", value: "a\nb" }],
|
|
317
|
+
},
|
|
318
|
+
],
|
|
319
|
+
},
|
|
320
|
+
],
|
|
321
|
+
});
|
|
322
|
+
});
|
|
323
|
+
|
|
265
324
|
describe("links", () => {
|
|
266
325
|
it("should convert an external link from cast to hast", () => {
|
|
267
326
|
// Arrange
|
|
@@ -423,7 +482,7 @@ describe("nested nodes", () => {
|
|
|
423
482
|
},
|
|
424
483
|
{
|
|
425
484
|
type: "element",
|
|
426
|
-
properties: {},
|
|
485
|
+
properties: { dataPrn: "prn:content-value:content-value" },
|
|
427
486
|
tagName: "span",
|
|
428
487
|
children: [
|
|
429
488
|
{
|
|
@@ -459,7 +518,7 @@ describe("wihtout transformations", () => {
|
|
|
459
518
|
children: [
|
|
460
519
|
{
|
|
461
520
|
type: "element",
|
|
462
|
-
properties: {},
|
|
521
|
+
properties: { dataPrn: "prn:content-value" },
|
|
463
522
|
tagName: "span",
|
|
464
523
|
children: [
|
|
465
524
|
{
|
package/src/lib/index.ts
CHANGED
|
@@ -7,12 +7,14 @@ import {
|
|
|
7
7
|
code,
|
|
8
8
|
contentValue,
|
|
9
9
|
heading,
|
|
10
|
+
highlight,
|
|
11
|
+
inlineCode,
|
|
10
12
|
italic,
|
|
11
13
|
link,
|
|
12
14
|
list,
|
|
13
15
|
listItem,
|
|
14
16
|
paragraph,
|
|
15
|
-
|
|
17
|
+
strikethrough,
|
|
16
18
|
text,
|
|
17
19
|
underline,
|
|
18
20
|
} from "./elements";
|
|
@@ -44,7 +46,9 @@ const one = zwitch("type", {
|
|
|
44
46
|
bold,
|
|
45
47
|
italic,
|
|
46
48
|
underline,
|
|
47
|
-
|
|
49
|
+
strikethrough,
|
|
50
|
+
highlight,
|
|
51
|
+
inlineCode,
|
|
48
52
|
text,
|
|
49
53
|
externalLink: link,
|
|
50
54
|
internalLink: link,
|