Source: internal/vendor/@carbon/ibmdotcom-utilities/utilities/markdownToHtml/markdownToHtml.js

/**
 * Copyright IBM Corp. 2020, 2023
 *
 * This source code is licensed under the Apache-2.0 license found in the
 * LICENSE file in the root directory of this source tree.
 */

import DOMPurify from 'isomorphic-dompurify';
import { marked } from 'marked';
import settings from '../settings/settings.js';
import striptags from 'striptags';
var prefix = settings.prefix;
var _cleanStringRegex = /\n|\s{2,}|&;/g;

/**
 * Cleans string by replacing multiple spaces with a single space
 * and removing single new lines.
 *
 * @param {string} str String to be checked
 * @returns {string} String with multiple spaces and single new lines removed
 * @private
 */
var _cleanString = function _cleanString(str) {
  return str.replace(_cleanStringRegex, ' ');
};

/**
 * Converts markdown syntaxes into html
 *
 * @param {string} str String to convert to html
 * @param {object} [options={}] Object with options for the conversion
 * @param {boolean} [options.allowHtml=false] Defines if should allow or remove html tags
 * @param {object} [options.renderer] Custom renderers
 * @param {Set<string>} [options.customTags] List of custom element tags the `renderer` uses.
 * @returns {string} String converted to html
 * @example
 * import { markdownToHtml } from '@carbon/ibmdotcom-utilities';
 *
 * markdownToHtml('Lorem *ipsum* dolor __sit__.')
 * // 'Lorem <em class="bx--type-light">ipsum</em> dolor <strong class="bx--type-semibold">sit</strong>.'
 */
function markdownToHtml(str) {
  var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
    _ref$allowHtml = _ref.allowHtml,
    allowHtml = _ref$allowHtml === void 0 ? false : _ref$allowHtml,
    _ref$renderer = _ref.renderer,
    renderer = _ref$renderer === void 0 ? {} : _ref$renderer,
    customTags = _ref.customTags;
  var converted = allowHtml ? str : striptags(str);

  /**
   * Custom rendering options to add Carbon styles
   *
   */
  var defaultRenderer = {
    link: function link(href, title, text) {
      var linkTitle = title ? "title=\"".concat(title, "\"") : null;
      return "<a class=\"".concat(prefix, "--link ").concat(prefix, "--link--lg\" href=\"").concat(href, "\" ").concat(linkTitle, ">").concat(text, "</a>");
    },
    list: function list(body, ordered) {
      var listType = ordered ? 'ol' : 'ul';
      var listClass = ordered ? "".concat(prefix, "--list--ordered") : "".concat(prefix, "--list--unordered");
      return "<".concat(listType, " class=\"").concat(listClass, "\">").concat(body, "</").concat(listType, ">");
    },
    listitem: function listitem(text) {
      return "<li class=\"".concat(prefix, "--list__item\">").concat(text, "</li>");
    }
  };
  marked.use({
    smartypants: true,
    renderer: Object.assign(defaultRenderer, renderer)
  });
  if (customTags) {
    DOMPurify.addHook('uponSanitizeElement', function (node, _ref2) {
      var allowedTags = _ref2.allowedTags,
        tagName = _ref2.tagName;
      if (customTags.has(tagName) && !allowedTags[tagName]) {
        allowedTags[tagName] = true;
      }
    });
  }
  var convertedMarkdown = DOMPurify.sanitize(marked(converted));
  if (customTags) {
    DOMPurify.removeHook('uponSanitizeElement');
  }
  return _cleanString(convertedMarkdown);
}
export default markdownToHtml;