Rendering Markdown

Markdown is a popular, lightweight markup language. The language is similar to Wiki markup (used on Wikipedia), with an emphasis on simplicity. Its main purpose is to enable users to write plain text and get stylized, formatted HTML output. As such, it is used by popular websites, such as Reddit, Stack Overflow, GitHub, as well as various forums as a replacement of the less intuitive BBCode format.

Markdown is the fastest way to enable formatted text input for our users without embedding a full-fledged HTML editor into the page. There are multiple libraries to render markdown; in this recipe, we're going to use the simple markdown-js script to render markdown in real time.

How to do it...

Rendering markdown is very simple. A minimal example is as follows:

<!DOCTYPE HTML>
<html>
    <head>
        <title>Render markdown</title>
        <style type="text/css">
            #markdown, #render { width: 48%; min-height:320px; }
            #markdown { float: left; }
            #render { float: right; }
        </style>
    </head>
    <body>
        <textarea id="markdown">
# Markdown example.
This is an example of markdown text. We can link to [Google](http://www.google.com)
or insert Google's logo:
![Google Logo](https://www.google.com/images/srpr/logo3w.png)

## Text formatting
We can use *emphasis* or **strong** text,
> insert a quote
etc.</textarea>
        <div id="render"></div>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
        <script src="https://raw.github.com/spion/markdown-js/master/lib/markdown.js"></script>
        <script type="text/javascript">
            function rendermd(val) { $("#render").html(markdown.toHTML($("#markdown").val())); }
            $("#markdown").on('keyup', rendermd); $(rendermd);
        </script>
    </body>
</html>

How it works...

When the page is loaded, the markdown text in the textarea element is rendered into the #render element on the right-hand side. Every key press will also cause the script to update the rendered element.

There's more...

Find out more about the markdown format from its official website at http://daringfireball.net/projects/markdown/.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.151.45