document-ir 0.0.6 → 0.0.7
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 +2 -13
- package/esm/WhitespaceStretchingTransformer.d.ts +2 -2
- package/esm/WhitespaceStretchingTransformer.js +17 -15
- package/esm/WhitespaceTransformer.js +7 -7
- package/package.json +1 -1
- package/script/WhitespaceStretchingTransformer.d.ts +2 -2
- package/script/WhitespaceStretchingTransformer.js +17 -15
- package/script/WhitespaceTransformer.js +7 -7
package/README.md
CHANGED
|
@@ -1,15 +1,4 @@
|
|
|
1
1
|
# document-ir
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
```bash
|
|
6
|
-
bun install
|
|
7
|
-
```
|
|
8
|
-
|
|
9
|
-
To run:
|
|
10
|
-
|
|
11
|
-
```bash
|
|
12
|
-
bun run index.ts
|
|
13
|
-
```
|
|
14
|
-
|
|
15
|
-
This project was created using `bun init` in bun v0.6.7. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
|
|
3
|
+
This helpful library can help contain and manipulate blog content much like
|
|
4
|
+
multiple passes of a compiler.
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { IdentityTransformer } from "./IdentityTransformer.js";
|
|
2
2
|
import { DocumentNode, Node, TextNode } from "./types.js";
|
|
3
3
|
interface BlockInfo {
|
|
4
|
-
type:
|
|
4
|
+
type: "_block";
|
|
5
5
|
content: WhiteSpaceContainer[];
|
|
6
6
|
parent: BlockInfo | InlineInfo | null;
|
|
7
7
|
}
|
|
8
8
|
interface InlineInfo {
|
|
9
|
-
type:
|
|
9
|
+
type: "_inline";
|
|
10
10
|
content: WhiteSpaceContainer[];
|
|
11
11
|
parent: BlockInfo | InlineInfo | null;
|
|
12
12
|
}
|
|
@@ -15,9 +15,9 @@ export class WhitespaceStretchingTransformer extends IdentityTransformer {
|
|
|
15
15
|
value: void 0
|
|
16
16
|
});
|
|
17
17
|
this.root = {
|
|
18
|
-
type:
|
|
18
|
+
type: "_block",
|
|
19
19
|
content: [],
|
|
20
|
-
parent: null
|
|
20
|
+
parent: null,
|
|
21
21
|
};
|
|
22
22
|
this.cursor = this.root;
|
|
23
23
|
}
|
|
@@ -25,9 +25,9 @@ export class WhitespaceStretchingTransformer extends IdentityTransformer {
|
|
|
25
25
|
async beforeBlock() {
|
|
26
26
|
const parent = this.cursor;
|
|
27
27
|
const block = {
|
|
28
|
-
type:
|
|
28
|
+
type: "_block",
|
|
29
29
|
content: [],
|
|
30
|
-
parent
|
|
30
|
+
parent,
|
|
31
31
|
};
|
|
32
32
|
parent.content.push(block);
|
|
33
33
|
this.cursor = block;
|
|
@@ -42,9 +42,9 @@ export class WhitespaceStretchingTransformer extends IdentityTransformer {
|
|
|
42
42
|
async beforeInline() {
|
|
43
43
|
const parent = this.cursor;
|
|
44
44
|
const inline = {
|
|
45
|
-
type:
|
|
45
|
+
type: "_inline",
|
|
46
46
|
content: [],
|
|
47
|
-
parent
|
|
47
|
+
parent,
|
|
48
48
|
};
|
|
49
49
|
parent.content.push(inline);
|
|
50
50
|
this.cursor = inline;
|
|
@@ -58,13 +58,13 @@ export class WhitespaceStretchingTransformer extends IdentityTransformer {
|
|
|
58
58
|
reviewBlock(block) {
|
|
59
59
|
const nodes = [];
|
|
60
60
|
const visit = (r, level) => {
|
|
61
|
-
if (r.type ==
|
|
61
|
+
if (r.type == "text") {
|
|
62
62
|
nodes.push({
|
|
63
63
|
node: r,
|
|
64
|
-
level
|
|
64
|
+
level,
|
|
65
65
|
});
|
|
66
66
|
}
|
|
67
|
-
else if (r.type ==
|
|
67
|
+
else if (r.type == "_block") {
|
|
68
68
|
this.reviewBlock(r);
|
|
69
69
|
nodes.push({ node: null, level });
|
|
70
70
|
}
|
|
@@ -79,7 +79,9 @@ export class WhitespaceStretchingTransformer extends IdentityTransformer {
|
|
|
79
79
|
}
|
|
80
80
|
for (let i = 0; i < nodes.length; i++) {
|
|
81
81
|
const first = nodes[i];
|
|
82
|
-
const second = i + 1 < nodes.length
|
|
82
|
+
const second = i + 1 < nodes.length
|
|
83
|
+
? nodes[i + 1]
|
|
84
|
+
: { node: null, level: 0 };
|
|
83
85
|
// No use acting on a dead node
|
|
84
86
|
if (!first.node || !second.node) {
|
|
85
87
|
continue;
|
|
@@ -89,13 +91,13 @@ export class WhitespaceStretchingTransformer extends IdentityTransformer {
|
|
|
89
91
|
continue;
|
|
90
92
|
}
|
|
91
93
|
if (first.level < second.level) {
|
|
92
|
-
if (second.node.text.startsWith(
|
|
94
|
+
if (second.node.text.startsWith(" ")) {
|
|
93
95
|
second.node.text = second.node.text.slice(1);
|
|
94
|
-
first.node.text +=
|
|
96
|
+
first.node.text += " ";
|
|
95
97
|
}
|
|
96
98
|
}
|
|
97
99
|
else if (first.level > second.level) {
|
|
98
|
-
if (first.node.text.endsWith(
|
|
100
|
+
if (first.node.text.endsWith(" ")) {
|
|
99
101
|
first.node.text = first.node.text.slice(0, -1);
|
|
100
102
|
second.node.text = ` ${second.node.text}`;
|
|
101
103
|
}
|
|
@@ -105,8 +107,8 @@ export class WhitespaceStretchingTransformer extends IdentityTransformer {
|
|
|
105
107
|
// deno-lint-ignore require-await
|
|
106
108
|
async text(node) {
|
|
107
109
|
const replacement = {
|
|
108
|
-
type:
|
|
109
|
-
text: `${node.text}
|
|
110
|
+
type: "text",
|
|
111
|
+
text: `${node.text}`,
|
|
110
112
|
};
|
|
111
113
|
this.cursor.content.push(replacement);
|
|
112
114
|
return replacement;
|
|
@@ -2,7 +2,7 @@ import { IdentityTransformer } from "./IdentityTransformer.js";
|
|
|
2
2
|
class RemoveEmptyTextTransformer extends IdentityTransformer {
|
|
3
3
|
// deno-lint-ignore require-await
|
|
4
4
|
async text(node) {
|
|
5
|
-
if (node.text ==
|
|
5
|
+
if (node.text == "") {
|
|
6
6
|
return null;
|
|
7
7
|
}
|
|
8
8
|
return node;
|
|
@@ -28,11 +28,11 @@ export class WhitespaceTransformer extends IdentityTransformer {
|
|
|
28
28
|
}
|
|
29
29
|
// deno-lint-ignore require-await
|
|
30
30
|
async text(node) {
|
|
31
|
-
let result =
|
|
31
|
+
let result = "";
|
|
32
32
|
for (const c of node.text) {
|
|
33
|
-
if (c ==
|
|
33
|
+
if (c == " " || c == "\n" || c == "\t" || c == "\r") {
|
|
34
34
|
if (!this.stripWhitespace) {
|
|
35
|
-
result +=
|
|
35
|
+
result += " ";
|
|
36
36
|
this.stripWhitespace = true;
|
|
37
37
|
}
|
|
38
38
|
}
|
|
@@ -45,15 +45,15 @@ export class WhitespaceTransformer extends IdentityTransformer {
|
|
|
45
45
|
return null;
|
|
46
46
|
}
|
|
47
47
|
const text = {
|
|
48
|
-
type:
|
|
49
|
-
text: result
|
|
48
|
+
type: "text",
|
|
49
|
+
text: result,
|
|
50
50
|
};
|
|
51
51
|
this.lastText = text;
|
|
52
52
|
return text;
|
|
53
53
|
}
|
|
54
54
|
stripLastText() {
|
|
55
55
|
if (this.lastText) {
|
|
56
|
-
if (this.lastText.text.endsWith(
|
|
56
|
+
if (this.lastText.text.endsWith(" ")) {
|
|
57
57
|
this.lastText.text = this.lastText.text.slice(0, -1);
|
|
58
58
|
}
|
|
59
59
|
this.lastText = null;
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import { IdentityTransformer } from "./IdentityTransformer.js";
|
|
2
2
|
import { DocumentNode, Node, TextNode } from "./types.js";
|
|
3
3
|
interface BlockInfo {
|
|
4
|
-
type:
|
|
4
|
+
type: "_block";
|
|
5
5
|
content: WhiteSpaceContainer[];
|
|
6
6
|
parent: BlockInfo | InlineInfo | null;
|
|
7
7
|
}
|
|
8
8
|
interface InlineInfo {
|
|
9
|
-
type:
|
|
9
|
+
type: "_inline";
|
|
10
10
|
content: WhiteSpaceContainer[];
|
|
11
11
|
parent: BlockInfo | InlineInfo | null;
|
|
12
12
|
}
|
|
@@ -18,9 +18,9 @@ class WhitespaceStretchingTransformer extends IdentityTransformer_js_1.IdentityT
|
|
|
18
18
|
value: void 0
|
|
19
19
|
});
|
|
20
20
|
this.root = {
|
|
21
|
-
type:
|
|
21
|
+
type: "_block",
|
|
22
22
|
content: [],
|
|
23
|
-
parent: null
|
|
23
|
+
parent: null,
|
|
24
24
|
};
|
|
25
25
|
this.cursor = this.root;
|
|
26
26
|
}
|
|
@@ -28,9 +28,9 @@ class WhitespaceStretchingTransformer extends IdentityTransformer_js_1.IdentityT
|
|
|
28
28
|
async beforeBlock() {
|
|
29
29
|
const parent = this.cursor;
|
|
30
30
|
const block = {
|
|
31
|
-
type:
|
|
31
|
+
type: "_block",
|
|
32
32
|
content: [],
|
|
33
|
-
parent
|
|
33
|
+
parent,
|
|
34
34
|
};
|
|
35
35
|
parent.content.push(block);
|
|
36
36
|
this.cursor = block;
|
|
@@ -45,9 +45,9 @@ class WhitespaceStretchingTransformer extends IdentityTransformer_js_1.IdentityT
|
|
|
45
45
|
async beforeInline() {
|
|
46
46
|
const parent = this.cursor;
|
|
47
47
|
const inline = {
|
|
48
|
-
type:
|
|
48
|
+
type: "_inline",
|
|
49
49
|
content: [],
|
|
50
|
-
parent
|
|
50
|
+
parent,
|
|
51
51
|
};
|
|
52
52
|
parent.content.push(inline);
|
|
53
53
|
this.cursor = inline;
|
|
@@ -61,13 +61,13 @@ class WhitespaceStretchingTransformer extends IdentityTransformer_js_1.IdentityT
|
|
|
61
61
|
reviewBlock(block) {
|
|
62
62
|
const nodes = [];
|
|
63
63
|
const visit = (r, level) => {
|
|
64
|
-
if (r.type ==
|
|
64
|
+
if (r.type == "text") {
|
|
65
65
|
nodes.push({
|
|
66
66
|
node: r,
|
|
67
|
-
level
|
|
67
|
+
level,
|
|
68
68
|
});
|
|
69
69
|
}
|
|
70
|
-
else if (r.type ==
|
|
70
|
+
else if (r.type == "_block") {
|
|
71
71
|
this.reviewBlock(r);
|
|
72
72
|
nodes.push({ node: null, level });
|
|
73
73
|
}
|
|
@@ -82,7 +82,9 @@ class WhitespaceStretchingTransformer extends IdentityTransformer_js_1.IdentityT
|
|
|
82
82
|
}
|
|
83
83
|
for (let i = 0; i < nodes.length; i++) {
|
|
84
84
|
const first = nodes[i];
|
|
85
|
-
const second = i + 1 < nodes.length
|
|
85
|
+
const second = i + 1 < nodes.length
|
|
86
|
+
? nodes[i + 1]
|
|
87
|
+
: { node: null, level: 0 };
|
|
86
88
|
// No use acting on a dead node
|
|
87
89
|
if (!first.node || !second.node) {
|
|
88
90
|
continue;
|
|
@@ -92,13 +94,13 @@ class WhitespaceStretchingTransformer extends IdentityTransformer_js_1.IdentityT
|
|
|
92
94
|
continue;
|
|
93
95
|
}
|
|
94
96
|
if (first.level < second.level) {
|
|
95
|
-
if (second.node.text.startsWith(
|
|
97
|
+
if (second.node.text.startsWith(" ")) {
|
|
96
98
|
second.node.text = second.node.text.slice(1);
|
|
97
|
-
first.node.text +=
|
|
99
|
+
first.node.text += " ";
|
|
98
100
|
}
|
|
99
101
|
}
|
|
100
102
|
else if (first.level > second.level) {
|
|
101
|
-
if (first.node.text.endsWith(
|
|
103
|
+
if (first.node.text.endsWith(" ")) {
|
|
102
104
|
first.node.text = first.node.text.slice(0, -1);
|
|
103
105
|
second.node.text = ` ${second.node.text}`;
|
|
104
106
|
}
|
|
@@ -108,8 +110,8 @@ class WhitespaceStretchingTransformer extends IdentityTransformer_js_1.IdentityT
|
|
|
108
110
|
// deno-lint-ignore require-await
|
|
109
111
|
async text(node) {
|
|
110
112
|
const replacement = {
|
|
111
|
-
type:
|
|
112
|
-
text: `${node.text}
|
|
113
|
+
type: "text",
|
|
114
|
+
text: `${node.text}`,
|
|
113
115
|
};
|
|
114
116
|
this.cursor.content.push(replacement);
|
|
115
117
|
return replacement;
|
|
@@ -5,7 +5,7 @@ const IdentityTransformer_js_1 = require("./IdentityTransformer.js");
|
|
|
5
5
|
class RemoveEmptyTextTransformer extends IdentityTransformer_js_1.IdentityTransformer {
|
|
6
6
|
// deno-lint-ignore require-await
|
|
7
7
|
async text(node) {
|
|
8
|
-
if (node.text ==
|
|
8
|
+
if (node.text == "") {
|
|
9
9
|
return null;
|
|
10
10
|
}
|
|
11
11
|
return node;
|
|
@@ -31,11 +31,11 @@ class WhitespaceTransformer extends IdentityTransformer_js_1.IdentityTransformer
|
|
|
31
31
|
}
|
|
32
32
|
// deno-lint-ignore require-await
|
|
33
33
|
async text(node) {
|
|
34
|
-
let result =
|
|
34
|
+
let result = "";
|
|
35
35
|
for (const c of node.text) {
|
|
36
|
-
if (c ==
|
|
36
|
+
if (c == " " || c == "\n" || c == "\t" || c == "\r") {
|
|
37
37
|
if (!this.stripWhitespace) {
|
|
38
|
-
result +=
|
|
38
|
+
result += " ";
|
|
39
39
|
this.stripWhitespace = true;
|
|
40
40
|
}
|
|
41
41
|
}
|
|
@@ -48,15 +48,15 @@ class WhitespaceTransformer extends IdentityTransformer_js_1.IdentityTransformer
|
|
|
48
48
|
return null;
|
|
49
49
|
}
|
|
50
50
|
const text = {
|
|
51
|
-
type:
|
|
52
|
-
text: result
|
|
51
|
+
type: "text",
|
|
52
|
+
text: result,
|
|
53
53
|
};
|
|
54
54
|
this.lastText = text;
|
|
55
55
|
return text;
|
|
56
56
|
}
|
|
57
57
|
stripLastText() {
|
|
58
58
|
if (this.lastText) {
|
|
59
|
-
if (this.lastText.text.endsWith(
|
|
59
|
+
if (this.lastText.text.endsWith(" ")) {
|
|
60
60
|
this.lastText.text = this.lastText.text.slice(0, -1);
|
|
61
61
|
}
|
|
62
62
|
this.lastText = null;
|