CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-markdown

A sensible Markdown parser for javascript

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

Markdown

A sensible Markdown parser for JavaScript that produces well-formed HTML through a multi-stage process with intermediate JSON representations. It supports both Node.js and browser environments, offers extensible dialect support, and includes a command-line tool for markdown-to-HTML conversion.

Package Information

  • Package Name: markdown
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install markdown
  • Binary Command: md2html (when installed globally with -g)

Core Imports

// Main API (recommended approach)
var markdown = require("markdown").markdown;

// Alternative import: top-level convenience export
var { parse } = require("markdown");  // Convenience alias for markdown.toHTML

// Access all functions through the main markdown object:
var actualParse = markdown.parse;         // Parse to JsonML tree
var toHTML = markdown.toHTML;             // Convert to HTML string  
var toHTMLTree = markdown.toHTMLTree;     // Convert to HTML JsonML
var renderJsonML = markdown.renderJsonML; // Render JsonML to string

For browser environments:

<script src="lib/markdown.js"></script>
<script>
  // Available as window.markdown
  var html = markdown.toHTML("Hello *World*!");
</script>

Basic Usage

var markdown = require("markdown").markdown;

// Simple conversion - markdown string to HTML
var html = markdown.toHTML("Hello **World**!");
console.log(html); // <p>Hello <strong>World</strong>!</p>

// Two-step process for processing intermediate data
var tree = markdown.parse("Hello *World*!");
var htmlTree = markdown.toHTMLTree(tree);
var html = markdown.renderJsonML(htmlTree);

// Using different dialects
var html = markdown.toHTML("Hello **World**!", "Maruku");

// Alternative: use the convenience top-level parse export
var parse = require("markdown").parse; // Convenience alias for toHTML
var html = parse("Hello **World**!"); // Same as markdown.toHTML()

Architecture

The markdown package uses a three-stage processing architecture:

  1. Parse Stage: Converts markdown text into a JsonML tree representing markdown structure
  2. Transform Stage: Converts the markdown JsonML tree into an HTML JsonML tree, resolving references and applying dialect rules
  3. Render Stage: Serializes the HTML JsonML tree into well-formed HTML strings

Key architectural components:

  • Markdown Class: Core processor with dialect support and block/inline parsing
  • Dialect System: Extensible rule sets (Gruber, Maruku) defining parsing behavior
  • JsonML Representation: Intermediate tree format enabling manipulation between stages
  • Block/Inline Processors: Separate rule engines for block-level and inline elements
  • Reference Resolution: Link and image reference processing with attribute management

Capabilities

Core Parsing and Conversion

Essential functions for converting markdown to HTML with optional intermediate processing.

/**
 * Parse markdown text into JsonML tree representation
 * @param {string} source - Markdown text to parse
 * @param {string|Object} dialect - Dialect name or dialect object (default: "Gruber")
 * @returns {Array} JsonML tree representing markdown structure
 */
function parse(source, dialect);

/**
 * Convert markdown text or JsonML tree directly to HTML string
 * @param {string|Array} source - Markdown text or JsonML tree
 * @param {string|Object} dialect - Dialect name or dialect object (default: "Gruber")
 * @param {Object} options - Processing options
 * @returns {string} HTML string
 */
function toHTML(source, dialect, options);

/**
 * Convert markdown to HTML JsonML tree for processing
 * @param {string|Array} input - Markdown text or markdown JsonML tree
 * @param {string|Object} dialect - Dialect name or dialect object (default: "Gruber")
 * @param {Object} options - Processing options
 * @returns {Array} JsonML tree representing HTML structure
 */
function toHTMLTree(input, dialect, options);

/**
 * Render JsonML tree to well-formed HTML/XML string
 * @param {Array} jsonml - JsonML array to render
 * @param {Object} options - Rendering options
 * @param {boolean} options.root - Include root element in output (default: false)
 * @returns {string} HTML/XML string
 */
function renderJsonML(jsonml, options);

Core API

Markdown Processor Class

