mirror of
https://github.com/usememos/memos.git
synced 2025-04-03 04:11:20 +02:00
chore: update table node delimiter
This commit is contained in:
parent
05c0aeb789
commit
8ed827cd2d
@ -172,10 +172,8 @@ func convertToASTNode(node *apiv2pb.Node) ast.Node {
|
|||||||
|
|
||||||
func convertTableToASTNode(node *apiv2pb.Node) *ast.Table {
|
func convertTableToASTNode(node *apiv2pb.Node) *ast.Table {
|
||||||
table := &ast.Table{
|
table := &ast.Table{
|
||||||
Header: node.GetTableNode().Header,
|
Header: node.GetTableNode().Header,
|
||||||
}
|
Delimiter: node.GetTableNode().Delimiter,
|
||||||
for _, d := range node.GetTableNode().Delimiter {
|
|
||||||
table.Delimiter = append(table.Delimiter, int(d))
|
|
||||||
}
|
}
|
||||||
for _, row := range node.GetTableNode().Rows {
|
for _, row := range node.GetTableNode().Rows {
|
||||||
table.Rows = append(table.Rows, row.Cells)
|
table.Rows = append(table.Rows, row.Cells)
|
||||||
@ -185,10 +183,8 @@ func convertTableToASTNode(node *apiv2pb.Node) *ast.Table {
|
|||||||
|
|
||||||
func convertTableFromASTNode(node *ast.Table) *apiv2pb.TableNode {
|
func convertTableFromASTNode(node *ast.Table) *apiv2pb.TableNode {
|
||||||
table := &apiv2pb.TableNode{
|
table := &apiv2pb.TableNode{
|
||||||
Header: node.Header,
|
Header: node.Header,
|
||||||
}
|
Delimiter: node.Delimiter,
|
||||||
for _, d := range node.Delimiter {
|
|
||||||
table.Delimiter = append(table.Delimiter, int32(d))
|
|
||||||
}
|
}
|
||||||
for _, row := range node.Rows {
|
for _, row := range node.Rows {
|
||||||
table.Rows = append(table.Rows, &apiv2pb.TableNode_Row{Cells: row})
|
table.Rows = append(table.Rows, &apiv2pb.TableNode_Row{Cells: row})
|
||||||
|
@ -198,9 +198,8 @@ func (n *MathBlock) Restore() string {
|
|||||||
type Table struct {
|
type Table struct {
|
||||||
BaseBlock
|
BaseBlock
|
||||||
|
|
||||||
Header []string
|
Header []string
|
||||||
// Delimiter is the list of delimiter counts.
|
Delimiter []string
|
||||||
Delimiter []int
|
|
||||||
Rows [][]string
|
Rows [][]string
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -215,11 +214,7 @@ func (n *Table) Restore() string {
|
|||||||
}
|
}
|
||||||
result += "|\n"
|
result += "|\n"
|
||||||
for _, d := range n.Delimiter {
|
for _, d := range n.Delimiter {
|
||||||
symbol := ""
|
result += fmt.Sprintf("| %s ", d)
|
||||||
for i := 0; i < d; i++ {
|
|
||||||
symbol += "-"
|
|
||||||
}
|
|
||||||
result += fmt.Sprintf("| %s ", symbol)
|
|
||||||
}
|
}
|
||||||
result += "|\n"
|
result += "|\n"
|
||||||
for index, row := range n.Rows {
|
for index, row := range n.Rows {
|
||||||
|
@ -26,21 +26,21 @@ func (*TableParser) Match(tokens []*tokenizer.Token) (int, bool) {
|
|||||||
return 0, false
|
return 0, false
|
||||||
}
|
}
|
||||||
|
|
||||||
alignTokens := []*tokenizer.Token{}
|
delimiterTokens := []*tokenizer.Token{}
|
||||||
for _, token := range tokens[len(headerTokens)+1:] {
|
for _, token := range tokens[len(headerTokens)+1:] {
|
||||||
if token.Type == tokenizer.Newline {
|
if token.Type == tokenizer.Newline {
|
||||||
break
|
break
|
||||||
} else {
|
} else {
|
||||||
alignTokens = append(alignTokens, token)
|
delimiterTokens = append(delimiterTokens, token)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(alignTokens) < 5 || len(tokens) < len(headerTokens)+len(alignTokens)+3 {
|
if len(delimiterTokens) < 5 || len(tokens) < len(headerTokens)+len(delimiterTokens)+3 {
|
||||||
return 0, false
|
return 0, false
|
||||||
}
|
}
|
||||||
|
|
||||||
rowTokens := []*tokenizer.Token{}
|
rowTokens := []*tokenizer.Token{}
|
||||||
for index, token := range tokens[len(headerTokens)+len(alignTokens)+2:] {
|
for index, token := range tokens[len(headerTokens)+len(delimiterTokens)+2:] {
|
||||||
temp := len(headerTokens) + len(alignTokens) + 2 + index
|
temp := len(headerTokens) + len(delimiterTokens) + 2 + index
|
||||||
if token.Type == tokenizer.Newline && temp != len(tokens)-1 && tokens[temp+1].Type != tokenizer.Pipe {
|
if token.Type == tokenizer.Newline && temp != len(tokens)-1 && tokens[temp+1].Type != tokenizer.Pipe {
|
||||||
break
|
break
|
||||||
} else {
|
} else {
|
||||||
@ -60,20 +60,23 @@ func (*TableParser) Match(tokens []*tokenizer.Token) (int, bool) {
|
|||||||
return 0, false
|
return 0, false
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check align.
|
// Check delimiter.
|
||||||
if len(alignTokens) < 5 {
|
if len(delimiterTokens) < 5 {
|
||||||
return 0, false
|
return 0, false
|
||||||
}
|
}
|
||||||
alignCells, ok := matchTableCellTokens(alignTokens)
|
alignCells, ok := matchTableCellTokens(delimiterTokens)
|
||||||
if alignCells != headerCells || !ok {
|
if alignCells != headerCells || !ok {
|
||||||
return 0, false
|
return 0, false
|
||||||
}
|
}
|
||||||
for _, t := range tokenizer.Split(alignTokens, tokenizer.Pipe) {
|
for _, t := range tokenizer.Split(delimiterTokens, tokenizer.Pipe) {
|
||||||
delimiterTokens := t[1 : len(t)-1]
|
delimiterTokens := t[1 : len(t)-1]
|
||||||
if len(delimiterTokens) < 3 {
|
if len(delimiterTokens) < 3 {
|
||||||
return 0, false
|
return 0, false
|
||||||
}
|
}
|
||||||
for _, token := range delimiterTokens {
|
if (delimiterTokens[0].Type != tokenizer.Colon && delimiterTokens[0].Type != tokenizer.Hyphen) || (delimiterTokens[len(delimiterTokens)-1].Type != tokenizer.Colon && delimiterTokens[len(delimiterTokens)-1].Type != tokenizer.Hyphen) {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
for _, token := range delimiterTokens[1 : len(delimiterTokens)-1] {
|
||||||
if token.Type != tokenizer.Hyphen {
|
if token.Type != tokenizer.Hyphen {
|
||||||
return 0, false
|
return 0, false
|
||||||
}
|
}
|
||||||
@ -95,7 +98,7 @@ func (*TableParser) Match(tokens []*tokenizer.Token) (int, bool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return len(headerTokens) + len(alignTokens) + len(rowTokens) + 2, true
|
return len(headerTokens) + len(delimiterTokens) + len(rowTokens) + 2, true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *TableParser) Parse(tokens []*tokenizer.Token) (ast.Node, error) {
|
func (p *TableParser) Parse(tokens []*tokenizer.Token) (ast.Node, error) {
|
||||||
@ -109,14 +112,14 @@ func (p *TableParser) Parse(tokens []*tokenizer.Token) (ast.Node, error) {
|
|||||||
alignTokens := rawRows[1]
|
alignTokens := rawRows[1]
|
||||||
rowTokens := rawRows[2:]
|
rowTokens := rawRows[2:]
|
||||||
header := make([]string, 0)
|
header := make([]string, 0)
|
||||||
delimiter := make([]int, 0)
|
delimiter := make([]string, 0)
|
||||||
rows := make([][]string, 0)
|
rows := make([][]string, 0)
|
||||||
|
|
||||||
for _, t := range tokenizer.Split(headerTokens, tokenizer.Pipe) {
|
for _, t := range tokenizer.Split(headerTokens, tokenizer.Pipe) {
|
||||||
header = append(header, tokenizer.Stringify(t[1:len(t)-1]))
|
header = append(header, tokenizer.Stringify(t[1:len(t)-1]))
|
||||||
}
|
}
|
||||||
for _, t := range tokenizer.Split(alignTokens, tokenizer.Pipe) {
|
for _, t := range tokenizer.Split(alignTokens, tokenizer.Pipe) {
|
||||||
delimiter = append(delimiter, len(t[1:len(t)-1]))
|
delimiter = append(delimiter, tokenizer.Stringify(t[1:len(t)-1]))
|
||||||
}
|
}
|
||||||
for _, row := range rowTokens {
|
for _, row := range rowTokens {
|
||||||
cells := make([]string, 0)
|
cells := make([]string, 0)
|
||||||
|
@ -18,7 +18,7 @@ func TestTableParser(t *testing.T) {
|
|||||||
text: "| header |\n| --- |\n| cell |\n",
|
text: "| header |\n| --- |\n| cell |\n",
|
||||||
table: &ast.Table{
|
table: &ast.Table{
|
||||||
Header: []string{"header"},
|
Header: []string{"header"},
|
||||||
Delimiter: []int{3},
|
Delimiter: []string{"---"},
|
||||||
Rows: [][]string{
|
Rows: [][]string{
|
||||||
{"cell"},
|
{"cell"},
|
||||||
},
|
},
|
||||||
@ -28,7 +28,18 @@ func TestTableParser(t *testing.T) {
|
|||||||
text: "| header1 | header2 |\n| --- | ---- |\n| cell1 | cell2 |\n| cell3 | cell4 |",
|
text: "| header1 | header2 |\n| --- | ---- |\n| cell1 | cell2 |\n| cell3 | cell4 |",
|
||||||
table: &ast.Table{
|
table: &ast.Table{
|
||||||
Header: []string{"header1", "header2"},
|
Header: []string{"header1", "header2"},
|
||||||
Delimiter: []int{3, 4},
|
Delimiter: []string{"---", "----"},
|
||||||
|
Rows: [][]string{
|
||||||
|
{"cell1", "cell2"},
|
||||||
|
{"cell3", "cell4"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: "| header1 | header2 |\n| :-- | ----: |\n| cell1 | cell2 |\n| cell3 | cell4 |",
|
||||||
|
table: &ast.Table{
|
||||||
|
Header: []string{"header1", "header2"},
|
||||||
|
Delimiter: []string{":--", "----:"},
|
||||||
Rows: [][]string{
|
Rows: [][]string{
|
||||||
{"cell1", "cell2"},
|
{"cell1", "cell2"},
|
||||||
{"cell3", "cell4"},
|
{"cell3", "cell4"},
|
||||||
|
@ -21,6 +21,7 @@ const (
|
|||||||
DollarSign TokenType = "$"
|
DollarSign TokenType = "$"
|
||||||
EqualSign TokenType = "="
|
EqualSign TokenType = "="
|
||||||
Pipe TokenType = "|"
|
Pipe TokenType = "|"
|
||||||
|
Colon TokenType = ":"
|
||||||
Backslash TokenType = "\\"
|
Backslash TokenType = "\\"
|
||||||
Newline TokenType = "\n"
|
Newline TokenType = "\n"
|
||||||
Space TokenType = " "
|
Space TokenType = " "
|
||||||
@ -83,6 +84,8 @@ func Tokenize(text string) []*Token {
|
|||||||
tokens = append(tokens, NewToken(EqualSign, "="))
|
tokens = append(tokens, NewToken(EqualSign, "="))
|
||||||
case '|':
|
case '|':
|
||||||
tokens = append(tokens, NewToken(Pipe, "|"))
|
tokens = append(tokens, NewToken(Pipe, "|"))
|
||||||
|
case ':':
|
||||||
|
tokens = append(tokens, NewToken(Colon, ":"))
|
||||||
case '\\':
|
case '\\':
|
||||||
tokens = append(tokens, NewToken(Backslash, `\`))
|
tokens = append(tokens, NewToken(Backslash, `\`))
|
||||||
case '\n':
|
case '\n':
|
||||||
@ -142,4 +145,4 @@ func Split(tokens []*Token, delimiter TokenType) [][]*Token {
|
|||||||
result = append(result, current)
|
result = append(result, current)
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
@ -130,7 +130,7 @@ message MathBlockNode {
|
|||||||
|
|
||||||
message TableNode {
|
message TableNode {
|
||||||
repeated string header = 1;
|
repeated string header = 1;
|
||||||
repeated int32 delimiter = 2;
|
repeated string delimiter = 2;
|
||||||
|
|
||||||
message Row {
|
message Row {
|
||||||
repeated string cells = 1;
|
repeated string cells = 1;
|
||||||
|
@ -1330,7 +1330,7 @@
|
|||||||
| Field | Type | Label | Description |
|
| Field | Type | Label | Description |
|
||||||
| ----- | ---- | ----- | ----------- |
|
| ----- | ---- | ----- | ----------- |
|
||||||
| header | [string](#string) | repeated | |
|
| header | [string](#string) | repeated | |
|
||||||
| delimiter | [int32](#int32) | repeated | |
|
| delimiter | [string](#string) | repeated | |
|
||||||
| rows | [TableNode.Row](#memos-api-v2-TableNode-Row) | repeated | |
|
| rows | [TableNode.Row](#memos-api-v2-TableNode-Row) | repeated | |
|
||||||
|
|
||||||
|
|
||||||
|
@ -1166,7 +1166,7 @@ type TableNode struct {
|
|||||||
unknownFields protoimpl.UnknownFields
|
unknownFields protoimpl.UnknownFields
|
||||||
|
|
||||||
Header []string `protobuf:"bytes,1,rep,name=header,proto3" json:"header,omitempty"`
|
Header []string `protobuf:"bytes,1,rep,name=header,proto3" json:"header,omitempty"`
|
||||||
Delimiter []int32 `protobuf:"varint,2,rep,packed,name=delimiter,proto3" json:"delimiter,omitempty"`
|
Delimiter []string `protobuf:"bytes,2,rep,name=delimiter,proto3" json:"delimiter,omitempty"`
|
||||||
Rows []*TableNode_Row `protobuf:"bytes,3,rep,name=rows,proto3" json:"rows,omitempty"`
|
Rows []*TableNode_Row `protobuf:"bytes,3,rep,name=rows,proto3" json:"rows,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1209,7 +1209,7 @@ func (x *TableNode) GetHeader() []string {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (x *TableNode) GetDelimiter() []int32 {
|
func (x *TableNode) GetDelimiter() []string {
|
||||||
if x != nil {
|
if x != nil {
|
||||||
return x.Delimiter
|
return x.Delimiter
|
||||||
}
|
}
|
||||||
@ -2101,7 +2101,7 @@ var file_api_v2_markdown_service_proto_rawDesc = []byte{
|
|||||||
0x6c, 0x65, 0x4e, 0x6f, 0x64, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72,
|
0x6c, 0x65, 0x4e, 0x6f, 0x64, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72,
|
||||||
0x18, 0x01, 0x20, 0x03, 0x28, 0x09, 0x52, 0x06, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x12, 0x1c,
|
0x18, 0x01, 0x20, 0x03, 0x28, 0x09, 0x52, 0x06, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x12, 0x1c,
|
||||||
0x0a, 0x09, 0x64, 0x65, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x65, 0x72, 0x18, 0x02, 0x20, 0x03, 0x28,
|
0x0a, 0x09, 0x64, 0x65, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x65, 0x72, 0x18, 0x02, 0x20, 0x03, 0x28,
|
||||||
0x05, 0x52, 0x09, 0x64, 0x65, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x65, 0x72, 0x12, 0x2f, 0x0a, 0x04,
|
0x09, 0x52, 0x09, 0x64, 0x65, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x65, 0x72, 0x12, 0x2f, 0x0a, 0x04,
|
||||||
0x72, 0x6f, 0x77, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x1b, 0x2e, 0x6d, 0x65, 0x6d,
|
0x72, 0x6f, 0x77, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x1b, 0x2e, 0x6d, 0x65, 0x6d,
|
||||||
0x6f, 0x73, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x32, 0x2e, 0x54, 0x61, 0x62, 0x6c, 0x65, 0x4e,
|
0x6f, 0x73, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x32, 0x2e, 0x54, 0x61, 0x62, 0x6c, 0x65, 0x4e,
|
||||||
0x6f, 0x64, 0x65, 0x2e, 0x52, 0x6f, 0x77, 0x52, 0x04, 0x72, 0x6f, 0x77, 0x73, 0x1a, 0x1b, 0x0a,
|
0x6f, 0x64, 0x65, 0x2e, 0x52, 0x6f, 0x77, 0x52, 0x04, 0x72, 0x6f, 0x77, 0x73, 0x1a, 0x1b, 0x0a,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user