diff options
author | Harrison Healey <harrisonmhealey@gmail.com> | 2018-08-01 11:43:58 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-01 11:43:58 -0400 |
commit | ecfba2c2e9e46389e2012e9adf1ab993aaa7ea5e (patch) | |
tree | 9caa617309e989ba4aafae9dbfaebabd99a4e537 /utils/markdown/links.go | |
parent | d81a61398d01d839e70e2345da787e7ef89c0832 (diff) | |
download | chat-ecfba2c2e9e46389e2012e9adf1ab993aaa7ea5e.tar.gz chat-ecfba2c2e9e46389e2012e9adf1ab993aaa7ea5e.tar.bz2 chat-ecfba2c2e9e46389e2012e9adf1ab993aaa7ea5e.zip |
MM-11175 Add logic to server to understand markdown images with dimensions (#9159)
Diffstat (limited to 'utils/markdown/links.go')
-rw-r--r-- | utils/markdown/links.go | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/utils/markdown/links.go b/utils/markdown/links.go index 419797cb9..9f3128c4f 100644 --- a/utils/markdown/links.go +++ b/utils/markdown/links.go @@ -128,3 +128,57 @@ func parseLinkLabel(markdown string, position int) (raw Range, next int, ok bool return } + +// As a non-standard feature, we allow image links to specify dimensions of the image by adding "=WIDTHxHEIGHT" +// after the image destination but before the image title like ![alt](http://example.com/image.png =100x200 "title"). +// Both width and height are optional, but at least one of them must be specified. +func parseImageDimensions(markdown string, position int) (raw Range, next int, ok bool) { + if position >= len(markdown) { + return + } + + originalPosition := position + + // Read = + position += 1 + if position >= len(markdown) { + return + } + + // Read width + hasWidth := false + for isNumericByte(markdown[position]) { + hasWidth = true + position += 1 + } + + // Look for early end of dimensions + if isWhitespaceByte(markdown[position]) || markdown[position] == ')' { + return Range{originalPosition, position - 1}, position, true + } + + // Read the x + if markdown[position] != 'x' && markdown[position] != 'X' { + return + } + position += 1 + + // Read height + hasHeight := false + for isNumericByte(markdown[position]) { + hasHeight = true + position += 1 + } + + // Make sure the there's no trailing characters + if !isWhitespaceByte(markdown[position]) && markdown[position] != ')' { + return + } + + if !hasWidth && !hasHeight { + // At least one of width or height is required + return + } + + return Range{originalPosition, position - 1}, position, true +} |