Skip to content

feat(markdown-renderer): add optional syntax highlighting support#1815

Draft
Killusions wants to merge 3 commits intomainfrom
feat/markdown-renderer-syntax-highlighting
Draft

feat(markdown-renderer): add optional syntax highlighting support#1815
Killusions wants to merge 3 commits intomainfrom
feat/markdown-renderer-syntax-highlighting

Conversation

@Killusions
Copy link
Copy Markdown
Member

@Killusions Killusions commented Apr 2, 2026

Merge after #1810 and #1811

Add optional syntax highlighting the markdown renderer for libraries like highlight.js and a custom hljs theme using Element design tokens


Documentation.
Examples.
Dashboards Demo.
Playwright report.

Coverage Reports:

Code Coverage

Add max-inline-size constraint to message bubble and wrapper to prevent
overflow. Fix attachment preview backdrop click behavior. Use index-based
tracking for message list and add scroll-to-bottom on message send.
Update example action labels for clarity.
…line and caching

Rewrite the markdown renderer to use a recursive processing pipeline with
placeholder-based architecture. Add SSR support via optional doc/isBrowser
parameters. Introduce LRU caching for code blocks and tables. Improve
table rendering with proper thead/th support. Add code-wrapper structure
with language labels. Update SCSS for new structural layout including
code-wrapper, table-wrapper, and table-scroll-container.
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request refactors the markdown renderer to support server-side rendering and introduces LRU caching for code blocks and tables. It adds a syntax highlighter input and integrates highlight.js for code formatting. Feedback identifies a memory leak caused by uncleared placeholder maps, suggests improving JSDoc for the options interface, and recommends safer handling of the global document object to prevent ReferenceErrors in SSR environments.

Comment on lines +545 to +546
return (text: string): HTMLElement => {
const div = docRef.createElement('div');
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.

high

The placeholder maps (codeBlockPlaceholderMap, tablePlaceholderMap, and linkPlaceholderMap) are shared across all calls to this renderer instance but are never cleared. This causes them to grow indefinitely as more content is processed, leading to a memory leak. They should be cleared at the start of each rendering cycle.

  return (text: string): HTMLElement => {
    codeBlockPlaceholderMap.clear();
    tablePlaceholderMap.clear();
    linkPlaceholderMap.clear();

    const div = docRef.createElement('div');

Comment on lines +12 to +18
export interface MarkdownRendererOptions {
/**
* Optional syntax highlighter function for code blocks.
* Receives code content and optional language, returns highlighted HTML or undefined.
*/
syntaxHighlighter?: (code: string, language?: string) => string | undefined;
}
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.

medium

The MarkdownRendererOptions interface and its properties should be documented with JSDoc to provide clarity for API consumers and ensure complete documentation in the generated API goldens. When adding these comments, ensure that any '/' sequences are escaped as '/' to prevent premature termination of the comment block.

References
  1. In JSDoc/TSDoc comments, escape the */ sequence (as */) to prevent premature termination of the comment block.

isBrowser?: boolean
): ((text: string) => Node) => {
// Use provided document or fall back to global document for backwards compatibility
const docRef = doc ?? document;
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.

medium

Directly falling back to the global document can cause a ReferenceError in Server-Side Rendering (SSR) environments where document is not defined. Ensure that browser-dependent features or global objects are only accessed after verifying the environment (e.g., using isPlatformBrowser or checking if document is defined) to prevent errors during SSR.

Suggested change
const docRef = doc ?? document;
const docRef = doc ?? (typeof document !== 'undefined' ? document : undefined);
References
  1. When using browser-dependent UI features, ensure they are only executed in a browser environment. Use a check like isPlatformBrowser to prevent errors during Server-Side Rendering (SSR).

Add optional syntax highlighting the markdown renderer
for libraries like highlight.js
and a custom hljs theme using Element design tokens
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.

1 participant