Skip to content

Commit

Permalink
fix: feishu service implicitly changed heading structure (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wsine authored Jan 24, 2024
1 parent 4341bf6 commit 404356f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 27 deletions.
33 changes: 24 additions & 9 deletions core/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,41 @@ import (
"testing"

"github.com/Wsine/feishu2md/core"
"github.com/Wsine/feishu2md/utils"
)

func getIdAndSecretFromEnv() (string, string) {
utils.LoadEnv()
appID := os.Getenv("FEISHU_APP_ID")
appSecret := os.Getenv("FEISHU_APP_SECRET")
func getIdAndSecretFromEnv(t *testing.T) (string, string) {
appID := ""
appSecret := ""

configPath, err := core.GetConfigFilePath()
if err != nil {
t.Error(err)
}
if _, err := os.Stat(configPath); os.IsNotExist(err) {
appID = os.Getenv("FEISHU_APP_ID")
appSecret = os.Getenv("FEISHU_APP_SECRET")
} else {
config, err := core.ReadConfigFromFile(configPath)
if err != nil {
t.Error(err)
}
appID = config.Feishu.AppId
appSecret = config.Feishu.AppSecret
}

return appID, appSecret
}

func TestNewClient(t *testing.T) {
appID, appSecret := getIdAndSecretFromEnv()
appID, appSecret := getIdAndSecretFromEnv(t)
c := core.NewClient(appID, appSecret, "feishu.cn")
if c == nil {
t.Errorf("Error creating DocClient")
}
}

func TestDownloadImage(t *testing.T) {
appID, appSecret := getIdAndSecretFromEnv()
appID, appSecret := getIdAndSecretFromEnv(t)
c := core.NewClient(appID, appSecret, "feishu.cn")
imgToken := "boxcnA1QKPanfMhLxzF1eMhoArM"
filename, err := c.DownloadImage(
Expand All @@ -47,7 +62,7 @@ func TestDownloadImage(t *testing.T) {
}

func TestGetDocxContent(t *testing.T) {
appID, appSecret := getIdAndSecretFromEnv()
appID, appSecret := getIdAndSecretFromEnv(t)
c := core.NewClient(appID, appSecret, "feishu.cn")
docx, blocks, err := c.GetDocxContent(
context.Background(),
Expand All @@ -67,7 +82,7 @@ func TestGetDocxContent(t *testing.T) {
}

func TestGetWikiNodeInfo(t *testing.T) {
appID, appSecret := getIdAndSecretFromEnv()
appID, appSecret := getIdAndSecretFromEnv(t)
c := core.NewClient(appID, appSecret, "feishu.cn")
const token = "wikcnLgRX9AMtvaB5x1cl57Yuah"
node, err := c.GetWikiNodeInfo(context.Background(), token)
Expand Down
45 changes: 27 additions & 18 deletions core/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
import (
"context"
"fmt"
"reflect"
"strings"

"github.com/Wsine/feishu2md/utils"
Expand Down Expand Up @@ -134,32 +135,23 @@ func (p *Parser) ParseDocxBlock(b *lark.DocxBlock, indentLevel int) string {
case lark.DocxBlockTypeText:
buf.WriteString(p.ParseDocxBlockText(b.Text))
case lark.DocxBlockTypeHeading1:
buf.WriteString("# ")
buf.WriteString(p.ParseDocxBlockText(b.Heading1))
buf.WriteString(p.ParseDocxBlockHeading(b, 1))
case lark.DocxBlockTypeHeading2:
buf.WriteString("## ")
buf.WriteString(p.ParseDocxBlockText(b.Heading2))
buf.WriteString(p.ParseDocxBlockHeading(b, 2))
case lark.DocxBlockTypeHeading3:
buf.WriteString("### ")
buf.WriteString(p.ParseDocxBlockText(b.Heading3))
buf.WriteString(p.ParseDocxBlockHeading(b, 3))
case lark.DocxBlockTypeHeading4:
buf.WriteString("#### ")
buf.WriteString(p.ParseDocxBlockText(b.Heading4))
buf.WriteString(p.ParseDocxBlockHeading(b, 4))
case lark.DocxBlockTypeHeading5:
buf.WriteString("##### ")
buf.WriteString(p.ParseDocxBlockText(b.Heading5))
buf.WriteString(p.ParseDocxBlockHeading(b, 5))
case lark.DocxBlockTypeHeading6:
buf.WriteString("###### ")
buf.WriteString(p.ParseDocxBlockText(b.Heading6))
buf.WriteString(p.ParseDocxBlockHeading(b, 6))
case lark.DocxBlockTypeHeading7:
buf.WriteString("####### ")
buf.WriteString(p.ParseDocxBlockText(b.Heading7))
buf.WriteString(p.ParseDocxBlockHeading(b, 7))
case lark.DocxBlockTypeHeading8:
buf.WriteString("######## ")
buf.WriteString(p.ParseDocxBlockText(b.Heading8))
buf.WriteString(p.ParseDocxBlockHeading(b, 8))
case lark.DocxBlockTypeHeading9:
buf.WriteString("######### ")
buf.WriteString(p.ParseDocxBlockText(b.Heading9))
buf.WriteString(p.ParseDocxBlockHeading(b, 9))
case lark.DocxBlockTypeBullet:
buf.WriteString(p.ParseDocxBlockBullet(b, indentLevel))
case lark.DocxBlockTypeOrdered:
Expand Down Expand Up @@ -296,6 +288,23 @@ func (p *Parser) ParseDocxTextElementTextRun(tr *lark.DocxTextElementTextRun) st
return buf.String()
}

func (p *Parser) ParseDocxBlockHeading(b *lark.DocxBlock, headingLevel int) string {
buf := new(strings.Builder)

buf.WriteString(strings.Repeat("#", headingLevel))
buf.WriteString(" ")

headingText := reflect.ValueOf(b).Elem().FieldByName(fmt.Sprintf("Heading%d", headingLevel))
buf.WriteString(p.ParseDocxBlockText(headingText.Interface().(*lark.DocxBlockText)))

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

return buf.String()
}

func (p *Parser) ParseDocxBlockImage(img *lark.DocxBlockImage) string {
buf := new(strings.Builder)
buf.WriteString(fmt.Sprintf("![](%s)", img.Token))
Expand Down

0 comments on commit 404356f

Please sign in to comment.