Skip to content

Commit ec961f1

Browse files
committed
Minor improvements to inspect()
1 parent 8519c58 commit ec961f1

4 files changed

Lines changed: 507 additions & 243 deletions

File tree

packages/utils/README.md

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ The package includes a powerful tree visualization utility that renders structur
387387

388388
| Function | Description |
389389
| ------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------- |
390-
| [`inspect`](https://github.com/datocms/structured-text/blob/main/packages/utils/src/inspector.ts#L202) | Render a structured text document or node as an ASCII tree |
390+
| [`inspect`](https://github.com/datocms/structured-text/blob/main/packages/utils/src/inspector.ts#L199) | Render a structured text document or node as an ASCII tree |
391391

392392
```javascript
393393
import { inspect } from 'datocms-structured-text-utils';
@@ -424,14 +424,13 @@ console.log(inspect(structuredText));
424424
**Output:**
425425

426426
```
427-
└ root
428-
├ heading (level: 1)
429-
│ └ span "Main Title"
430-
├ paragraph
431-
│ ├ span "This is a "
432-
│ ├ span (marks: strong) "bold"
433-
│ └ span " paragraph."
434-
└ block (item: "block-123")
427+
├ heading (level: 1)
428+
│ └ span "Main Title"
429+
├ paragraph
430+
│ ├ span "This is a "
431+
│ ├ span (marks: strong) "bold"
432+
│ └ span " paragraph."
433+
└ block (item: "block-123")
435434
```
436435

437436
### Custom Block Formatting
@@ -466,12 +465,11 @@ console.log(tree);
466465
**Output:**
467466

468467
```
469-
└ root
470-
├ paragraph
471-
│ └ span "Content before block"
472-
├ block
473-
│ id: 456
474-
│ title: Hero Section
475-
└ paragraph
476-
└ span "Content after block"
468+
├ paragraph
469+
│ └ span "Content before block"
470+
├ block
471+
│ id: 456
472+
│ title: Hero Section
473+
└ paragraph
474+
└ span "Content after block"
477475
```
Lines changed: 109 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,167 +1,186 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`inspector inspect should render a document wrapper 1`] = `
4-
"└ root
5-
└ paragraph
6-
└ span \\"Test\\"
4+
"└ paragraph
5+
└ span \\"Test\\"
76
"
87
`;
98

109
exports[`inspector inspect should render a simple root node 1`] = `
11-
"└ root
12-
└ paragraph
13-
└ span \\"Hello World\\"
10+
"└ paragraph
11+
└ span \\"Hello World\\"
1412
"
1513
`;
1614

1715
exports[`inspector inspect should render block and inline block nodes 1`] = `
18-
"└ root
19-
├ block (item: \\"block-123\\")
20-
└ paragraph
21-
├ span \\"Text with \\"
22-
├ inlineBlock (item: \\"inline-456\\")
23-
└ span \\" inline block\\"
16+
"├ block (item: \\"block-123\\")
17+
└ paragraph
18+
├ span \\"Text with \\"
19+
├ inlineBlock (item: \\"inline-456\\")
20+
└ span \\" inline block\\"
2421
"
2522
`;
2623

2724
exports[`inspector inspect should render code nodes with language and content 1`] = `
28-
"└ root
29-
├ code (language: \\"javascript\\") \\"console.log(\\"Hello, World!\\");\\"
30-
└ code \\"echo \\"No language specified\\"\\"
25+
"├ code (language: \\"javascript\\") \\"console.log(\\"Hello, World!\\");\\"
26+
└ code \\"echo \\"No language specified\\"\\"
3127
"
3228
`;
3329

3430
exports[`inspector inspect should render heading nodes with level 1`] = `
35-
"└ root
36-
├ heading (level: 1)
37-
│ └ span \\"Main Title\\"
38-
└ heading (level: 2)
39-
└ span \\"Subtitle\\"
31+
"├ heading (level: 1)
32+
│ └ span \\"Main Title\\"
33+
└ heading (level: 2)
34+
└ span \\"Subtitle\\"
4035
"
4136
`;
4237

4338
exports[`inspector inspect should render link nodes with URL and meta 1`] = `
44-
"└ root
45-
└ paragraph
46-
├ span \\"Visit \\"
47-
├ link (url: \\"https://example.com\\")
48-
│ └ span \\"this link\\"
49-
└ link (url: \\"https://meta.example.com\\", meta: {title=\\"Example Site\\", target=\\"_blank\\"})
50-
└ span \\"link with meta\\"
39+
"└ paragraph
40+
├ span \\"Visit \\"
41+
├ link (url: \\"https://example.com\\")
42+
│ └ span \\"this link\\"
43+
└ link (url: \\"https://meta.example.com\\", meta: {title=\\"Example Site\\", target=\\"_blank\\"})
44+
└ span \\"link with meta\\"
5145
"
5246
`;
5347

5448
exports[`inspector inspect should render list nodes with style 1`] = `
55-
"└ root
56-
└ list (style: bulleted)
57-
├ listItem
58-
│ └ paragraph
59-
│ └ span \\"First item\\"
60-
└ listItem
61-
└ paragraph
62-
└ span \\"Second item\\"
49+
"└ list (style: bulleted)
50+
├ listItem
51+
│ └ paragraph
52+
│ └ span \\"First item\\"
53+
└ listItem
54+
└ paragraph
55+
└ span \\"Second item\\"
6356
"
6457
`;
6558

6659
exports[`inspector inspect should render multiple children with correct tree structure 1`] = `
67-
"└ root
68-
├ paragraph
69-
│ └ span \\"First paragraph\\"
70-
└ paragraph
71-
└ span \\"Second paragraph\\"
60+
"├ paragraph
61+
│ └ span \\"First paragraph\\"
62+
└ paragraph
63+
└ span \\"Second paragraph\\"
7264
"
7365
`;
7466

7567
exports[`inspector inspect should render paragraph with custom style 1`] = `
76-
"└ root
77-
└ paragraph (style: \\"custom-style\\")
78-
└ span \\"Styled paragraph\\"
68+
"└ paragraph (style: \\"custom-style\\")
69+
└ span \\"Styled paragraph\\"
7970
"
8071
`;
8172

8273
exports[`inspector inspect should render span nodes with marks 1`] = `
83-
"└ root
84-
└ paragraph
85-
├ span \\"Normal text\\"
86-
├ span (marks: strong) \\"Bold text\\"
87-
└ span (marks: strong, emphasis) \\"Bold italic\\"
74+
"└ paragraph
75+
├ span \\"Normal text\\"
76+
├ span (marks: strong) \\"Bold text\\"
77+
└ span (marks: strong, emphasis) \\"Bold italic\\"
8878
"
8979
`;
9080

9181
exports[`inspector inspect should render thematic break 1`] = `
92-
"└ root
93-
├ paragraph
94-
│ └ span \\"Before break\\"
95-
├ thematicBreak
96-
└ paragraph
97-
└ span \\"After break\\"
82+
"├ paragraph
83+
│ └ span \\"Before break\\"
84+
├ thematicBreak
85+
└ paragraph
86+
└ span \\"After break\\"
9887
"
9988
`;
10089

10190
exports[`inspector inspect should truncate long text content 1`] = `
102-
"└ root
103-
├ paragraph
104-
│ └ span \\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...\\"
105-
└ code \\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...\\"
91+
"├ paragraph
92+
│ └ span \\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...\\"
93+
└ code \\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...\\"
94+
"
95+
`;
96+
97+
exports[`inspector inspect with block formatter should add TreeNode children to blocks without existing children 1`] = `
98+
"└ block
99+
└ Block Info
100+
├ Title: Block with TreeNode Children
101+
└ ID: block-789
106102
"
107103
`;
108104

109105
exports[`inspector inspect with block formatter should fall back to default formatting when no formatter provided 1`] = `
110-
"└ root
111-
├ block (item: \\"block-123\\")
112-
└ paragraph
113-
└ inlineBlock (item: \\"inline-456\\")
106+
"├ block (item: \\"block-123\\")
107+
└ paragraph
108+
└ inlineBlock (item: \\"inline-456\\")
114109
"
115110
`;
116111

117112
exports[`inspector inspect with block formatter should fall back to default formatting when no formatter provided 2`] = `
118-
"└ root
119-
├ block (item: \\"block-123\\")
120-
└ paragraph
121-
└ inlineBlock (item: \\"inline-456\\")
113+
"├ block (item: \\"block-123\\")
114+
└ paragraph
115+
└ inlineBlock (item: \\"inline-456\\")
122116
"
123117
`;
124118

125119
exports[`inspector inspect with block formatter should format block with ID using custom formatter 1`] = `
126-
"└ root
127-
└ block ID: block-123
120+
"└ block ID: block-123
128121
"
129122
`;
130123

131124
exports[`inspector inspect with block formatter should format block with object using custom formatter 1`] = `
132-
"└ root
133-
└ block My Block Title (block-456)
125+
"└ block My Block Title (block-456)
134126
"
135127
`;
136128

137129
exports[`inspector inspect with block formatter should format inline blocks with custom formatter 1`] = `
138-
"└ root
139-
└ paragraph
140-
├ span \\"Click \\"
141-
├ inlineBlock Button: \\"Click me\\"
142-
└ span \\" to continue\\"
130+
"└ paragraph
131+
├ span \\"Click \\"
132+
├ inlineBlock Button: \\"Click me\\"
133+
└ span \\" to continue\\"
134+
"
135+
`;
136+
137+
exports[`inspector inspect with block formatter should handle TreeNode returns for inline blocks 1`] = `
138+
"└ paragraph
139+
├ span \\"Click \\"
140+
├ inlineBlock
141+
│ └ button (button-123)
142+
│ ├ Variant: primary
143+
│ └ Size: large
144+
└ span \\" to continue\\"
145+
"
146+
`;
147+
148+
exports[`inspector inspect with block formatter should handle blockFormatter returning a single TreeNode 1`] = `
149+
"└ block
150+
└ Content: Block content here
151+
├ Title: My Block Title
152+
└ ID: block-123
153+
"
154+
`;
155+
156+
exports[`inspector inspect with block formatter should handle blockFormatter returning an array of TreeNodes 1`] = `
157+
"└ block
158+
├ Properties
159+
│ ├ Title: Complex Block
160+
│ └ ID: block-456
161+
└ Metadata
162+
├ Author: Jane Doe
163+
└ Tags
164+
├ important
165+
└ feature
143166
"
144167
`;
145168

146169
exports[`inspector inspect with block formatter should handle multi-line block formatter output 1`] = `
147-
"└ root
148-
├ paragraph
149-
│ └ span \\"Before block\\"
150-
├ block
151-
│ Complex Block (block-789)
152-
│ Description: A detailed description
153-
│ Author: John Doe
154-
│ Date: 2023-01-01
155-
└ paragraph
156-
└ span \\"After block\\"
170+
"├ paragraph
171+
│ └ span \\"Before block\\"
172+
├ block Complex Block (block-789) Description: A detailed description Author: John Doe Date: 2023-01-01
173+
└ paragraph
174+
└ span \\"After block\\"
157175
"
158176
`;
159177

160178
exports[`inspector inspect with block formatter should provide max line width to formatter 1`] = `
161-
"└ root
162-
└ block
163-
My Block
164-
Description: This is a very long description that might exceed the suggested line width
165-
ID: block-123
179+
"└ block My Block Description: This is a very long description that might exceed the suggested line width ID: block-123
180+
"
181+
`;
182+
183+
exports[`inspector inspect with block formatter should treat string returns as single-line (strip newlines) 1`] = `
184+
"└ block Title: Multi-line Block Description: Description with multiple lines ID: block-789
166185
"
167186
`;

0 commit comments

Comments
 (0)