Skip to content

Commit

Permalink
Merge pull request #1913 from anyproto/go-4574-importing-markdown-fil…
Browse files Browse the repository at this point in the history
…e-that-contains-broken-link-makes-app

GO-4574 importing markdown file that contains broken link makes app crash
  • Loading branch information
requilence authored Dec 7, 2024
2 parents 000ee08 + 2f53d7d commit efa8c8d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
10 changes: 5 additions & 5 deletions core/block/import/markdown/blockconverter.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (m *mdConverter) handleSingleMark(block *model.Block, files map[string]*Fil
}
file.HasInboundLinks = true
} else if wholeLineLink {
block.Content = m.convertTextToBookmark(txt.Marks.Marks[0].Param)
m.convertTextToBookmark(txt.Marks.Marks[0].Param, block)
}
}

Expand Down Expand Up @@ -154,7 +154,7 @@ func (m *mdConverter) handleSingleLinkMark(block *model.Block, files map[string]
return true
}
} else if m.isWholeLineLink(txt.Text, mark) {
block.Content = m.convertTextToBookmark(mark.Param)
m.convertTextToBookmark(mark.Param, block)
return true
}
return false
Expand Down Expand Up @@ -238,12 +238,12 @@ func (m *mdConverter) convertTextToPageLink(block *model.Block) {
}
}

func (m *mdConverter) convertTextToBookmark(url string) *model.BlockContentOfBookmark {
func (m *mdConverter) convertTextToBookmark(url string, block *model.Block) {
if err := uri.ValidateURI(url); err != nil {
return nil
return
}

return &model.BlockContentOfBookmark{
block.Content = &model.BlockContentOfBookmark{
Bookmark: &model.BlockContentBookmark{
Url: url,
},
Expand Down
30 changes: 30 additions & 0 deletions core/block/import/markdown/import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ func buildExpectedTree(fileNameToObjectId map[string]string, provider *MockTempD
testMdPath := filepath.Join("testdata", "test.md")
testCsvPath := filepath.Join("testdata", "test.csv")
testTxtPath := filepath.Join("testdata", "test.txt")
url := "http://example.com/%zz"
want := blockbuilder.Root(
blockbuilder.ID(rootId),
blockbuilder.Children(
Expand All @@ -171,6 +172,13 @@ func buildExpectedTree(fileNameToObjectId map[string]string, provider *MockTempD
Param: fileNameToObjectId[testCsvPath],
},
}})),
blockbuilder.Text("Should not panic test5", blockbuilder.TextMarks(model.BlockContentTextMarks{Marks: []*model.BlockContentTextMark{
{
Range: &model.Range{From: 17, To: 22},
Type: model.BlockContentTextMark_Link,
Param: url,
},
}})),
blockbuilder.Text("File does not exist with bold mark test1", blockbuilder.TextMarks(model.BlockContentTextMarks{Marks: []*model.BlockContentTextMark{
{
Range: &model.Range{From: 35, To: 40},
Expand Down Expand Up @@ -215,6 +223,17 @@ func buildExpectedTree(fileNameToObjectId map[string]string, provider *MockTempD
Type: model.BlockContentTextMark_Bold,
},
}})),
blockbuilder.Text("Should not panic test5", blockbuilder.TextMarks(model.BlockContentTextMarks{Marks: []*model.BlockContentTextMark{
{
Range: &model.Range{From: 17, To: 22},
Type: model.BlockContentTextMark_Link,
Param: url,
},
{
Range: &model.Range{From: 17, To: 22},
Type: model.BlockContentTextMark_Bold,
},
}})),
blockbuilder.Bookmark(fileMdPath),
blockbuilder.Text("test2", blockbuilder.TextMarks(model.BlockContentTextMarks{Marks: []*model.BlockContentTextMark{
{
Expand All @@ -239,6 +258,17 @@ func buildExpectedTree(fileNameToObjectId map[string]string, provider *MockTempD
Type: model.BlockContentTextMark_Bold,
},
}})),
blockbuilder.Text("test5", blockbuilder.TextMarks(model.BlockContentTextMarks{Marks: []*model.BlockContentTextMark{
{
Range: &model.Range{From: 0, To: 5},
Type: model.BlockContentTextMark_Link,
Param: url,
},
{
Range: &model.Range{From: 0, To: 5},
Type: model.BlockContentTextMark_Bold,
},
}})),
blockbuilder.Link(rootId),
))
return want
Expand Down
6 changes: 6 additions & 0 deletions core/block/import/markdown/testdata/links.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Test file block [test3](test.txt)

Test link to csv [test4](test.csv)

Should not panic [test5](http://example.com/%zz)

File does not exist with bold mark **[test1](file.md)**

Test link to page with bold mark **[test2](test.md)**
Expand All @@ -14,10 +16,14 @@ Test file block with bold mark **[test3](test.txt)**

Test link to csv with bold mark **[test4](test.csv)**

Should not panic **[test5](http://example.com/%zz)**

**[test1](file.md)**

**[test2](test.md)**

**[test3](test.txt)**

**[test4](test.csv)**

**[test5](http://example.com/%zz)**

0 comments on commit efa8c8d

Please sign in to comment.