-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support markdown in chat and resource (#56)
- Loading branch information
1 parent
26fcdb7
commit 12b9537
Showing
9 changed files
with
113 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import DOMPurify from 'dompurify'; | ||
import { marked } from 'marked'; | ||
|
||
/** | ||
* Renders the given markdown string to HTML and sanitizes it. | ||
* @param markdown - The markdown string to be rendered. | ||
* @returns A promise that resolves to the sanitized HTML string. | ||
*/ | ||
export async function renderMarkdown(markdown: string): Promise<string> { | ||
const renderer = new marked.Renderer(); | ||
|
||
// prevent rendering of hidden links | ||
renderer.link = (token) => { | ||
return token.text ? `${token.text} (${token.href})` : token.href; | ||
}; | ||
|
||
// prevent showing images | ||
renderer.image = () => ''; | ||
|
||
const dirty = await marked(markdown, { renderer }); | ||
return DOMPurify.sanitize(dirty); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters