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

GO-4162 corrupted links in markdown import #1624

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
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
85 changes: 58 additions & 27 deletions core/block/import/markdown/blockconverter.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,35 +98,66 @@

func (m *mdConverter) processTextBlock(block *model.Block, files map[string]*FileInfo) {
txt := block.GetText()
if txt != nil && txt.Marks != nil && len(txt.Marks.Marks) == 1 &&
txt.Marks.Marks[0].Type == model.BlockContentTextMark_Link {
link := txt.Marks.Marks[0].Param
wholeLineLink := m.isWholeLineLink(txt)
ext := filepath.Ext(link)

// todo: bug with multiple markup links in arow when the first is external
if file := files[link]; file != nil {
if strings.EqualFold(ext, ".csv") {
m.processCSVFileLink(block, files, link, wholeLineLink)
if txt != nil && txt.Marks != nil {
if len(txt.Marks.Marks) == 1 && txt.Marks.Marks[0].Type == model.BlockContentTextMark_Link {
m.handleSingleMark(block, files)
} else {
m.handleSeveralMarks(block, files)
}
}
}

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)
// todo: bug with multiple markup links in arow when the first is external
if file := files[link]; file != nil {
if strings.EqualFold(ext, ".csv") {
m.processCSVFileLink(block, files, link, wholeLineLink)
return
}
if strings.EqualFold(ext, ".md") {
// only convert if this is the only link in the row
m.convertToAnytypeLinkBlock(block, wholeLineLink)
} else {
block.Content = anymark.ConvertTextToFile(txt.Marks.Marks[0].Param)
}
file.HasInboundLinks = true
} else if wholeLineLink {
block.Content = m.convertTextToBookmark(txt.Marks.Marks[0].Param)
}
}

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) {
block.Content = anymark.ConvertTextToFile(mark.Param)
return
}
} else if m.isWholeLineLink(txt.Text, mark) {
block.Content = m.convertTextToBookmark(mark.Param)
return
}
if strings.EqualFold(ext, ".md") {
// only convert if this is the only link in the row
m.convertToAnytypeLinkBlock(block, wholeLineLink)
} else {
anymark.ConvertTextToFile(block)
}
file.HasInboundLinks = true
} else if wholeLineLink {
m.convertTextToBookmark(block)
}
}
}

func (m *mdConverter) isWholeLineLink(txt *model.BlockContentText) bool {
func (m *mdConverter) isWholeLineLink(text string, marks *model.BlockContentTextMark) bool {
var wholeLineLink bool
textRunes := []rune(txt.Text)
var from, to = int(txt.Marks.Marks[0].Range.From), int(txt.Marks.Marks[0].Range.To)
textRunes := []rune(text)
var from, to = int(marks.Range.From), int(marks.Range.To)
if from == 0 || (from < len(textRunes) && len(strings.TrimSpace(string(textRunes[0:from]))) == 0) {
if to >= len(textRunes) || len(strings.TrimSpace(string(textRunes[to:]))) == 0 {
wholeLineLink = true
Expand Down Expand Up @@ -201,14 +232,14 @@
}
}

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest using real markdown test data here

testing small internal functions leads to fragile tests and don't cover the whole process of import

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)
})
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

сonsider use @deff7's testutil.BuildStateFromAST

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see advantages in using it, because we need just to manipulate text block

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,
},
},
},
},
},
}
}
Loading