// ***************************************************************************** // Copyright (C) 2024 EclipseSource GmbH. // // This program and the accompanying materials are made available under the // terms of the Eclipse Public License v. 2.0 which is available at // http://www.eclipse.org/legal/epl-2.0. // // This Source Code may also be made available under the following Secondary // Licenses when the conditions for such availability set forth in the Eclipse // Public License v. 2.0 are satisfied: GNU General Public License, version 2 // with the GNU Classpath Exception which is available at // https://www.gnu.org/software/classpath/license.html. // // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0 // ***************************************************************************** import { ChatResponsePartRenderer } from '../chat-response-part-renderer.js'; import { inject, injectable } from '@theia/core/shared/inversify'; import { ChatResponseContent, InformationalChatResponseContent, MarkdownChatResponseContent, } from '@theia/ai-chat/lib/common'; import { ReactNode, useEffect, useRef } from '@theia/core/shared/react'; import * as React from '@theia/core/shared/react'; import markdownit from '@theia/core/shared/markdown-it'; import * as markdownitemoji from '@theia/core/shared/markdown-it-emoji'; import * as DOMPurify from '@theia/core/shared/dompurify'; import { MarkdownString } from '@theia/core/lib/common/markdown-rendering'; import { OpenerService, open } from '@theia/core/lib/browser'; import { URI } from '@theia/core'; import { blockExternalResources, BLOCKED_RESOURCE_ALLOW_CLASS, BLOCKED_RESOURCE_CLASS, BLOCKED_RESOURCE_WRAPPER_CLASS, restoreBlockedResource } from './block-external-resources.js'; export interface MarkdownRenderProps { text: string | MarkdownString; openerService: OpenerService; className?: string; } /** * Renders the given markdown via {@link useMarkdownRendering} into a `
|<\/p>|
<\/p>$/g, '') : markdownIt.render(markdown);
template.innerHTML = DOMPurify.sanitize(html, {
// DOMPurify usually strips non http(s) links from hrefs
// but we want to allow them (see handleClick via OpenerService in useMarkdownRendering)
ALLOW_UNKNOWN_PROTOCOLS: true,
ADD_TAGS: ['iframe', 'frame'],
ADD_ATTR: ['src', 'srcset', 'srcdoc', 'poster', 'href', 'xlink:href', 'data']
});
// Active embedded content is always blocked; trusted content (blockExternalResourceLoading=false)
// still renders its external URL resources directly.
blockExternalResources(template.content, blockExternalResourceLoading);
return template.content;
}
/** Prepares the text of a chat request text part for inline rendering. */
export function prepareRequestText(text: string): string {
return text
.replace(/^[\r\n]+|[\r\n]+$/g, '') // remove excessive new lines
.replace(/(^ )/g, ' '); // enforce keeping space before
}
}
export interface DeclaredEventsEventListenerObject extends EventListenerObject {
handledEvents?: (keyof HTMLElementEventMap)[];
}
/**
* This hook uses markdown-it directly to render markdown.
* The reason to use markdown-it directly is that the MarkdownRenderer is
* overridden by theia with a monaco version. This monaco version strips all html
* tags from the markdown with empty content. This leads to unexpected behavior when
* rendering markdown with html tags.
*
* Moreover, we want to intercept link clicks to use the Theia OpenerService instead of the default browser behavior.
*
* @param markdown the string to render as markdown
* @param skipSurroundingParagraph whether to remove a surrounding paragraph element (default: false)
* @param openerService the service to handle link opening
* @param eventHandler `handleEvent` will be called by default for `click` events and additionally
* for all events enumerated in {@link DeclaredEventsEventListenerObject.handledEvents}. If `handleEvent` returns `true`,
* no additional handlers will be run for the event.
* @param blockExternalResourceLoading whether external URL resources should be blocked until explicitly allowed (default: true).
* Trusted content authored by the user (e.g. their own chat requests) can pass `false` to render such resources directly.
* Active embedded content (iframes, frames, objects, embeds) is always blocked regardless of this flag.
* @returns the ref to use in an element to render the markdown
*/
export const useMarkdownRendering = (
markdown: string | MarkdownString,
openerService: OpenerService,
skipSurroundingParagraph: boolean = false,
eventHandler?: DeclaredEventsEventListenerObject,
blockExternalResourceLoading: boolean = true
) => {
// null is valid in React
// eslint-disable-next-line no-null/no-null
const ref = useRef