Skip to content

Commit 404356f

Browse files
authored
fix: feishu service implicitly changed heading structure (#102)
1 parent 4341bf6 commit 404356f

File tree

2 files changed

+51
-27
lines changed

2 files changed

+51
-27
lines changed

core/client_test.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,41 @@ import (
77
"testing"
88

99
"github.com/Wsine/feishu2md/core"
10-
"github.com/Wsine/feishu2md/utils"
1110
)
1211

13-
func getIdAndSecretFromEnv() (string, string) {
14-
utils.LoadEnv()
15-
appID := os.Getenv("FEISHU_APP_ID")
16-
appSecret := os.Getenv("FEISHU_APP_SECRET")
12+
func getIdAndSecretFromEnv(t *testing.T) (string, string) {
13+
appID := ""
14+
appSecret := ""
15+
16+
configPath, err := core.GetConfigFilePath()
17+
if err != nil {
18+
t.Error(err)
19+
}
20+
if _, err := os.Stat(configPath); os.IsNotExist(err) {
21+
appID = os.Getenv("FEISHU_APP_ID")
22+
appSecret = os.Getenv("FEISHU_APP_SECRET")
23+
} else {
24+
config, err := core.ReadConfigFromFile(configPath)
25+
if err != nil {
26+
t.Error(err)
27+
}
28+
appID = config.Feishu.AppId
29+
appSecret = config.Feishu.AppSecret
30+
}
31+
1732
return appID, appSecret
1833
}
1934

2035
func TestNewClient(t *testing.T) {
21-
appID, appSecret := getIdAndSecretFromEnv()
36+
appID, appSecret := getIdAndSecretFromEnv(t)
2237
c := core.NewClient(appID, appSecret, "feishu.cn")
2338
if c == nil {
2439
t.Errorf("Error creating DocClient")
2540
}
2641
}
2742

2843
func TestDownloadImage(t *testing.T) {
29-
appID, appSecret := getIdAndSecretFromEnv()
44+
appID, appSecret := getIdAndSecretFromEnv(t)
3045
c := core.NewClient(appID, appSecret, "feishu.cn")
3146
imgToken := "boxcnA1QKPanfMhLxzF1eMhoArM"
3247
filename, err := c.DownloadImage(
@@ -47,7 +62,7 @@ func TestDownloadImage(t *testing.T) {
4762
}
4863

4964
func TestGetDocxContent(t *testing.T) {
50-
appID, appSecret := getIdAndSecretFromEnv()
65+
appID, appSecret := getIdAndSecretFromEnv(t)
5166
c := core.NewClient(appID, appSecret, "feishu.cn")
5267
docx, blocks, err := c.GetDocxContent(
5368
context.Background(),
@@ -67,7 +82,7 @@ func TestGetDocxContent(t *testing.T) {
6782
}
6883

6984
func TestGetWikiNodeInfo(t *testing.T) {
70-
appID, appSecret := getIdAndSecretFromEnv()
85+
appID, appSecret := getIdAndSecretFromEnv(t)
7186
c := core.NewClient(appID, appSecret, "feishu.cn")
7287
const token = "wikcnLgRX9AMtvaB5x1cl57Yuah"
7388
node, err := c.GetWikiNodeInfo(context.Background(), token)

core/parser.go

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package core
33
import (
44
"context"
55
"fmt"
6+
"reflect"
67
"strings"
78

89
"github.com/Wsine/feishu2md/utils"
@@ -134,32 +135,23 @@ func (p *Parser) ParseDocxBlock(b *lark.DocxBlock, indentLevel int) string {
134135
case lark.DocxBlockTypeText:
135136
buf.WriteString(p.ParseDocxBlockText(b.Text))
136137
case lark.DocxBlockTypeHeading1:
137-
buf.WriteString("# ")
138-
buf.WriteString(p.ParseDocxBlockText(b.Heading1))
138+
buf.WriteString(p.ParseDocxBlockHeading(b, 1))
139139
case lark.DocxBlockTypeHeading2:
140-
buf.WriteString("## ")
141-
buf.WriteString(p.ParseDocxBlockText(b.Heading2))
140+
buf.WriteString(p.ParseDocxBlockHeading(b, 2))
142141
case lark.DocxBlockTypeHeading3:
143-
buf.WriteString("### ")
144-
buf.WriteString(p.ParseDocxBlockText(b.Heading3))
142+
buf.WriteString(p.ParseDocxBlockHeading(b, 3))
145143
case lark.DocxBlockTypeHeading4:
146-
buf.WriteString("#### ")
147-
buf.WriteString(p.ParseDocxBlockText(b.Heading4))
144+
buf.WriteString(p.ParseDocxBlockHeading(b, 4))
148145
case lark.DocxBlockTypeHeading5:
149-
buf.WriteString("##### ")
150-
buf.WriteString(p.ParseDocxBlockText(b.Heading5))
146+
buf.WriteString(p.ParseDocxBlockHeading(b, 5))
151147
case lark.DocxBlockTypeHeading6:
152-
buf.WriteString("###### ")
153-
buf.WriteString(p.ParseDocxBlockText(b.Heading6))
148+
buf.WriteString(p.ParseDocxBlockHeading(b, 6))
154149
case lark.DocxBlockTypeHeading7:
155-
buf.WriteString("####### ")
156-
buf.WriteString(p.ParseDocxBlockText(b.Heading7))
150+
buf.WriteString(p.ParseDocxBlockHeading(b, 7))
157151
case lark.DocxBlockTypeHeading8:
158-
buf.WriteString("######## ")
159-
buf.WriteString(p.ParseDocxBlockText(b.Heading8))
152+
buf.WriteString(p.ParseDocxBlockHeading(b, 8))
160153
case lark.DocxBlockTypeHeading9:
161-
buf.WriteString("######### ")
162-
buf.WriteString(p.ParseDocxBlockText(b.Heading9))
154+
buf.WriteString(p.ParseDocxBlockHeading(b, 9))
163155
case lark.DocxBlockTypeBullet:
164156
buf.WriteString(p.ParseDocxBlockBullet(b, indentLevel))
165157
case lark.DocxBlockTypeOrdered:
@@ -296,6 +288,23 @@ func (p *Parser) ParseDocxTextElementTextRun(tr *lark.DocxTextElementTextRun) st
296288
return buf.String()
297289
}
298290

291+
func (p *Parser) ParseDocxBlockHeading(b *lark.DocxBlock, headingLevel int) string {
292+
buf := new(strings.Builder)
293+
294+
buf.WriteString(strings.Repeat("#", headingLevel))
295+
buf.WriteString(" ")
296+
297+
headingText := reflect.ValueOf(b).Elem().FieldByName(fmt.Sprintf("Heading%d", headingLevel))
298+
buf.WriteString(p.ParseDocxBlockText(headingText.Interface().(*lark.DocxBlockText)))
299+
300+
for _, childId := range b.Children {
301+
childBlock := p.blockMap[childId]
302+
buf.WriteString(p.ParseDocxBlock(childBlock, 0))
303+
}
304+
305+
return buf.String()
306+
}
307+
299308
func (p *Parser) ParseDocxBlockImage(img *lark.DocxBlockImage) string {
300309
buf := new(strings.Builder)
301310
buf.WriteString(fmt.Sprintf("![](%s)", img.Token))

0 commit comments

Comments
 (0)