Giter Site home page Giter Site logo

wmd's Introduction

WMD

A Markdown parser for CommonJS (and node.js) based on the excellent Showdown.

Essentially WMD, is just a wrapper around Showdown, with a few hooks for preprocessing, and some default preprocessors and postprocessors.

Example

var wmd = require('wmd');

var html = wmd('Markdown *rocks*.');
console.log(html);

Documentation

wmd(markdown, options)

  • markdown - A string containing Markdown.
  • options - (optional) An object containing options (see options section)

The main function for converting Markdown to HTML, and normally the only function you'll need ot use. Applies all preprocessors defined in options before passing the result to Showdown for the final rendering.

By default, the underscores and metadata preprocessors are used.

This function returns a doc object. The contents of the doc object may differ depending on the preprocessors used, but will always contain the following:

  • doc.html - The final HTML output of the conversion.
  • doc.markdown - The markdown text passed to the processsor after all preprocesor functions have been applied.
  • doc.raw - The raw string before preprocessors were applied.

The string representation of a doc object (doc.toString()) is the same as doc.html.

wmd.preprocessors

An object containing core preprocessor functions:

  • underscores - code-friendly underscore processing taken from GitHub Flavored Markdown. This means the bar in foo_bar_baz does not get emphasis.

  • fencedCodeBlocks - GitHub style fenced code blocks

  • yamlFrontMatter - Jekyll style YAML front matter for metadata

  • metadata - takes metatdata information from the top of a markdown file and adds it to doc.metadata.

    property1: some value
    property2: multi
               line
               value
    
    # Markdown goes here
    

    Would result in the following doc object:

    {
        metadata: {
            property1: "some value",
            property2: "multi\nline\nvalue"
        },
        html: "<h1>Markdown goes here</h1>",
        markdown: "# Markdown goes here",
        raw: "property1: some value\nproperty2: multi\nline\nvalue\n\n# Markdown goes here"
    }
    

Adding preprocessors to wmd:

var wmd = require('wmd');
var html = wmd('Markdown *rocks*.', {
    preprocessors: [
        function (doc) {
            doc.markdown += '.. even more!';
            return doc;
        }
    ]
});

By default, the underscores and metadata preprocessors will be used.

wmd.postprocessors

An object containing core postprocessor functions:

  • jsdom - uses jsdom to add doc.window containing the HTML generated from markdown
  • first_para - adds doc.first_para containing the text in the first p tag
  • heading - adds doc.heading containing the text in the first h1 tag

Adding postprocessors to wmd:

var wmd = require('wmd');
var html = wmd('Markdown *rocks*.', {
    postprocessors: [
        function (doc) {
            doc.html += '<b>more html stuff</b>';
            return doc;
        }
    ]
});

By default, no postprocessors will be used.

wmd.processor(markdown)

  • markdown - A string containing Markdown.

The function which performs the conversion from markdown to html. By default this is just Showdown's makeHTML function.

wmd.preprocess(doc, options)

  • doc - A doc object
  • options - (optional) An object containing options (see options section)

Applies the preprocessor functions defined in options to the doc (usually updating doc.markdown, sometimes adding new properties) before the doc is passed to the processor.

wmd.postprocess(doc, options)

  • doc - A doc object
  • options - (optional) An object containing options (see options section)

Applies the postprocessor functions defined in options to the doc.

wmd.readOptions(options)

  • options - (optional) An object containing options (see options section)

You would not normally need to call this directly. This function adds default options to those passed to the main wmd function.

Options

  • preprocessors - An array of functions which can transform the document before its passed to the processor function. By default the underscores and metadata preprocessors are used.
  • postprocessors - An array of functions which can transform the document after its been passed to the processor function. By default, no postprocessors are used.

wmd's People

Stargazers

 avatar leornado avatar lukaszkups avatar mrmnmly avatar Luke Plaster avatar Matthew Scragg avatar โœŒ Makis Tracend avatar Markus Tiefenbacher avatar Dinesh Kr. Choudhary avatar Michael R. Pralow avatar Rex Staples avatar Rui Carmo avatar Judd Kussrow avatar Jeff Barczewski avatar Caolan McMahon avatar

Watchers

Caolan McMahon avatar James Cloos avatar  avatar

wmd's Issues

Standalone metadata preprocessor

@caolan, I would like to use your metadata preprocessor, but I would like to use it with the marked engine. Are you interested in splitting it out into a standalone repo? Or would you like me to?

FWIW, I wrote a number of other pre-processors that I'm using with the marked engine. My current plan is to adopt the same preprocessor API that you are using (taking and returning an object with a markdown property).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.