Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Markdown editor

Markdown renderer and editor in browser in frontend development could be useful for many applications for content authoring.

markdown-it package

#install markdown-it
npm install markdown-it
#install markdown-it addons
npm install markdown-it-abbr markdown-it-container markdown-it-deflist markdown-it-emoji markdown-it-footnote markdown-it-ins markdown-it-mark markdown-it-sub markdown-it-sup
#install highlighter for markdown
npm install highlight.js

Setting up for markdown editing

Add a javascript file, for example myjs.js as shown,

'use strict';

const MarkdownIt = require('markdown-it');
module.exports.mdHtml = new MarkdownIt()
    .use(require('markdown-it-abbr'))
    .use(require('markdown-it-container'), 'warning')
    .use(require('markdown-it-deflist'))
    .use(require('markdown-it-emoji'))
    .use(require('markdown-it-footnote'))
    .use(require('markdown-it-ins'))
    .use(require('markdown-it-mark'))
    .use(require('markdown-it-sub'))
    .use(require('markdown-it-sup'));

Now we can wrap this javascript for browser and use it our html web app.

For instance in Svelte we can do the following,

<script>
    import md from "./myjs.js";
  
    let src = 'markdown content';
    $:markdown = md.render(src);
  };
</script>

<textarea bind:value={source} />
<div>{markdown}</div>

Synchronized scrolling

This is a rather interesting subject. This sample project I did, implements it using the scheme used in markdown-it demo. VS code uses something probably similar, but they have more feature. VS code source is worth exploring to learn more.

Every time the content is updated, the demo injects the line numbers in the generated content using injectLineNumbers. Next, buildScrollMap builds a map of line number versus position using a hidden element, sourceLikeDiv. This map is used by the following scroll handlers,

  • syncSrcScroll: handler that monitors generated content scroll position and synchronizes the markdown source position.
  • syncResultScroll: handler that monitors markdown source content scroll position and synchronizes the generated content position.

Showdown.js

Showdown use

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>dev_cookbook</title>
    <script src="https://unpkg.com/showdown/dist/showdown.min.js"></script>
</head>

<body>
    <script>
        var div = document.createElement("DIV");
        document.body.appendChild(div);   // Append <button> to <body>
        var converter = new showdown.Converter();
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (this.readyState == 4 && this.status == 200) {
                div.innerHTML = converter.makeHtml(this.responseText);
            }
        };
        xhttp.open("GET", "README.md", true);
        xhttp.send();
    </script>
</body>

</html>

Using a code editor for entering text

Instead of a text area to enter source, we an use a code editor.

  • Dillinger is a good example, Github source. It also integrated server side pdf generation of markdown render.
  • Dillinger uses Ace code editor, Github source. Ace allows highlighting code.
  • highlight.js has Markdown syntax highlighting, integrating markdown highlighting might be a good idea.