Skip to content

Commit

Permalink
fix: handle start with single newline (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
3w36zj6 authored Jan 14, 2025
1 parent 2a66153 commit 0f5e7d7
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/typstToTextlintAst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,20 @@ export const convertRawTypstAstObjectToTextlintAstObject = (
return endOffset;
};

// If the source code starts with a single newline, add a Break node before the first node.
if (textlintAstObject.c) {
const rootChildrenStartLocation = extractLocation(
textlintAstObject.c[0].s,
textlintAstObject.c[0].c,
);
if (rootChildrenStartLocation.start.line === 2) {
// @ts-expect-error
textlintAstObject.c.unshift({
s: "<span style='color:#7dcfff'>Marked::Parbreak</span> &lt;1:0~2:0&gt;",
});
}
}

calculateOffsets(textlintAstObject);

// Root node is always `Document` node
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/start-with-single-newline/input.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

This is test that starts with a single newline.
66 changes: 66 additions & 0 deletions test/fixtures/start-with-single-newline/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"children": [
{
"value": "\n",
"raw": "\n",
"range": [0, 1],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 0
}
},
"type": "Break"
},
{
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 47
}
},
"range": [1, 48],
"raw": "This is test that starts with a single newline.",
"type": "Paragraph",
"children": [
{
"value": "This is test that starts with a single newline.",
"raw": "This is test that starts with a single newline.",
"range": [1, 48],
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 47
}
},
"type": "Str"
}
]
}
],
"raw": "\nThis is test that starts with a single newline.",
"range": [0, 48],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 47
}
},
"type": "Document"
}

0 comments on commit 0f5e7d7

Please sign in to comment.