Advanced processing with full control over parsing stages and dialect customization.

/**
 * Create new Markdown processor instance
 * @param {string|Object} dialect - Dialect name or dialect object
 * @constructor
 * @throws {Error} When dialect is unknown
 */
function Markdown(dialect);

// Key instance methods
/**
 * Parse source into JsonML markdown tree
 * @param {string|Array} source - Markdown source or block array
 * @param {Array} custom_root - Custom root node (optional)
 * @returns {Array} JsonML markdown tree
 */
Markdown.prototype.toTree = function(source, custom_root);

/**
 * Process individual block and return JsonML nodes
 * @param {string} block - Block to process
 * @param {Array} next - Following blocks
 * @returns {Array} Array of JsonML nodes
 */
Markdown.prototype.processBlock = function(block, next);

/**
 * Process inline content within a block
 * @param {string} block - Block content to process
 * @returns {Array} Processed inline content array
 */
Markdown.prototype.processInline = function(block);

Markdown Class

Dialect System

Extensible parsing rules supporting Gruber (default) and Maruku dialects with custom dialect creation.

// Built-in dialects
var Markdown.dialects.Gruber; // Default Gruber dialect
var Markdown.dialects.Maruku; // Extended Maruku dialect

/**
 * Create dialect subclass with prototype inheritance
 * @param {Object} d - Base dialect to extend
 * @returns {Object} New dialect with inherited block and inline processors
 */
function Markdown.subclassDialect(d);

/**
 * Build processing order for block rules in dialect
 * @param {Object} d - Dialect block rules object
 */
function Markdown.buildBlockOrder(d);

/**
 * Build regex patterns for inline rules in dialect
 * @param {Object} d - Dialect inline rules object
 */
function Markdown.buildInlinePatterns(d);

Dialects

Command Line Interface

CLI tool for converting markdown files to HTML with dialect selection.

// Command: md2html [options] [file]
// Options:
//   --dialect=DIALECT  Choose "Gruber" (default) or "Maruku"
//   --help            Show usage information
// 
// Input: File path or stdin (-)
// Output: HTML to stdout

Usage Examples:

# Convert file
md2html document.md > document.html

# Use Maruku dialect  
md2html --dialect=Maruku document.md

# Process from stdin
echo "# Hello World" | md2html

Command Line

Types

/**
 * JsonML (JSON Markup Language) format for representing structured markup
 * 
 * Basic Structure: [tag_name, attributes, ...children]
 * - tag_name: String - HTML/XML tag name
 * - attributes: Object - Key-value pairs for attributes (can be empty {})
 * - children: Mixed - Text strings or nested JsonML arrays
 * 
 * Examples:
 * ["p", {}, "Simple paragraph"]
 * ["p", { class: "highlight" }, "Paragraph with class"]
 * ["p", {}, "Hello ", ["strong", {}, "World"], "!"]
 * ["div", {}, ["h1", {}, "Title"], ["p", {}, "Content"]]
 */

/**
 * Block object with metadata
 * @typedef {string} Block
 * @property {string} trailing - Trailing whitespace/newlines
 * @property {number} lineNumber - Source line number
 * @property {Function} inspect - Debug inspection method
 * @property {Function} toSource - Source serialization method
 */

/**
 * Dialect object defining parsing rules
 * @typedef {Object} Dialect
 * @property {Object} block - Block-level processing rules
 * @property {Object} inline - Inline processing rules
 * @property {Array} block.__order__ - Block processor execution order
 * @property {string} inline.__patterns__ - Inline pattern regex
 * @property {Function} inline.__call__ - Main inline processor
 */

/**
 * Processing options for HTML conversion
 * @typedef {Object} ProcessingOptions
 * @property {Function} preprocessTreeNode - Tree preprocessing function
 */

/**
 * Rendering options for JsonML output
 * @typedef {Object} RenderingOptions
 * @property {boolean} root - Include root element in output
 */

docs

command-line.md

core-api.md

dialects.md

index.md

markdown-class.md

tile.json