From 9c87d3afc3d95a354cdbcc4f0ec0020f68de3d22 Mon Sep 17 00:00:00 2001 From: D0000M Date: Wed, 30 Oct 2024 15:09:04 +0800 Subject: [PATCH 1/3] add test \|\| in table --- parser/tests/table_test.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/parser/tests/table_test.go b/parser/tests/table_test.go index da55fcd..34d2ba4 100644 --- a/parser/tests/table_test.go +++ b/parser/tests/table_test.go @@ -85,6 +85,28 @@ func TestTableParser(t *testing.T) { }, }, }, + { + text: "| header |\n| --- |\n| cell\\|\\|cell |\n", + node: &ast.Table{ + Header: []ast.Node{ + &ast.Paragraph{ + Children: []ast.Node{ + &ast.Text{Content: "header"}, + }, + }, + }, + Delimiter: []string{"---"}, + Rows: [][]ast.Node{ + { + &ast.Paragraph{ + Children: []ast.Node{ + &ast.Text{Content: "cell||cell"}, + }, + }, + }, + }, + }, + }, } for _, test := range tests { From d65f282d87f7a85a0992fc907009b2e99cbab104 Mon Sep 17 00:00:00 2001 From: D0000M Date: Wed, 30 Oct 2024 16:16:11 +0800 Subject: [PATCH 2/3] fix issue#9 v0.1 --- parser/table.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/parser/table.go b/parser/table.go index 645e24c..171b08d 100644 --- a/parser/table.go +++ b/parser/table.go @@ -153,10 +153,23 @@ func matchTableCellTokens(tokens []*tokenizer.Token) (int, bool) { } pipes := 0 + escapePipe := false + var preToken *tokenizer.Token = nil for _, token := range tokens { - if token.Type == tokenizer.Pipe { + if (token.Type == tokenizer.Pipe) && !escapePipe { pipes++ + } else if (token.Type == tokenizer.Pipe) && escapePipe { + token.Type = "" + token.Value = "|" + preToken.Type = "" + preToken.Value = "" } + if token.Type == tokenizer.Backslash { + escapePipe = true + } else { + escapePipe = false + } + preToken = token } cells := tokenizer.Split(tokens, tokenizer.Pipe) if len(cells) != pipes+1 { From 4ec8ed2ebc5d57e633fea5013a8515e030f46fdc Mon Sep 17 00:00:00 2001 From: D0000M Date: Wed, 30 Oct 2024 16:21:34 +0800 Subject: [PATCH 3/3] fix issue#9 v1 --- parser/table.go | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/parser/table.go b/parser/table.go index 171b08d..36d7977 100644 --- a/parser/table.go +++ b/parser/table.go @@ -153,23 +153,25 @@ func matchTableCellTokens(tokens []*tokenizer.Token) (int, bool) { } pipes := 0 - escapePipe := false - var preToken *tokenizer.Token = nil - for _, token := range tokens { - if (token.Type == tokenizer.Pipe) && !escapePipe { - pipes++ - } else if (token.Type == tokenizer.Pipe) && escapePipe { - token.Type = "" - token.Value = "|" - preToken.Type = "" - preToken.Value = "" - } + escapeNextPipe := false + for i, token := range tokens { if token.Type == tokenizer.Backslash { - escapePipe = true + escapeNextPipe = true + continue + } + if token.Type == tokenizer.Pipe { + if escapeNextPipe { + tokens[i].Type = "" + tokens[i].Value = "|" + tokens[i-1].Type = "" + tokens[i-1].Value = "" + escapeNextPipe = false + } else { + pipes++ + } } else { - escapePipe = false + escapeNextPipe = false } - preToken = token } cells := tokenizer.Split(tokens, tokenizer.Pipe) if len(cells) != pipes+1 {