Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix markdown request renderer #14211

Merged
merged 6 commits into from
Sep 30, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
allow to use MarkdownString or string in custom hook
eneufeld committed Sep 26, 2024
commit 88424720dd681d4db201b4cd0f1de483036081d8
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@ import { ReactNode, useEffect, useRef } from '@theia/core/shared/react';
import * as React from '@theia/core/shared/react';
import * as markdownit from '@theia/core/shared/markdown-it';
import * as DOMPurify from '@theia/core/shared/dompurify';
import { MarkdownString } from '@theia/core/lib/common/markdown-rendering';

@injectable()
export class MarkdownPartRenderer implements ChatResponsePartRenderer<MarkdownChatResponseContent | InformationalChatResponseContent> {
@@ -52,7 +53,7 @@ export class MarkdownPartRenderer implements ChatResponsePartRenderer<MarkdownCh
}

const MarkdownRender = ({ response }: { response: MarkdownChatResponseContent | InformationalChatResponseContent }) => {
const ref = useMarkdownRendering(response.content.value);
const ref = useMarkdownRendering(response.content);

return <div ref={ref}></div>;
};
@@ -67,13 +68,14 @@ const MarkdownRender = ({ response }: { response: MarkdownChatResponseContent |
* @param markdown the string to render as markdown
* @returns the ref to use in an element to render the markdown
*/
export const useMarkdownRendering = (markdown: string) => {
export const useMarkdownRendering = (markdown: string | MarkdownString) => {
// eslint-disable-next-line no-null/no-null
const ref = useRef<HTMLDivElement | null>(null);
useEffect(() => {
const markdownIt = markdownit();
const host = document.createElement('div');
const html = markdownIt.render(markdown);
const markdownString = typeof markdown === 'string' ? markdown : markdown.value;
const html = markdownIt.render(markdownString);
host.innerHTML = DOMPurify.sanitize(html, {
ALLOW_UNKNOWN_PROTOCOLS: true // DOMPurify usually strips non http(s) links from hrefs
});
Loading