All files / frontend/helpers inlineMarkdown.ts

100% Statements 28/28
78.57% Branches 11/14
100% Functions 6/6
100% Lines 28/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102                                      2x       2x   2x                       2x           2x 3x 2x 3x     8x 8x 8x     2x 3x 3x           3x     2x 2x 6x 6x   6x 1x     5x         5x                       18x 18x 18x 18x   18x          
/*
  Copyright (C) 2026 Sber
 
  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at
 
          http://www.apache.org/licenses/LICENSE-2.0
 
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
*/
 
import DOMPurify from 'dompurify';
import MarkdownIt from 'markdown-it';
 
const purifier = typeof window !== 'undefined' && typeof DOMPurify === 'function'
  ? DOMPurify(window)
  : DOMPurify;
 
export const INLINE_MARKDOWN_EMBED_ATTRIBUTE = 'data-inline-markdown-embed';
 
const EMBED_LANGUAGES = new Set(['mermaid', 'plantuml']);
 
export type MarkdownEmbed = {
  id: string;
  type: 'image' | 'mermaid' | 'plantuml';
  src?: string;
  alt?: string;
  source?: string;
};
 
type RenderEnvironment = { embeds: MarkdownEmbed[] };
 
const renderer = new MarkdownIt({
  html: false,
  breaks: false,
  linkify: false
});
 
renderer.renderer.rules.table_open = (tokens, idx, options, env, slf) =>
  `<div class="inline-markdown-table-container">${slf.renderToken(tokens, idx, options)}`;
renderer.renderer.rules.table_close = (tokens, idx, options, env, slf) =>
  `${slf.renderToken(tokens, idx, options)}</div>\n`;
 
function createEmbed(env: RenderEnvironment, descriptor: Omit<MarkdownEmbed, 'id'>) {
  const id = `inline-markdown-embed-${env.embeds.length}`;
  env.embeds.push({id, ...descriptor});
  return id;
}
 
renderer.renderer.rules.image = (tokens, idx, options, env, slf) => {
  const token = tokens[idx];
  const id = createEmbed(env, {
    type: 'image',
    src: token.attrGet('src') || '',
    alt: slf.renderInlineAsText(token.children || [], options, env)
  });
 
  return `<span ${INLINE_MARKDOWN_EMBED_ATTRIBUTE}="${id}"></span>`;
};
 
const defaultFenceRenderer = renderer.renderer.rules.fence;
renderer.renderer.rules.fence = (tokens, idx, options, env, slf) => {
  const token = tokens[idx];
  const language = (token.info || '').trim().split(/\s+/)[0].toLowerCase();
 
  if (!EMBED_LANGUAGES.has(language)) {
    return defaultFenceRenderer(tokens, idx, options, env, slf);
  }
 
  const id = createEmbed(env, {
    type: language,
    source: token.content
  });
 
  return `<div ${INLINE_MARKDOWN_EMBED_ATTRIBUTE}="${id}"></div>\n`;
};
 
/**
 * Renders Markdown that can be displayed inside an editable-table cell.
 * Images and supported diagram fences are returned as descriptors and are
 * represented in HTML by elements carrying INLINE_MARKDOWN_EMBED_ATTRIBUTE.
 *
 * @param {*} value Markdown source.
 * @returns {{html: string, embeds: Array<object>}}
 */
export function renderInlineMarkdown(value: unknown): { html: string; embeds: MarkdownEmbed[] } {
  const source = value == null ? '' : String(value);
  const env = {embeds: []};
  const rendered = renderer.render(source, env);
  const sanitized = purifier.sanitize(rendered);
 
  return {
    html: sanitized,
    embeds: env.embeds
  };
}