Pure JavaScript Markdown Parser - Developer Guide
Version: 1.0.1
Last Updated: March 25, 2026
- Quick Start
- Constructor
- Core Methods
- Configuration Options
- Helper Methods
- Usage Examples
- Supported Markdown Syntax
<script src="MDParser.js"></script>
<script>
var parser = new MDParser();
var html = parser.parse('# Hello World');
document.body.innerHTML = html;
</script>var MDParser = require('./MDParser.js');
var parser = new MDParser();
var html = parser.parse('# Hello World');
console.log(html);Creates a new MDParser instance.
Parameters:
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
| options | Object | No | {} | Configuration options object |
Returns: MDParser instance
Example:
// Default configuration
var parser = new MDParser();
// Custom configuration
var parser = new MDParser({
sanitize: true,
highlight: true,
gfm: true,
tables: true
});Converts Markdown text to HTML.
Syntax:
var html = parser.parse(markdown);Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
| markdown | string | Yes | Markdown formatted text |
Returns: string - Converted HTML string
Example:
var parser = new MDParser();
// Simple text
var html1 = parser.parse('# Heading');
// Returns: <h1>Heading</h1>
// Complex content
var html2 = parser.parse(`
# Main Heading
This is **bold** and *italic* text.
## Code Example
\`\`\`javascript
function hello() {
console.log('Hello!');
}
\`\`\`
## Unordered List
- Item 1
- Item 2
- Item 3
## Ordered List
1. First item
2. Second item
3. Third item
## Task List
- [x] Completed
- [ ] Incomplete
## Blockquote
> This is a blockquote
## Table
| Name | Version | Status |
|------|---------|--------|
| MDParser | 1.0.1 | Stable |
`);| Option | Type | Default | Description |
|---|---|---|---|
| sanitize | boolean | true | Enable HTML sanitization for XSS protection |
| highlight | boolean | true | Enable syntax highlighting for code blocks |
| highlightTheme | string | 'github' | Code highlighting theme (github only for now) |
| linkify | boolean | true | Auto-convert URLs to clickable links |
| breaks | boolean | false | Convert single newlines to <br> tags |
| gfm | boolean | true | Enable GitHub Flavored Markdown |
| tables | boolean | true | Enable table syntax |
| tasklists | boolean | true | Enable task list syntax |
Set configuration options.
Syntax:
parser.setOptions(options);Example:
var parser = new MDParser();
// Modify options
parser.setOptions({
highlightTheme: 'github',
breaks: true,
tables: false
});Get current configuration.
Syntax:
var options = parser.getOptions();Returns: Object - Copy of current configuration
Example:
var parser = new MDParser();
var currentOptions = parser.getOptions();
console.log(currentOptions.highlight); // trueEscape HTML special characters to prevent XSS attacks.
Syntax:
var escaped = parser.escapeHtml(text);Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
| text | string | Yes | Text to escape |
Returns: string - Escaped text
Example:
var parser = new MDParser();
var safe = parser.escapeHtml('<script>alert("XSS")</script>');
// Returns: <script>alert("XSS")</script>Get parser version number.
Syntax:
var version = parser.getVersion();Returns: string - Version number
Example:
var parser = new MDParser();
console.log(parser.getVersion()); // "1.0.1"Get list of supported features.
Syntax:
var features = parser.getFeatures();Returns: Array - Array of supported feature names
Example:
var parser = new MDParser();
var features = parser.getFeatures();
console.log(features);
// ["headings", "blockquotes", "lists", "tasklists", "codeblocks", ...]<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>MDParser Demo</title>
<script src="MDParser.js"></script>
</head>
<body>
<textarea id="editor" placeholder="Enter Markdown here..."></textarea>
<div id="preview"></div>
<script>
var parser = new MDParser({
highlight: true,
gfm: true
});
var editor = document.getElementById('editor');
var preview = document.getElementById('preview');
editor.addEventListener('input', function() {
var html = parser.parse(editor.value);
preview.innerHTML = html;
});
</script>
</body>
</html>var parser = new MDParser({
sanitize: true,
highlight: true,
gfm: true,
tables: true,
tasklists: true
});
var markdownContent = `
# My Blog Article
**Author**: John Doe
**Date**: 2026-03-25
## Introduction
This is a sample article written in Markdown.
## Code Example
\`\`\`javascript
function fibonacci(n) {
if (n <= 1) return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
console.log(fibonacci(10)); // 55
\`\`\`
## Task List
- [x] Markdown parsing
- [x] Code highlighting
- [x] Table support
- [ ] More features coming
## Table
| Feature | Status | Description |
|---------|--------|-------------|
| Headings | Done | Supports H1-H6 |
| Lists | Done | Ordered and unordered |
| Highlighting | Done | Built-in syntax highlighting |
## Links & Images
[Visit GitHub](https://github.com)

## Blockquote
> Thanks for using MDParser!
`;
var html = parser.parse(markdownContent);
document.getElementById('article').innerHTML = html;var parser = new MDParser({
sanitize: true, // Required for XSS protection
gfm: true,
tasklists: false // Not needed in comments
});
function renderComment(markdownText) {
return parser.parse(markdownText);
}
// Usage
var commentHTML = renderComment('User says: This article is **awesome**! [View original](https://example.com)');
document.getElementById('comment').innerHTML = commentHTML;# Heading 1
## Heading 2
### Heading 3
#### Heading 4
##### Heading 5
###### Heading 6**Bold text**
*Italic text*
~~Strikethrough text~~Unordered List:
- Item 1
- Item 2
- Nested itemOrdered List:
1. First item
2. Second item
3. Third itemTask List:
- [x] Completed
- [ ] IncompleteInline Code:
This is `inline code`Code Block:
```javascript
function hello() {
console.log('Hello');
}
```Supported languages: javascript, js, python, html, css, json, sql, bash
[Link Text](https://example.com)
Image alt text is optional
> This is a blockquote
> Can span multiple lines| Column 1 | Column 2 | Column 3 |
|----------|----------|----------|
| Content | Content | Content |---https://example.comAutomatically converted to clickable links
Use \* to escape asterisks
Use \\ to escape backslashes-
Security: Keep
sanitizeoption astrueto prevent XSS attacks. -
Performance: For large content, use debounce to optimize rendering frequency.
-
Code Highlighting: Supports syntax highlighting for JavaScript, Python, HTML, CSS, JSON, SQL, Bash and more.
-
Compatibility: Written in pure JavaScript, compatible with all modern browsers.
- Fixed code blocks, tables, blockquotes, links, and images rendering issues
- Fixed inline elements rendering inside lists
- Optimized code highlighting logic
- Improved regex matching
- Initial release
- Support for common Markdown syntax
- Code highlighting support
- GitHub Flavored Markdown extensions (tables, task lists)
- Multiple configuration options