WYSIWYG-Markdown

by Nico

HTML

<!doctype html>
<html class="no-js" lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>WYSIWYG-Markdown</title>
  <link rel="stylesheet" href="./style.css">
  <meta name="description" content="A simple and easy to use WYSIWYG editor with markdown output.">

  <link rel="manifest" href="site.webmanifest">
  <meta name="theme-color" content="#fafafa">

  <script src="js/app.js" type="application/javascript"></script>
</head>
<body>
<div class="container">
  <h2>Editor preview</h2>
  <div id="md-editor"></div>

  <h5>hidden form textarea</h5>
  <textarea id="md-input" rows="2" cols="80" name="content">This is a **lightweight** markdown [editor](https://codeberg.org/turbopixel/WYSIWYG-Markdown) for your ~~app~~ website, written in vanilla javascript!</textarea>
</div>

<script>
  document.addEventListener("DOMContentLoaded", () => {
    MarkdownWysiwyg.init({
      textareaId: "md-input",
      mountId: "md-editor"
    });
  });
</script>
</body>
</html>

JavaScript

(function () {
  function escapeHtml(str) {
    return str
      .replace(/&/g, "&amp;")
      .replace(/</g, "&lt;")
      .replace(/>/g, "&gt;")
      .replace(/"/g, "&quot;")
      .replace(/'/g, "&#039;");
  }

  function createEl(tag, attrs) {
    var el = document.createElement(tag);
    if (attrs) for (var k in attrs) el.setAttribute(k, attrs[k]);
    return el;
  }

  function markdownToHtml(md) {
    var s = escapeHtml(md || "");
    s = s.replace(/~~(.*?)~~/g, "<del>$1</del>");
    s = s.replace(/\*\*(.*?)\*\*/g, "<strong>$1</strong>");
    s = s.replace(/(^|[^*])\*(?!\s)([^*]+?)\*(?!\S)/g, function (_, p1, p2) {
      return p1 + "<em>" + p2 + "</em>";
    });
    s = s.replace(/\[([^\]]+?)\]\(([^)]+?)\)/g, function (_, text, url) {
      return '<a href="' + url + '" title="' + text + '" target="_blank" rel="noopener noreferrer">' + text + "</a>";
    });

    var lines = s.split("\n");
    var out = [];
    var inList = false;

    function flushList() {
      if (inList) {
        out.push("</ul>");
        inList = false;
      }
    }

    for (var i = 0; i < lines.length; i++) {
      var line = lines[i];
      var m = /^(\s*)([-*])\s+(.*)$/.exec(line);
      if (m) {
        if (!inList) {
          out.push("<ul>");
          inList = true;
        }
        out.push("<li>" + m[3] + "</li>");
      } else {
        flushList();
        if (line.trim()) out.push("<p>" + line + "</p>");
      }
    }
    flushList();
    s = out.join("\n");
    return s;
  }

  function htmlToMarkdown(root) {
    function walk(node, inList) {
      if (node.nodeType === Node.TEXT_NODE) {
        return node.nodeValue || "";
      }
      if (node.nodeType !== Node.ELEMENT_NODE) return "";

      var tag = node.tagName.toLowerCase();

      if (tag === "br") return "\n";
      if (tag === "strong" || tag === "b") return "**" + childrenToMd(node, inList) + "**";
      if (tag === "em" || tag ===...