Skip to content

Fix/handle img tags - #3640

Closed
g14a wants to merge 4 commits into
cli:trunkfrom
g14a:fix/handle-img-tags
Closed

g14a wants to merge 4 commits into
cli:trunkfrom
g14a:fix/handle-img-tags

Conversation

@g14a

@g14a g14a commented May 14, 2021

Copy link
Copy Markdown
Contributor

This commit renders img tags in issue and pr previews.

Fixes #229

@mislav mislav left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for taking this on!

Comment thread Makefile
Comment on lines +69 to +70
# install -d ${DESTDIR}${mandir}/man1
# install -m644 ./share/man/man1/* ${DESTDIR}${mandir}/man1/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Accidental edit?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

oops my bad yes accidental edit

"login": "g14a"
},
"authorAssociation": "CONTRIBUTOR",
"body": "@vilmibm @ampinsk I tried solving this. Issues and PRs look like the following respectively. Does it look okay or can it be better from a design perspective?\n## Issues: \n<img width=\"854\" alt=\"image\" src=\"https://user-images.githubusercontent.com/17702388/113286902-6ea20500-930a-11eb-8860-754107c67b22.png\">\n## PRs:\n<img width=\"854\" alt=\"image\" src=\"https://user-images.githubusercontent.com/17702388/113286974-8c6f6a00-930a-11eb-98da-41e159056438.png\">\nDo you want to take a look at the PR which fixes this?",

@mislav mislav May 17, 2021 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since you've only edited the Render function and nothing else in the codebase, let's make a test that exercises only the Render function and not the entire issue view. This test has a lot of setup just to exercise one tiny piece of changed functionality.

Comment thread pkg/markdown/markdown.go Resolved
Comment thread pkg/markdown/markdown.go

func formatImgTags(content string) string {
lines := strings.Split(content, "\n")
re := regexp.MustCompile(`<img[^>]+\bsrc=["']([^"']+)["']`)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This part is getting tricky because you're basically doing HTML parsing without writing a full-fledged HTML parser. Note that attribute values such as src=VALUE don't necessarily have to be closed in quotes. Furthermore, images can have text in the alt attribute, which would be important to parse out and preserve. Finally, an <img> tag can be surrounded by regular content (like hello <img src="..."> world!), so we can't just assume that an <img> tag will always be on its own line and that the rest of the content can be discarded. Let's only replace the <img...> portion within the original content with the ![](...) syntax and preserve everything else.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Additionally this doesn't handle image tags in code blocks which was the issue with our initial emoji rendering solution. Overall, I think we should be adding this feature into the upstream glamour library instead of here. @mislav what do you think?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Oh yeah 🤦 excellent point @samcoe. I do agree that this should be handled at the level that parses Markdown (so, in Glamour) rather than with regexp matching.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It sounds like the glamour team is uninterested in handling HTML: charmbracelet/glamour#111 (comment)

If that is the case, we may want to consider an alternative to glamour in the long run.

I'm going to mark this PR as a draft so we can continue discussion.

Comment thread pkg/markdown/markdown.go
for i, line := range lines {
images := re.FindAllStringSubmatch(line, -1)
for _, element := range images {
lines[i] = fmt.Sprintf("![Image](%s)", element[1])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Instead of the text Image there should be the alt text parsed from the <img> tag. If the alt text was blank, then this text should be blank as well (![](SRC)). This is important for accessibility. A hardcoded alt text that just says Image hurts accessibility because it is not useful.

@vilmibm

vilmibm commented Sep 1, 2021

Copy link
Copy Markdown
Contributor

Closing this until we decide on a more holistic approach to this issue.

@vilmibm vilmibm closed this Sep 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

handle img tags in pr/issue view

4 participants