import DOMPurify, { type Config as DomPurifyConfig } from "dompurify";
import MarkdownIt from "markdown-it";
import defined from "terriajs-cesium/Source/Core/defined";
import { Term } from "../ReactViewModels/defaultTerms";
import injectTerms from "./injectTerms";
const md = new MarkdownIt({
html: true,
linkify: true
});
const htmlRegex = /^\s*<[^>]+>/;
export interface MarkdownOptions {
// requires tooltipTerms as well
injectTermsAsTooltips?: boolean;
// requires injectTermsAsTooltips as well
tooltipTerms?: Term[];
/** Single line rendering, without paragraph wrap */
inline?: boolean;
}
/**
* Convert a String in markdown format (which includes html) into html format.
* @param {String} markdownString String in markdown format.
* @param {Boolean} allowUnsafeHtml Pass true to allow unsafe html. Defaults to false.
* @param {Object} [domPurifyOptions] Options to pass to DOMPurify, eg. {ADD_TAGS: ['ying', 'yang']} (https://github.com/cure53/DOMPurify).
* @param {Object} [markdownOptions] Options for markdown parsing
* @return {String} HTML-formatted string.
*/
function markdownToHtml(
markdownString: string,
allowUnsafeHtml: boolean = false,
domPurifyOptions: DomPurifyConfig = {},
markdownOptions: MarkdownOptions = {}
) {
if (!defined(markdownString) || markdownString.length === 0) {
return markdownString;
}
// If the text looks like html, don't try to interpret it as Markdown because
// we'll probably break it in the process.
// It would wrap non-standard tags such as