From 9ae4c8ac802b596271241ffc8d45c2338050e1fc Mon Sep 17 00:00:00 2001 From: 3w36zj6 <52315048+3w36zj6@users.noreply.github.com> Date: Sun, 13 Oct 2024 15:42:53 +0900 Subject: [PATCH] fix: correct parsing of inline code surrounded by triple backticks --- src/typstToTextlintAst.ts | 8 +- test/fixtures/Code/input.typ | 8 + test/fixtures/Code/output.json | 458 ++++++++++++++++++++++++++++++++- 3 files changed, 469 insertions(+), 5 deletions(-) diff --git a/src/typstToTextlintAst.ts b/src/typstToTextlintAst.ts index 2c48bc6..d8cf949 100644 --- a/src/typstToTextlintAst.ts +++ b/src/typstToTextlintAst.ts @@ -307,7 +307,13 @@ export const convertRawTypstAstObjectToTextlintAstObject = ( if (node.loc.start.line === node.loc.end.line) { // If Code node.type = ASTNodeTypes.Code; - node.value = node.raw.replace(/`([\s\S]*?)`/, "$1"); + if (/^```([\s\S]*?)```$/.test(node.raw)) { + const codeBlockPattern = /^```(\w+)?\s([\s\S]*?)\s*```$/; + const match = node.raw.match(codeBlockPattern); + node.value = match ? match[2].trim() : ""; + } else { + node.value = node.raw.replace(/`([\s\S]*?)`/, "$1"); + } } else { // If CodeBlock node.type = ASTNodeTypes.CodeBlock; diff --git a/test/fixtures/Code/input.typ b/test/fixtures/Code/input.typ index 23a0e8a..271ece0 100644 --- a/test/fixtures/Code/input.typ +++ b/test/fixtures/Code/input.typ @@ -1,3 +1,11 @@ `inline code` aaa `inline code` bbb + +What is ```rust fn main()``` in Rust +would be ```c int main()``` in C. + +This has ``` `backticks` ``` in it +(but the spaces are trimmed). And +``` here``` the leading space is +also trimmed. diff --git a/test/fixtures/Code/output.json b/test/fixtures/Code/output.json index de500b1..5e77459 100644 --- a/test/fixtures/Code/output.json +++ b/test/fixtures/Code/output.json @@ -145,19 +145,469 @@ } } ] + }, + { + "value": "\n\n", + "type": "Break", + "raw": "\n\n", + "range": [36, 38], + "loc": { + "start": { + "line": 3, + "column": 21 + }, + "end": { + "line": 5, + "column": 0 + } + } + }, + { + "loc": { + "start": { + "line": 5, + "column": 0 + }, + "end": { + "line": 6, + "column": 33 + } + }, + "range": [38, 108], + "raw": "What is ```rust fn main()``` in Rust\nwould be ```c int main()``` in C.", + "type": "Paragraph", + "children": [ + { + "value": "What is", + "type": "Str", + "raw": "What is", + "range": [38, 45], + "loc": { + "start": { + "line": 5, + "column": 0 + }, + "end": { + "line": 5, + "column": 7 + } + } + }, + { + "type": "Str", + "raw": " ", + "value": " ", + "range": [45, 46], + "loc": { + "start": { + "line": 5, + "column": 7 + }, + "end": { + "line": 5, + "column": 8 + } + } + }, + { + "type": "Code", + "raw": "```rust fn main()```", + "range": [46, 66], + "loc": { + "start": { + "line": 5, + "column": 8 + }, + "end": { + "line": 5, + "column": 28 + } + }, + "value": "fn main()" + }, + { + "type": "Str", + "raw": " ", + "value": " ", + "range": [66, 67], + "loc": { + "start": { + "line": 5, + "column": 28 + }, + "end": { + "line": 5, + "column": 29 + } + } + }, + { + "value": "in Rust", + "type": "Str", + "raw": "in Rust", + "range": [67, 74], + "loc": { + "start": { + "line": 5, + "column": 29 + }, + "end": { + "line": 5, + "column": 36 + } + } + }, + { + "type": "Str", + "raw": "\n", + "value": "\n", + "range": [74, 75], + "loc": { + "start": { + "line": 5, + "column": 36 + }, + "end": { + "line": 6, + "column": 0 + } + } + }, + { + "value": "would be", + "type": "Str", + "raw": "would be", + "range": [75, 83], + "loc": { + "start": { + "line": 6, + "column": 0 + }, + "end": { + "line": 6, + "column": 8 + } + } + }, + { + "type": "Str", + "raw": " ", + "value": " ", + "range": [83, 84], + "loc": { + "start": { + "line": 6, + "column": 8 + }, + "end": { + "line": 6, + "column": 9 + } + } + }, + { + "type": "Code", + "raw": "```c int main()```", + "range": [84, 102], + "loc": { + "start": { + "line": 6, + "column": 9 + }, + "end": { + "line": 6, + "column": 27 + } + }, + "value": "int main()" + }, + { + "type": "Str", + "raw": " ", + "value": " ", + "range": [102, 103], + "loc": { + "start": { + "line": 6, + "column": 27 + }, + "end": { + "line": 6, + "column": 28 + } + } + }, + { + "value": "in C.", + "type": "Str", + "raw": "in C.", + "range": [103, 108], + "loc": { + "start": { + "line": 6, + "column": 28 + }, + "end": { + "line": 6, + "column": 33 + } + } + } + ] + }, + { + "value": "\n\n", + "type": "Break", + "raw": "\n\n", + "range": [108, 110], + "loc": { + "start": { + "line": 6, + "column": 33 + }, + "end": { + "line": 8, + "column": 0 + } + } + }, + { + "loc": { + "start": { + "line": 8, + "column": 0 + }, + "end": { + "line": 11, + "column": 13 + } + }, + "range": [110, 225], + "raw": "This has ``` `backticks` ``` in it\n(but the spaces are trimmed). And\n``` here``` the leading space is\nalso trimmed.", + "type": "Paragraph", + "children": [ + { + "value": "This has", + "type": "Str", + "raw": "This has", + "range": [110, 118], + "loc": { + "start": { + "line": 8, + "column": 0 + }, + "end": { + "line": 8, + "column": 8 + } + } + }, + { + "type": "Str", + "raw": " ", + "value": " ", + "range": [118, 119], + "loc": { + "start": { + "line": 8, + "column": 8 + }, + "end": { + "line": 8, + "column": 9 + } + } + }, + { + "type": "Code", + "raw": "``` `backticks` ```", + "range": [119, 138], + "loc": { + "start": { + "line": 8, + "column": 9 + }, + "end": { + "line": 8, + "column": 28 + } + }, + "value": "`backticks`" + }, + { + "type": "Str", + "raw": " ", + "value": " ", + "range": [138, 139], + "loc": { + "start": { + "line": 8, + "column": 28 + }, + "end": { + "line": 8, + "column": 29 + } + } + }, + { + "value": "in it", + "type": "Str", + "raw": "in it", + "range": [139, 144], + "loc": { + "start": { + "line": 8, + "column": 29 + }, + "end": { + "line": 8, + "column": 34 + } + } + }, + { + "type": "Str", + "raw": "\n", + "value": "\n", + "range": [144, 145], + "loc": { + "start": { + "line": 8, + "column": 34 + }, + "end": { + "line": 9, + "column": 0 + } + } + }, + { + "value": "(but the spaces are trimmed). And", + "type": "Str", + "raw": "(but the spaces are trimmed). And", + "range": [145, 178], + "loc": { + "start": { + "line": 9, + "column": 0 + }, + "end": { + "line": 9, + "column": 33 + } + } + }, + { + "type": "Str", + "raw": "\n", + "value": "\n", + "range": [178, 179], + "loc": { + "start": { + "line": 9, + "column": 33 + }, + "end": { + "line": 10, + "column": 0 + } + } + }, + { + "type": "Code", + "raw": "``` here```", + "range": [179, 190], + "loc": { + "start": { + "line": 10, + "column": 0 + }, + "end": { + "line": 10, + "column": 11 + } + }, + "value": "here" + }, + { + "type": "Str", + "raw": " ", + "value": " ", + "range": [190, 191], + "loc": { + "start": { + "line": 10, + "column": 11 + }, + "end": { + "line": 10, + "column": 12 + } + } + }, + { + "value": "the leading space is", + "type": "Str", + "raw": "the leading space is", + "range": [191, 211], + "loc": { + "start": { + "line": 10, + "column": 12 + }, + "end": { + "line": 10, + "column": 32 + } + } + }, + { + "type": "Str", + "raw": "\n", + "value": "\n", + "range": [211, 212], + "loc": { + "start": { + "line": 10, + "column": 32 + }, + "end": { + "line": 11, + "column": 0 + } + } + }, + { + "value": "also trimmed.", + "type": "Str", + "raw": "also trimmed.", + "range": [212, 225], + "loc": { + "start": { + "line": 11, + "column": 0 + }, + "end": { + "line": 11, + "column": 13 + } + } + } + ] } ], "type": "Document", - "raw": "`inline code`\n\naaa `inline code` bbb", - "range": [0, 36], + "raw": "`inline code`\n\naaa `inline code` bbb\n\nWhat is ```rust fn main()``` in Rust\nwould be ```c int main()``` in C.\n\nThis has ``` `backticks` ``` in it\n(but the spaces are trimmed). And\n``` here``` the leading space is\nalso trimmed.", + "range": [0, 225], "loc": { "start": { "line": 1, "column": 0 }, "end": { - "line": 3, - "column": 21 + "line": 11, + "column": 13 } } }