Skip to content

Commit

Permalink
GO-4162: handle multiple markdown
Browse files Browse the repository at this point in the history
Signed-off-by: AnastasiaShemyakinskaya <shem98a@mail.ru>
  • Loading branch information
AnastasiaShemyakinskaya committed Oct 1, 2024
1 parent 81435d0 commit e1272b8
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 28 deletions.
2 changes: 1 addition & 1 deletion core/block/import/html/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (h *HTML) updateFilesInLinks(block *model.Block, filesSource source.Source,
if newFileName, createFileBlock, err = common.ProvideFileName(mark.Param, filesSource, path, h.tempDirProvider); err == nil {
mark.Param = newFileName
if createFileBlock {
anymark.ConvertTextToFile(block)
block.Content = anymark.ConvertTextToFile(mark.Param)
break
}
continue
Expand Down
23 changes: 11 additions & 12 deletions core/block/import/markdown/anymark/anyblocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ func provideCodeBlock(textArr []string, language string, id string) *model.Block
}
}

func ConvertTextToFile(block *model.Block) {
func ConvertTextToFile(filePath string) *model.BlockContentOfFile {
// "svg" excluded
if block.GetText().GetMarks().Marks[0].Param == "" {
return
if filePath == "" {
return nil
}

imageFormats := []string{"jpg", "jpeg", "png", "gif", "webp"}
Expand All @@ -104,7 +104,7 @@ func ConvertTextToFile(block *model.Block) {
pdfFormat := "pdf"

fileType := model.BlockContentFile_File
fileExt := filepath.Ext(block.GetText().GetMarks().Marks[0].Param)
fileExt := filepath.Ext(filePath)
if fileExt != "" {
fileExt = fileExt[1:]
for _, ext := range imageFormats {
Expand All @@ -131,14 +131,13 @@ func ConvertTextToFile(block *model.Block) {
if strings.EqualFold(fileExt, pdfFormat) {
fileType = model.BlockContentFile_PDF
}

block.Content = &model.BlockContentOfFile{
File: &model.BlockContentFile{
Name: block.GetText().GetMarks().Marks[0].Param,
State: model.BlockContentFile_Empty,
Type: fileType,
},
}
}
return &model.BlockContentOfFile{
File: &model.BlockContentFile{
Name: filePath,
State: model.BlockContentFile_Empty,
Type: fileType,
},
}
}

Expand Down
34 changes: 19 additions & 15 deletions core/block/import/markdown/blockconverter.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,15 @@ func (m *mdConverter) processTextBlock(block *model.Block, files map[string]*Fil
txt := block.GetText()
if txt != nil && txt.Marks != nil {
if len(txt.Marks.Marks) == 1 && txt.Marks.Marks[0].Type == model.BlockContentTextMark_Link {
m.handleSingleMark(block, files, txt)
m.handleSingleMark(block, files)
} else {
m.handleSeveralMarks(block, files, txt)
m.handleSeveralMarks(block, files)
}
}
}

func (m *mdConverter) handleSingleMark(block *model.Block, files map[string]*FileInfo, txt *model.BlockContentText) {
func (m *mdConverter) handleSingleMark(block *model.Block, files map[string]*FileInfo) {
txt := block.GetText()
link := txt.Marks.Marks[0].Param
wholeLineLink := m.isWholeLineLink(txt.Text, txt.Marks.Marks[0])
ext := filepath.Ext(link)
Expand All @@ -121,30 +122,33 @@ func (m *mdConverter) handleSingleMark(block *model.Block, files map[string]*Fil
// only convert if this is the only link in the row
m.convertToAnytypeLinkBlock(block, wholeLineLink)
} else {
anymark.ConvertTextToFile(block)
block.Content = anymark.ConvertTextToFile(txt.Marks.Marks[0].Param)
}
file.HasInboundLinks = true
} else if wholeLineLink {
m.convertTextToBookmark(block)
block.Content = m.convertTextToBookmark(txt.Marks.Marks[0].Param)
}
}

func (m *mdConverter) handleSeveralMarks(block *model.Block, files map[string]*FileInfo, txt *model.BlockContentText) {
func (m *mdConverter) handleSeveralMarks(block *model.Block, files map[string]*FileInfo) {
txt := block.GetText()
for _, mark := range txt.Marks.Marks {
if mark.Type == model.BlockContentTextMark_Link {

Check failure on line 136 in core/block/import/markdown/blockconverter.go

View workflow job for this annotation

GitHub Actions / lint

`if mark.Type == model.BlockContentTextMark_Link` has complex nested blocks (complexity: 6) (nestif)
link := mark.Param
ext := filepath.Ext(link)
if file := files[link]; file != nil {
file.HasInboundLinks = true
if strings.EqualFold(ext, ".md") || strings.EqualFold(ext, ".csv") {
mark.Type = model.BlockContentTextMark_Object
continue
}
if m.isWholeLineLink(txt.Text, mark) {
anymark.ConvertTextToFile(block)
break
block.Content = anymark.ConvertTextToFile(mark.Param)
return
}
file.HasInboundLinks = true
} else if m.isWholeLineLink(txt.Text, mark) {
m.convertTextToBookmark(block)
block.Content = m.convertTextToBookmark(mark.Param)
return
}
}
}
Expand Down Expand Up @@ -228,14 +232,14 @@ func (m *mdConverter) convertTextToPageLink(block *model.Block) {
}
}

func (m *mdConverter) convertTextToBookmark(block *model.Block) {
if err := uri.ValidateURI(block.GetText().Marks.Marks[0].Param); err != nil {
return
func (m *mdConverter) convertTextToBookmark(url string) *model.BlockContentOfBookmark {
if err := uri.ValidateURI(url); err != nil {
return nil
}

block.Content = &model.BlockContentOfBookmark{
return &model.BlockContentOfBookmark{
Bookmark: &model.BlockContentBookmark{
Url: block.GetText().Marks.Marks[0].Param,
Url: url,
},
}
}
Expand Down
152 changes: 152 additions & 0 deletions core/block/import/markdown/blockconverter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,155 @@ func Test_processFiles(t *testing.T) {
assert.Len(t, fileBlocks, 0)
})
}

func Test_processTextBlock(t *testing.T) {
t.Run("block with single markdown - file doesn't exist", func(t *testing.T) {
// given
mc := mdConverter{}
block := getTestTxtBlock("file.md")

// when
mc.processTextBlock(block, map[string]*FileInfo{})

// then
assert.NotNil(t, block.GetBookmark())
})
t.Run("block with single markdown - file exists", func(t *testing.T) {
mc := mdConverter{}
block := getTestTxtBlock("file.md")

// when
mc.processTextBlock(block, map[string]*FileInfo{"file.md": {PageID: "id"}})

// then
assert.NotNil(t, block.GetLink())
assert.Equal(t, "file.md", block.GetLink().GetTargetBlockId())
})
t.Run("block with single markdown - file exists, but not md or csv", func(t *testing.T) {
mc := mdConverter{}
block := getTestTxtBlock("file.txt")

// when
mc.processTextBlock(block, map[string]*FileInfo{"file.txt": {}})

// then
assert.NotNil(t, block.GetFile())
assert.Equal(t, "file.txt", block.GetFile().GetName())
})
t.Run("block with single markdown - csv file exists", func(t *testing.T) {
mc := mdConverter{}
block := getTestTxtBlock("file.csv")

// when
mc.processTextBlock(block, map[string]*FileInfo{"file.csv": {}})

// then
assert.NotNil(t, block.GetLink())
assert.Equal(t, "file.csv", block.GetLink().GetTargetBlockId())
})
t.Run("block with multiple markdown - file doesn't exist", func(t *testing.T) {
mc := mdConverter{}
block := getTestTxtBlockWithMultipleMarks("file.md")

// when
mc.processTextBlock(block, map[string]*FileInfo{})

// then
assert.NotNil(t, block.GetBookmark())
})
t.Run("block with multiple markdown - file exists", func(t *testing.T) {
mc := mdConverter{}
block := getTestTxtBlockWithMultipleMarks("file.md")

// when
mc.processTextBlock(block, map[string]*FileInfo{"file.md": {PageID: "id"}})

// then
assert.NotNil(t, block.GetText())
assert.Len(t, block.GetText().GetMarks().GetMarks(), 3)
assert.Equal(t, "file.md", block.GetText().GetMarks().GetMarks()[1].Param)
assert.Equal(t, model.BlockContentTextMark_Object, block.GetText().GetMarks().GetMarks()[1].Type)
})
t.Run("block with multiple markdown - file exists, but not md or csv", func(t *testing.T) {
mc := mdConverter{}
block := getTestTxtBlockWithMultipleMarks("file.txt")

// when
mc.processTextBlock(block, map[string]*FileInfo{"file.txt": {}})

// then
assert.NotNil(t, block.GetFile())
})
t.Run("block with multiple markdown - csv file exists", func(t *testing.T) {
mc := mdConverter{}
block := getTestTxtBlockWithMultipleMarks("file.csv")

// when
mc.processTextBlock(block, map[string]*FileInfo{"file.csv": {}})

// then
// then
assert.NotNil(t, block.GetText())
assert.Len(t, block.GetText().GetMarks().GetMarks(), 3)
assert.Equal(t, "file.csv", block.GetText().GetMarks().GetMarks()[1].Param)
assert.Equal(t, model.BlockContentTextMark_Object, block.GetText().GetMarks().GetMarks()[1].Type)
})
}

func getTestTxtBlock(filename string) *model.Block {
return &model.Block{
Content: &model.BlockContentOfText{
Text: &model.BlockContentText{
Text: "test",
Marks: &model.BlockContentTextMarks{
Marks: []*model.BlockContentTextMark{
{
Range: &model.Range{
From: 0,
To: 4,
},
Type: model.BlockContentTextMark_Link,
Param: filename,
},
},
},
},
},
}
}

func getTestTxtBlockWithMultipleMarks(filename string) *model.Block {
return &model.Block{
Content: &model.BlockContentOfText{
Text: &model.BlockContentText{
Text: "test",
Marks: &model.BlockContentTextMarks{
Marks: []*model.BlockContentTextMark{
{
Range: &model.Range{
From: 0,
To: 1,
},
Type: model.BlockContentTextMark_Bold,
},
{
Range: &model.Range{
From: 0,
To: 4,
},
Type: model.BlockContentTextMark_Link,
Param: filename,
},
{
Range: &model.Range{
From: 0,
To: 2,
},
Type: model.BlockContentTextMark_Italic,
},
},
},
},
},
}
}

0 comments on commit e1272b8

Please sign in to comment.