diff --git a/frontend.js b/frontend.js index d9ff612..37d99dc 100644 --- a/frontend.js +++ b/frontend.js @@ -3,6 +3,96 @@ // Functions for commonly used elements +//Convert markdown to HTML and back +function markdownToHTML(markdown) { + // Replace headers (h1, h2, h3) with corresponding HTML tags + markdown = markdown.replace(/^# (.*$)/gim, '

$1

'); + markdown = markdown.replace(/^## (.*$)/gim, '

$1

'); + markdown = markdown.replace(/^### (.*$)/gim, '

$1

'); + + // Replace bold and italic text with corresponding HTML tags + markdown = markdown.replace(/\*\*(.*)\*\*/gim, '$1'); + markdown = markdown.replace(/\*(.*)\*/gim, '$1'); + + // Replace unordered list items with HTML list tags + markdown = markdown.replace(/^\s*-\s(.*)$/gim, '
  • $1
  • '); + markdown = markdown.replace(/<\/li>\n
  • /gim, '
  • '); + markdown = ''; + + // Replace ordered list items with HTML list tags + markdown = markdown.replace(/^\s*\d+\.\s(.*)$/gim, '
  • $1
  • '); + markdown = markdown.replace(/<\/li>\n
  • /gim, '
  • '); + markdown = '
      ' + markdown + '
    '; + + // Replace inline code with HTML tag + markdown = markdown.replace(/`(.*?)`/gim, '$1'); + + // Replace blockquotes with HTML
    tag + markdown = markdown.replace(/^\s*> (.*)$/gim, '
    $1
    '); + + // Replace horizontal rules with HTML
    tag + markdown = markdown.replace(/^\s*[-*_]{3,}\s*$/gim, '
    '); + + // Replace line breaks with HTML
    tag + markdown = markdown.replace(/\n$/gim, '
    '); + + // Replace images with HTML tag + markdown = markdown.replace(/!\[(.*?)\]\((.*?)\)/gim, '$1'); + + // Replace links with HTML tag + markdown = markdown.replace(/\[(.*?)\]\((.*?)\)/gim, '$1'); + + markdown = markdown.replaceAll("", "") + + return markdown; +} + +function htmlToMarkdown(html) { + // Replace headers (h1, h2, h3) with corresponding Markdown tags + html = html.replace(/

    (.*?)<\/h1>/gim, '# $1'); + html = html.replace(/

    (.*?)<\/h2>/gim, '## $1'); + html = html.replace(/

    (.*?)<\/h3>/gim, '### $1'); + + // Replace bold and italic text with corresponding Markdown tags + html = html.replace(/(.*?)<\/b>/gim, '**$1**'); + html = html.replace(/(.*?)<\/i>/gim, '*$1*'); + + // Replace unordered list items with Markdown list tags + html = html.replace(/