diff options
author | Chris <ccbrown112@gmail.com> | 2018-01-22 15:32:50 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-22 15:32:50 -0600 |
commit | 599991ea731953f772824ce3ed1e591246aa004f (patch) | |
tree | fca0f556f12e56bdcefa74ac6794dec64e04e3fc /utils/markdown/inspect.go | |
parent | a8445775351c32f8a12081f60bda2099571b2758 (diff) | |
download | chat-599991ea731953f772824ce3ed1e591246aa004f.tar.gz chat-599991ea731953f772824ce3ed1e591246aa004f.tar.bz2 chat-599991ea731953f772824ce3ed1e591246aa004f.zip |
PLT-3383: image proxy support (#7991)
* image proxy support
* go vet fix, remove mistakenly added coverage file
* fix test compile error
* add validation to config settings and documentation to model functions
* add message_source field to post
Diffstat (limited to 'utils/markdown/inspect.go')
-rw-r--r-- | utils/markdown/inspect.go | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/utils/markdown/inspect.go b/utils/markdown/inspect.go new file mode 100644 index 000000000..c4e3bf1ac --- /dev/null +++ b/utils/markdown/inspect.go @@ -0,0 +1,78 @@ +// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +package markdown + +// 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(interface{}) bool) { + document, referenceDefinitions := Parse(markdown) + InspectBlock(document, func(block Block) bool { + if !f(block) { + return false + } + switch v := block.(type) { + case *Paragraph: + for _, inline := range v.ParseInlines(referenceDefinitions) { + InspectInline(inline, func(inline Inline) bool { + return f(inline) + }) + } + } + return true + }) +} + +// InspectBlock traverses the blocks in depth-first order, starting with block. If f returns true, +// InspectBlock invokes f recursively for each child of the block, followed by a call of f(nil). +func InspectBlock(block Block, f func(Block) bool) { + if !f(block) { + return + } + switch v := block.(type) { + case *Document: + for _, child := range v.Children { + InspectBlock(child, f) + } + case *List: + for _, child := range v.Children { + InspectBlock(child, f) + } + case *ListItem: + for _, child := range v.Children { + InspectBlock(child, f) + } + case *BlockQuote: + for _, child := range v.Children { + InspectBlock(child, f) + } + } + f(nil) +} + +// InspectInline traverses the blocks in depth-first order, starting with block. If f returns true, +// InspectInline invokes f recursively for each child of the block, followed by a call of f(nil). +func InspectInline(inline Inline, f func(Inline) bool) { + if !f(inline) { + return + } + switch v := inline.(type) { + case *InlineImage: + for _, child := range v.Children { + InspectInline(child, f) + } + case *InlineLink: + for _, child := range v.Children { + InspectInline(child, f) + } + case *ReferenceImage: + for _, child := range v.Children { + InspectInline(child, f) + } + case *ReferenceLink: + for _, child := range v.Children { + InspectInline(child, f) + } + } + f(nil) +} |