10. April 2021

Converting Markdown to HTML on the fly in Web Pages

The Problem:

A long text on your web site, say, a privacy policy, with lots of headlines, lists and paragraphs. It is a lot of work to convert the text made in MS Word by your lawyer into the HTML that fits your web page design. 

The Solution:

Convert to Markdown, basically the plain text with '#'s and '-'s. Put the markdown into a <pre>, then let JavaScript on the client convert it into HTML replacing the Markdown text.

Like so:

<div id="html">
  <pre id="text">
# General

We take privacy seriously. 

[...long text with markdown...]

  </div>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/showdown/1.9.1/showdown.min.js"></script>

<script>
  $('#html').html(new showdown.Converter().makeHtml($('#text').text()));
</script>

Yes, a bit of jquery, which is already there in case of bootstrap.css and you will know how to do without.

If the conversion fails, there is always the markdown plaintext satisfiying the law. 

_happy_markdowning()

...just saying.

Keine Kommentare: