md_parser

1.3.0 • Public • Published

Use in Node.js

Please note, that the parser isn't complete and still in progress! It's a part of a code competition ;-)

npm install md_parser

Use as command line tool (CLI)

npm install md_parser -g

CLI Usage

md_parser <source> <outputFile>
  1. The 1st argument (source) is required. If it is a file, the file will be read.
  2. The 2nd argument is optional. If it's given, it will be handled as an output file. If it's not given, the result will be printed in console.

Reference

Markdown Parsing Process

The parser parses the following elements in this way:

  • Bold text **Bold**
  • Italic text *Italic*
  • Striked text ~~Striked~~
  • Paragraphs (Text that is seperated with line breaks)
  • Lists (a sub list needs two spaces) * List item
    • Unsorted lists (circular) * Item
    • Sorted lists (numeric) 1. Item
    • Alphabetic Lists lower and upper (lower/upper-alpha) a. Item / A. Item
    • Roman Lists (upper-roman) I. Item
  • Tables (Table Syntax of Github Markdown)
  • Code Blocks (inline <code></code> and blocked <pre><code></code></pre>)
  • Links [Link Text](http://maurice-conrad.eu)
  • Images with optional width & height ![Image Name](https://image-url.com/image.png){width,height} (Link syntax with an ! prefix)
  • Abbreviations ?[This is an Abbreviation](I meant this!) (Link syntax with an ? prefix)
  • iFrames $[Frame Title](http://maurice-conrad.eu) (Link syntax with an $ prefix)
  • Details
    => Summary
       Detailed information

List parsing

Please make sure that the lists will all be parsed as <ul> containing the specific list style as CSS rule. Why? Because of support for untypically lists like alphabetic and roman.

Require

const markdown = require('md_parser');
// Returns the following
{
  parse: [Function],
  rules: [Array]
}

API

The md_parserinstance returns the method parse and the array rules. This array contains the default parsing rules and can be customized. You can see more below.

Parse

const markdown = require('md_parser');
 
var markdownStr = "# Title 1\n## Title 2\n\nParagraph"; // The string containing the markdown context
 
markdown.parse(markdownStr, {
  //rules: [], // Custom parsing rules. Don't use by default
  validDocument: true, // Wether the returned string is a valid HTML document with DOCTYPE, head, body etc.
  pretty: true, // Wether the result is pretty printed
  disallowedFeatures: [ // Disallowed feature classes e.g. 'github', 'default', '3rd-party'... (Used to prevent bugs with not official features like abbreviations, iframes, details). The classes of a feature are defined its object in the rules array
    //"html5",
    //"paragraph"
  ]
});

Parsing Rules

The parsing rules are an array with some regular expressions and functions.

The default rules are located in the file default-rules.js. These rules are sometimes just regular expressions and sometimes more complex functions.
The md_parser instance contains such an array with the default rules. If you call the parse method and you don't pass a custom array with rules as option, the rules array of the md_parser instance will be used.

Parsing Rules Reference

A rules array contains objects as rules. Every object represents one markdown element.
Have a look at the general structure of a rules array.

// Rules array
[
  {...}, // Some other rules
  {
    name: "name-of-element", // The name of the element.
    classes: ["default", "my-class", "special-feauture-class"],
    query: '(.*)', // A regular expression string
    replace: '<myReplacement>$1</myReplacement>', // The replacement for the regex
    parse: function(str) {
      // Special parsing function that returns the parsed string
      return str;
    }
  },
  {...} // Some other rules
]
Example

You don't understand it?
For example, let's have a look at the parsing rule for a strong tag:

{
  name: "bold",
  query: '\\*{2}([^\\*]*)\\*{2}', // Queries bold text
  replace: '<strong>$1</strong>'
}

As you can see, a special parse function isn't required in this case. In some other cases there is no regular expression but just a special parse function within the rule object.

Readme

Keywords

Package Sidebar

Install

npm i md_parser

Weekly Downloads

0

Version

1.3.0

License

ISC

Last publish

Collaborators

  • mauriceconrad