Skip to content

Commit 0f5e7d7

Browse files
authored
fix: handle start with single newline (#4)
1 parent 2a66153 commit 0f5e7d7

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

src/typstToTextlintAst.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,20 @@ export const convertRawTypstAstObjectToTextlintAstObject = (
371371
return endOffset;
372372
};
373373

374+
// If the source code starts with a single newline, add a Break node before the first node.
375+
if (textlintAstObject.c) {
376+
const rootChildrenStartLocation = extractLocation(
377+
textlintAstObject.c[0].s,
378+
textlintAstObject.c[0].c,
379+
);
380+
if (rootChildrenStartLocation.start.line === 2) {
381+
// @ts-expect-error
382+
textlintAstObject.c.unshift({
383+
s: "<span style='color:#7dcfff'>Marked::Parbreak</span> &lt;1:0~2:0&gt;",
384+
});
385+
}
386+
}
387+
374388
calculateOffsets(textlintAstObject);
375389

376390
// Root node is always `Document` node
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
This is test that starts with a single newline.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
{
2+
"children": [
3+
{
4+
"value": "\n",
5+
"raw": "\n",
6+
"range": [0, 1],
7+
"loc": {
8+
"start": {
9+
"line": 1,
10+
"column": 0
11+
},
12+
"end": {
13+
"line": 2,
14+
"column": 0
15+
}
16+
},
17+
"type": "Break"
18+
},
19+
{
20+
"loc": {
21+
"start": {
22+
"line": 2,
23+
"column": 0
24+
},
25+
"end": {
26+
"line": 2,
27+
"column": 47
28+
}
29+
},
30+
"range": [1, 48],
31+
"raw": "This is test that starts with a single newline.",
32+
"type": "Paragraph",
33+
"children": [
34+
{
35+
"value": "This is test that starts with a single newline.",
36+
"raw": "This is test that starts with a single newline.",
37+
"range": [1, 48],
38+
"loc": {
39+
"start": {
40+
"line": 2,
41+
"column": 0
42+
},
43+
"end": {
44+
"line": 2,
45+
"column": 47
46+
}
47+
},
48+
"type": "Str"
49+
}
50+
]
51+
}
52+
],
53+
"raw": "\nThis is test that starts with a single newline.",
54+
"range": [0, 48],
55+
"loc": {
56+
"start": {
57+
"line": 1,
58+
"column": 0
59+
},
60+
"end": {
61+
"line": 2,
62+
"column": 47
63+
}
64+
},
65+
"type": "Document"
66+
}

0 commit comments

Comments
 (0)