Skip to content

Commit

Permalink
Manual cherry pick of [MM-52527] (#23519) (#23675)
Browse files Browse the repository at this point in the history
Automatic Merge
  • Loading branch information
vish9812 committed Jun 8, 2023
1 parent 8f455a0 commit 1b61e43
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 30 deletions.
9 changes: 9 additions & 0 deletions shared/markdown/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@

package markdown

const (
// Assuming 64k maxSize of a post which can be stored in DB.
// Allow scanning upto twice(arbitrary value) the post size.
maxLen = 1024 * 64 * 2
)

// Inspect traverses the markdown tree in depth-first order. If f returns true, Inspect invokes f
// recursively for each child of the block or inline, followed by a call of f(nil).
func Inspect(markdown string, f func(any) bool) {
if len(markdown) > maxLen {
return
}
document, referenceDefinitions := Parse(markdown)
InspectBlock(document, func(block Block) bool {
if !f(block) {
Expand Down
100 changes: 70 additions & 30 deletions shared/markdown/inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,85 @@ import (
)

func TestInspect(t *testing.T) {
markdown := `
t.Run("base", func(t *testing.T) {
markdown := `
[foo]: bar
- a
> [![]()]()
> [![foo]][foo]
- d
`

visited := []string{}
level := 0
Inspect(markdown, func(blockOrInline any) bool {
if blockOrInline == nil {
level--
} else {
visited = append(visited, strings.Repeat(" ", level*4)+strings.TrimPrefix(fmt.Sprintf("%T", blockOrInline), "*markdown."))
level++
}
return true
visited := []string{}
level := 0
Inspect(markdown, func(blockOrInline any) bool {
if blockOrInline == nil {
level--
} else {
visited = append(visited, strings.Repeat(" ", level*4)+strings.TrimPrefix(fmt.Sprintf("%T", blockOrInline), "*markdown."))
level++
}
return true
})

assert.Equal(t, []string{
"Document",
" Paragraph",
" List",
" ListItem",
" Paragraph",
" Text",
" BlockQuote",
" Paragraph",
" InlineLink",
" InlineImage",
" SoftLineBreak",
" ReferenceLink",
" ReferenceImage",
" Text",
" ListItem",
" Paragraph",
" Text",
}, visited)
})

t.Run("visit nodes when len is smaller than maxLen", func(t *testing.T) {
n := maxLen / 5
markdown := strings.Repeat(`![`, n) + strings.Repeat(`]()`, n)

visited := []string{}
level := 0
Inspect(markdown, func(blockOrInline any) bool {
if blockOrInline == nil {
level--
} else {
visited = append(visited, strings.Repeat(" ", level*4)+strings.TrimPrefix(fmt.Sprintf("%T", blockOrInline), "*markdown."))
level++
}
return true
})

assert.NotEmpty(t, visited)
})

assert.Equal(t, []string{
"Document",
" Paragraph",
" List",
" ListItem",
" Paragraph",
" Text",
" BlockQuote",
" Paragraph",
" InlineLink",
" InlineImage",
" SoftLineBreak",
" ReferenceLink",
" ReferenceImage",
" Text",
" ListItem",
" Paragraph",
" Text",
}, visited)
t.Run("do not visit any nodes when len is greater than maxLen", func(t *testing.T) {
n := (maxLen / 5) + 1
markdown := strings.Repeat(`![`, n) + strings.Repeat(`]()`, n)

visited := []string{}
level := 0
Inspect(markdown, func(blockOrInline any) bool {
if blockOrInline == nil {
level--
} else {
visited = append(visited, strings.Repeat(" ", level*4)+strings.TrimPrefix(fmt.Sprintf("%T", blockOrInline), "*markdown."))
level++
}
return true
})

assert.Empty(t, visited)
})
}

var counterSink int
Expand Down
1 change: 1 addition & 0 deletions store/sqlstore/post_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2547,6 +2547,7 @@ func (s *SqlPostStore) determineMaxPostSize() int {
}

// GetMaxPostSize returns the maximum number of runes that may be stored in a post.
// For any changes, accordingly update the markdown maxLen here - markdown/inspect.go.
func (s *SqlPostStore) GetMaxPostSize() int {
s.maxPostSizeOnce.Do(func() {
s.maxPostSizeCached = s.determineMaxPostSize()
Expand Down

0 comments on commit 1b61e43

Please sign in to comment.