{"version":3,"file":"index.modern.mjs","sources":["../src/index.js"],"sourcesContent":["/**\n * Creates an HTML tag with the given tag name, children, and attributes.\n * @param {string} tagName - The name of the HTML tag to create.\n * @param {string} children - The children of the HTML tag.\n * @param {Object} [attributes={}] - The attributes of the HTML tag.\n * @returns {string} The HTML tag as a string.\n */\nconst tag = (tagName, children, attributes = {}) =>\n  `<${tagName}${Object.entries(attributes)\n    .map(([k, v]) => (v ? ` ${k}=\"${encode(v)}\"` : ''))\n    .join('')}>${children}</${tagName}>`;\n\n/**\n * Removes the common leading whitespace from each line of a string.\n * @param {string} [text=\"\"] - The string to outdent.\n * @returns {string} The outdented string.\n */\nconst outdent = (text = '') => {\n  return text.replace(\n    new RegExp('^' + (text.match(/^\\s+/) || '')[0], 'gm'),\n    ''\n  );\n};\n\n/**\n * Encodes double quotes and HTML tags to entities.\n * @param {string} [text=\"\"] - The string to encode.\n * @returns {string} The encoded string.\n */\nconst encode = (text = '') =>\n  text.replace(/\"/g, '&quot;').replace(/</g, '&lt;').replace(/>/g, '&gt;');\n\nconst markdownListRegex = /(?:(^|\\n)([+-]|\\d+\\.) +(.*(\\n[ \\t]+.*)*))+/g;\n\n/**\n * Converts a string with markdown-style lists to an HTML unordered or ordered list.\n * @param {string} text - The string to convert to a list.\n * @returns {string} The HTML unordered or ordered list as a string.\n */\nconst list = (text) => {\n  const tagName = text.match(/^[+-]/m) ? 'ul' : 'ol';\n\n  return text\n    ? tag(\n        tagName,\n        text.replace(/(?:[+-]|\\d+\\.) +(.*)\\n?(([ \\t].*\\n?)*)/g, (match, a, b) =>\n          tag(\n            'li',\n            inlineBlock(`${a}\\n${outdent(b).replace(markdownListRegex, list)}`)\n          )\n        )\n      )\n    : '';\n};\n\n/**\n * Returns a function that creates an HTML tag with the given tag name,\n * using a regular expression to replace the matched text with the tag.\n * @param {string} tagName - The name of the HTML tag to create.\n * @param {RegExp} regex - The regular expression to use for matching the text to replace.\n * @param {string} replacement - The replacement string to use for creating the tag.\n * @param {function} [format=(string) => string] - An optional function to format the\n * matched text before creating the tag.\n * @returns {function} A function that takes a string and returns the HTML tag as a string.\n */\nconst createTaggedReplacement =\n  (tagName, regex, replacement, format = (string) => string) =>\n  (match) =>\n    tag(tagName, format(match.replace(regex, replacement)));\n\n/**\n * Creates an HTML block element with the given text.\n * @param {string} text - The text to convert to an HTML block element.\n * @returns {string} The HTML block element as a string.\n */\nconst block = (text) =>\n  p(\n    text,\n    [\n      // code block\n      /^(```)(.*)\\n((.*\\n)*?)\\1/gm,\n      (match, wrapper, language, text) =>\n        tag(\n          'pre',\n          tag(\n            'code',\n            encode(text),\n            language ? { class: `language-${language.trim()}` } : {}\n          )\n        ),\n\n      // blockquotes\n      /(^>.*\\n?)+/gm,\n      createTaggedReplacement('blockquote', /^> ?(.*)$/gm, '$1', inline),\n\n      // tables\n      /((^|\\n)\\|.+)+/g,\n      createTaggedReplacement(\n        'table',\n        /^.*(\\n\\|---.*?)?$/gm,\n        (match, subline = '') =>\n          createTaggedReplacement(\n            'tr',\n            /\\|(-?)([^|]*)\\1(\\|$)?/gm,\n            (match, type, text) =>\n              tag(type || subline ? 'th' : 'td', inlineBlock(text))\n          )(match.slice(0, match.length - subline.length))\n      ),\n\n      // lists\n      markdownListRegex,\n      list,\n\n      //anchor\n      /#\\[([^\\]]+?)]/g,\n      tag('a', '', { name: '$1' }),\n\n      // headlines\n      /^(#+) +(.*)(?:$)/gm,\n      (match, h, text) => tag('h' + h.length, inlineBlock(text)),\n\n      // horizontal rule\n      /^(===+|---+)(?=\\s*$)/gm,\n      '<hr>',\n    ],\n    parse\n  );\n\n/**\n * Creates an HTML inline element with the given text and options.\n * @param {string} [text=\"\"] - The text to convert to an HTML inline element.\n * @param {boolean} [dontInline=false] - Whether to prevent certain media elements from being inlined.\n * @returns {string} The HTML inline element as a string.\n */\nconst inlineBlock = (text = '', dontInline) => {\n  const temp = [];\n\n  const injectInlineBlock = (text) =>\n    text.replace(/\\\\(\\d+)/g, (match, code) =>\n      injectInlineBlock(temp[parseInt(code) - 1])\n    );\n\n  text = text\n    .trim()\n    // inline code block\n    .replace(\n      /`([^`]*)`/g,\n      (match, text) => '\\\\' + temp.push(tag('code', encode(text)))\n    )\n    // inline media (a / img)\n    .replace(\n      /[!]?\\[([!]?\\[.*?\\)|[^\\]]*?)]\\((.*?)( .*?)?\\)|(\\w+:\\/\\/[$\\-.+!*'()\\/,\\w]+)/g,\n      (match, text, href, title, link) => {\n        if (link) {\n          return dontInline\n            ? match\n            : '\\\\' + temp.push(tag('a', link, { href: link }));\n        }\n\n        return (\n          '\\\\' +\n          temp.push(\n            match[0] == '!'\n              ? tag('img', '', { src: href, alt: text, title })\n              : tag('a', inlineBlock(text, 1), { href, title })\n          )\n        );\n      }\n    );\n\n  return injectInlineBlock(dontInline ? text : inline(text));\n};\n\n/**\n * Creates an HTML inline element with the given text.\n * @param {string} text - The text to convert to an HTML inline element.\n * @returns {string} The HTML inline element as a string.\n */\nconst inline = (text) =>\n  p(\n    text,\n    [\n      // bold, italic, bold & italic\n      /([*_]{1,3})((.|\\n)+?)\\1/g,\n      (match, k, text) => {\n        k = k.length;\n        text = inline(text);\n        if (k > 1) text = tag('strong', text);\n        if (k % 2) text = tag('em', text);\n        return text;\n      },\n\n      // strike through\n      /(~{1,3})((.|\\n)+?)\\1/g,\n      (match, k, text) => tag([, 'u', 's', 'del'][k.length], inline(text)),\n\n      // replace remaining newlines with a <br>\n      /  \\n|\\n  /g,\n      '<br>',\n    ],\n    inline\n  );\n\n/**\n * Parses a string of text and converts it to HTML.\n * @param {string} text - The text to convert to HTML.\n * @param {Array} rules - An array of rules to apply to the text.\n * @param {function} parse - A function to use for parsing the text.\n * @returns {string} The HTML as a string.\n */\nconst p = (text, rules, parse) => {\n  for (let i = 0; i < rules.length; i += 2) {\n    const match = rules[i].exec(text);\n\n    if (match) {\n      const [matched, ...rest] = match;\n\n      return (\n        parse(text.slice(0, match.index)) +\n        (typeof rules[i + 1] === 'string'\n          ? rules[i + 1].replace(/\\$(\\d)/g, (_, d) => rest[d - 1])\n          : rules[i + 1].apply(null, match)) +\n        parse(text.slice(match.index + matched.length))\n      );\n    }\n  }\n\n  return text;\n};\n\n/**\n * Parses a string of text and converts it to HTML.\n * @param {string} text - The text to convert to HTML.\n * @returns {string} The HTML as a string.\n */\nconst parse = (text) => {\n  const escaped = text\n    .replace(/[\\r\\v\\b\\f]/g, '')\n    .replace(/\\\\./g, (m) => `&#${m.charCodeAt(1)};`);\n\n  const parsed =\n    block(escaped) === escaped && !escaped.match(/^[\\s\\n]*$/i)\n      ? inlineBlock(escaped).replace(/((.|\\n)+?)(\\n\\n+|$)/g, (m, text) =>\n          tag('p', text)\n        )\n      : block(escaped);\n\n  return parsed.replace(/&#(\\d+);/g, (m, code) => String.fromCharCode(+code));\n};\n\nexport { inline, inlineBlock, parse };\n"],"names":["tag","tagName","children","attributes","Object","entries","map","k","v","encode","join","text","replace","markdownListRegex","list","match","a","b","inlineBlock","outdent","RegExp","createTaggedReplacement","regex","replacement","format","string","block","p","wrapper","language","class","trim","inline","subline","type","slice","length","name","h","parse","dontInline","temp","injectInlineBlock","code","parseInt","push","href","title","link","src","alt","rules","i","exec","matched","rest","index","_","d","apply","escaped","m","charCodeAt","parsed","String","fromCharCode"],"mappings":"AAOA,MAAMA,EAAMA,CAACC,EAASC,EAAUC,EAAa,CAAE,IAC5C,IAAGF,IAAUG,OAAOC,QAAQF,GAC1BG,IAAI,EAAEC,EAAGC,KAAQA,EAAK,IAAGD,MAAME,EAAOD,MAAQ,IAC9CE,KAAK,OAAOR,MAAaD,KAmBxBQ,EAASA,CAACE,EAAO,KACrBA,EAAKC,QAAQ,KAAM,UAAUA,QAAQ,KAAM,QAAQA,QAAQ,KAAM,QAE7DC,EAAoB,8CAOpBC,EAAQH,IACZ,MAAMV,EAAUU,EAAKI,MAAM,UAAY,KAAO,KAE9C,OAAOJ,EACHX,EACEC,EACAU,EAAKC,QAAQ,0CAA2C,CAACG,EAAOC,EAAGC,IACjEjB,EACE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