Skip to content

Commit

Permalink
Added linked and table renderer for markdown in comment (#41)
Browse files Browse the repository at this point in the history
* Added linked and table renderer for markdown in comment

* Added white-space pre wrap for comment markdown

* updated snapshot

* clean up
  • Loading branch information
Rajat-Dabade authored Dec 2, 2024
1 parent bd93b94 commit c95e6e8
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,13 @@ exports[`components/cardDetail/comment return comment 1`] = `
</div>
</div>
<div
class="mocked-message-html"
class="comment-markdown"
>
Test Comment
<div
class="mocked-message-html"
>
Test Comment
</div>
</div>
</div>
</div>
Expand Down Expand Up @@ -244,9 +248,13 @@ exports[`components/cardDetail/comment return comment and delete comment 1`] = `
</div>
</div>
<div
class="mocked-message-html"
class="comment-markdown"
>
Test Comment
<div
class="mocked-message-html"
>
Test Comment
</div>
</div>
</div>
</div>
Expand Down Expand Up @@ -281,9 +289,13 @@ exports[`components/cardDetail/comment return comment readonly 1`] = `
</div>
</div>
<div
class="mocked-message-html"
class="comment-markdown"
>
Test Comment
<div
class="mocked-message-html"
>
Test Comment
</div>
</div>
</div>
</div>
Expand Down Expand Up @@ -416,9 +428,13 @@ exports[`components/cardDetail/comment return guest comment 1`] = `
</div>
</div>
<div
class="mocked-message-html"
class="comment-markdown"
>
Test Comment
<div
class="mocked-message-html"
>
Test Comment
</div>
</div>
</div>
</div>
Expand Down Expand Up @@ -551,9 +567,13 @@ exports[`components/cardDetail/comment return guest comment and delete comment 1
</div>
</div>
<div
class="mocked-message-html"
class="comment-markdown"
>
Test Comment
<div
class="mocked-message-html"
>
Test Comment
</div>
</div>
</div>
</div>
Expand Down Expand Up @@ -597,9 +617,13 @@ exports[`components/cardDetail/comment return guest comment readonly 1`] = `
</div>
</div>
<div
class="mocked-message-html"
class="comment-markdown"
>
Test Comment
<div
class="mocked-message-html"
>
Test Comment
</div>
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,13 @@ exports[`components/cardDetail/CommentsList comments show up 1`] = `
</div>
</div>
<div
class="mocked-message-html"
class="comment-markdown"
>
Test Comment
<div
class="mocked-message-html"
>
Test Comment
</div>
</div>
</div>
<div
Expand Down Expand Up @@ -109,9 +113,13 @@ exports[`components/cardDetail/CommentsList comments show up 1`] = `
</div>
</div>
<div
class="mocked-message-html"
class="comment-markdown"
>
Test Comment
<div
class="mocked-message-html"
>
Test Comment
</div>
</div>
</div>
<hr
Expand Down Expand Up @@ -151,9 +159,13 @@ exports[`components/cardDetail/CommentsList comments show up in readonly mode 1`
</div>
</div>
<div
class="mocked-message-html"
class="comment-markdown"
>
Test Comment
<div
class="mocked-message-html"
>
Test Comment
</div>
</div>
</div>
<div
Expand Down Expand Up @@ -181,9 +193,13 @@ exports[`components/cardDetail/CommentsList comments show up in readonly mode 1`
</div>
</div>
<div
class="mocked-message-html"
class="comment-markdown"
>
Test Comment
<div
class="mocked-message-html"
>
Test Comment
</div>
</div>
</div>
<hr
Expand Down
4 changes: 4 additions & 0 deletions webapp/src/components/cardDetail/comment.scss
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,8 @@
.comment-text * {
user-select: text;
}

.comment-markdown {
white-space: pre-wrap;
}
}
7 changes: 4 additions & 3 deletions webapp/src/components/cardDetail/comment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ const Comment: FC<Props> = (props: Props) => {
const formattedText =
<Provider store={(window as any).store}>
{messageHtmlToComponent(formatText(comment.title, {
singleline: false,
renderer: Utils.getMarkdownRenderer(),
atMentions: true,
mentionHighlight: false,
team: selectedTeam,
channelNamesMap,
}), {
Expand Down Expand Up @@ -87,7 +86,9 @@ const Comment: FC<Props> = (props: Props) => {
</MenuWrapper>
)}
</div>
{formattedText}
<div className='comment-markdown'>
{formattedText}
</div>
</div>
)
}
Expand Down
11 changes: 7 additions & 4 deletions webapp/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,11 @@ class Utils {
// Markdown

static htmlFromMarkdown(text: string): string {
// HACKHACK: Somehow, marked doesn't encode angle brackets
const renderer = this.getMarkdownRenderer()
return this.htmlFromMarkdownWithRenderer(text, renderer)
}

static getMarkdownRenderer(): marked.Renderer {
const renderer = new marked.Renderer()
renderer.link = (href, title, contents) => {
return '<a ' +
Expand All @@ -303,9 +307,8 @@ class Utils {

renderer.table = (header, body) => {
return `<div class="table-responsive"><table class="markdown__table"><thead>${header}</thead><tbody>${body}</tbody></table></div>`
}

return this.htmlFromMarkdownWithRenderer(text, renderer)
}
return renderer
}

static htmlFromMarkdownWithRenderer(text: string, renderer: marked.Renderer): string {
Expand Down
5 changes: 3 additions & 2 deletions webapp/src/webapp_globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import {NameMappedObjects} from "mattermost-redux/types/utilities"

import {Channel} from "mattermost-redux/types/channels"

import {Renderer} from "marked"

import {Team} from "./store/teams"


type Options = {
singleline: boolean;
atMentions: boolean;
mentionHighlight: boolean;
team: Team | null;
channelNamesMap: NameMappedObjects<Channel>;
renderer: Renderer;
}

type Props = {
Expand Down

0 comments on commit c95e6e8

Please sign in to comment.