Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add cell merge #145

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions core/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,47 @@ func (c *Client) GetDriveFolderFileList(ctx context.Context, pageToken *string,
}
return files, nil
}

func (c *Client) GetWikiName(ctx context.Context, spaceID string) (string, error) {
resp, _, err := c.larkClient.Drive.GetWikiSpace(ctx, &lark.GetWikiSpaceReq{
SpaceID: spaceID,
})

if err != nil {
return "", err
}

return resp.Space.Name, nil
}

func (c *Client) GetWikiNodeList(ctx context.Context, spaceID string, parentNodeToken *string) ([]*lark.GetWikiNodeListRespItem, error) {
resp, _, err := c.larkClient.Drive.GetWikiNodeList(ctx, &lark.GetWikiNodeListReq{
SpaceID: spaceID,
PageSize: nil,
PageToken: nil,
ParentNodeToken: parentNodeToken,
})

if err != nil {
return nil, err
}

nodes := resp.Items

for resp.HasMore {
resp, _, err := c.larkClient.Drive.GetWikiNodeList(ctx, &lark.GetWikiNodeListReq{
SpaceID: spaceID,
PageSize: nil,
PageToken: nil,
ParentNodeToken: parentNodeToken,
})

if err != nil {
return nil, err
}

nodes = append(nodes, resp.Items...)
}

return nodes, nil
}
81 changes: 76 additions & 5 deletions core/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ func (p *Parser) ParseDocxBlock(b *lark.DocxBlock, indentLevel int) string {
buf.WriteString(p.ParseDocxBlockPage(b))
case lark.DocxBlockTypeText:
buf.WriteString(p.ParseDocxBlockText(b.Text))
case lark.DocxBlockTypeCallout:
buf.WriteString(p.ParseDocxBlockCallout(b))
case lark.DocxBlockTypeHeading1:
buf.WriteString(p.ParseDocxBlockHeading(b, 1))
case lark.DocxBlockTypeHeading2:
Expand Down Expand Up @@ -217,6 +219,18 @@ func (p *Parser) ParseDocxBlockText(b *lark.DocxBlockText) string {
return buf.String()
}

func (p *Parser) ParseDocxBlockCallout(b *lark.DocxBlock) string {
buf := new(strings.Builder)

buf.WriteString(">[!TIP] \n")

for _, childId := range b.Children {
childBlock := p.blockMap[childId]
buf.WriteString(p.ParseDocxBlock(childBlock, 0))
}

return buf.String()
}
func (p *Parser) ParseDocxTextElement(e *lark.DocxTextElement, inline bool) string {
buf := new(strings.Builder)
if e.TextRun != nil {
Expand Down Expand Up @@ -364,7 +378,7 @@ func (p *Parser) ParseDocxBlockTableCell(b *lark.DocxBlock) string {
for _, child := range b.Children {
block := p.blockMap[child]
content := p.ParseDocxBlock(block, 0)
buf.WriteString(content)
buf.WriteString(content + "<br/>")
}

return buf.String()
Expand All @@ -374,20 +388,76 @@ func (p *Parser) ParseDocxBlockTable(t *lark.DocxBlockTable) string {
// - First row as header
// - Ignore cell merging
var rows [][]string
mergeInfoMap := map[int64]map[int64]*lark.DocxBlockTablePropertyMergeInfo{}

// 构建单元格合并信息的映射
if t.Property.MergeInfo != nil {
for i, merge := range t.Property.MergeInfo {
rowIndex := int64(i) / t.Property.ColumnSize
colIndex := int64(i) % t.Property.ColumnSize
if _, exists := mergeInfoMap[int64(rowIndex)]; !exists {
mergeInfoMap[int64(rowIndex)] = map[int64]*lark.DocxBlockTablePropertyMergeInfo{}
}
mergeInfoMap[rowIndex][colIndex] = merge
}
}
for i, blockId := range t.Cells {
block := p.blockMap[blockId]
cellContent := p.ParseDocxBlock(block, 0)
cellContent = strings.ReplaceAll(cellContent, "\n", "")
rowIndex := int64(i) / t.Property.ColumnSize
if len(rows) < int(rowIndex)+1 {
colIndex := int64(i) % t.Property.ColumnSize
// 初始化行
for len(rows) <= int(rowIndex) {
rows = append(rows, []string{})
}
rows[rowIndex] = append(rows[rowIndex], cellContent)
for len(rows[rowIndex]) <= int(colIndex) {
rows[rowIndex] = append(rows[rowIndex], "")
}
// 设置单元格内容
rows[rowIndex][colIndex] = cellContent
}

// 渲染为 HTML 表格
buf := new(strings.Builder)
buf.WriteString(renderMarkdownTable(rows))
buf.WriteString("\n")
buf.WriteString("<table>\n")

// 跟踪已经处理过的合并单元格
processedCells := map[string]bool{}

// 构建 HTML 表格内容
for rowIndex, row := range rows {
buf.WriteString("<tr>\n")
for colIndex, cellContent := range row {
cellKey := fmt.Sprintf("%d-%d", rowIndex, colIndex)

// 跳过已处理的单元格
if processedCells[cellKey] {
continue
}

mergeInfo := mergeInfoMap[int64(rowIndex)][int64(colIndex)]
if mergeInfo != nil {
// 合并单元格
buf.WriteString(fmt.Sprintf(
`<td rowspan="%d" colspan="%d">%s</td>`,
mergeInfo.RowSpan, mergeInfo.ColSpan, cellContent,
))
// 标记合并范围内的所有单元格为已处理
for r := rowIndex; r < rowIndex+int(mergeInfo.RowSpan); r++ {
for c := colIndex; c < colIndex+int(mergeInfo.ColSpan); c++ {
processedCells[fmt.Sprintf("%d-%d", r, c)] = true
}
}
} else {
// 普通单元格
buf.WriteString(fmt.Sprintf("<td>%s</td>", cellContent))
}
}
buf.WriteString("</tr>\n")
}
buf.WriteString("</table>\n")

return buf.String()
}

Expand Down Expand Up @@ -416,3 +486,4 @@ func (p *Parser) ParseDocxBlockGrid(b *lark.DocxBlock, indentLevel int) string {

return buf.String()
}

Loading