A Markdown to HTML converter written in Javascript
—
Methods for managing options on individual Converter instances, allowing per-instance customization that overrides global settings.
Sets an option for a specific Converter instance.
/**
* Set option for this converter instance
* @param key - Option name
* @param value - Option value
* @returns Converter instance for chaining
*/
converter.setOption(key: string, value: any): showdown.ConverterUsage Examples:
const converter = new showdown.Converter();
// Set instance-specific options
converter.setOption('tables', true);
converter.setOption('strikethrough', true);
// Chaining is supported
converter
.setOption('emoji', true)
.setOption('ghMentions', true)
.setOption('tasklists', true);
// This converter now has these options enabled
const html = converter.makeHtml('| Column 1 | Column 2 |\n|----------|----------|');Gets the value of an option for a specific Converter instance.
/**
* Get option value for this converter instance
* @param key - Option name
* @returns Option value
*/
converter.getOption(key: string): anyUsage Examples:
const converter = new showdown.Converter({ tables: true });
// Get instance option
const tablesEnabled = converter.getOption('tables');
console.log(tablesEnabled); // true
// Check if option is set
if (converter.getOption('strikethrough')) {
console.log('Strikethrough is enabled for this converter');
}
// Compare with global option
const globalTables = showdown.getOption('tables');
const instanceTables = converter.getOption('tables');
console.log('Global vs instance:', globalTables, instanceTables);Gets all options for a specific Converter instance.
/**
* Get all options for this converter instance
* @returns Object containing all converter options
*/
converter.getOptions(): ConverterOptionsUsage Examples:
const converter = new showdown.Converter({
tables: true,
strikethrough: true,
ghCodeBlocks: true
});
// Get all instance options
const options = converter.getOptions();
console.log(options);
// Check multiple options
const opts = converter.getOptions();
if (opts.tables && opts.strikethrough) {
console.log('This converter supports GitHub-style tables and strikethrough');
}
// Create another converter with same options
const converter2 = new showdown.Converter(converter.getOptions());Sets the flavor for a specific Converter instance.
/**
* Set flavor for this converter instance
* @param name - Flavor name
* @returns Converter instance for chaining
*/
converter.setFlavor(name: string): showdown.ConverterUsage Examples:
const converter = new showdown.Converter();
// Set GitHub flavor for this instance
converter.setFlavor('github');
// Chaining with other operations
converter
.setFlavor('github')
.setOption('emoji', false); // Override flavor setting
// Multiple converters with different flavors
const githubConverter = new showdown.Converter().setFlavor('github');
const originalConverter = new showdown.Converter().setFlavor('original');
const ghostConverter = new showdown.Converter().setFlavor('ghost');Gets the flavor for a specific Converter instance.
/**
* Get flavor for this converter instance
* @returns Current flavor name for this instance
*/
converter.getFlavor(): stringUsage Examples:
const converter = new showdown.Converter();
converter.setFlavor('github');
// Get instance flavor
const flavor = converter.getFlavor();
console.log('Converter flavor:', flavor); // 'github'
// Conditional processing based on flavor
if (converter.getFlavor() === 'github') {
console.log('Using GitHub-compatible converter');
} else if (converter.getFlavor() === 'original') {
console.log('Using original Markdown converter');
}Instance options override global options:
// Set global option
showdown.setOption('tables', false);
// Create converter with global settings
const converter1 = new showdown.Converter();
console.log(converter1.getOption('tables')); // false
// Override for specific instance
const converter2 = new showdown.Converter({ tables: true });
console.log(converter2.getOption('tables')); // true
// Global setting unchanged
console.log(showdown.getOption('tables')); // falseOptions are inherited from global settings at creation time:
// Set global options
showdown.setOption('strikethrough', true);
showdown.setOption('tables', true);
// Create converter (inherits global options)
const converter = new showdown.Converter();
console.log(converter.getOption('strikethrough')); // true
console.log(converter.getOption('tables')); // true
// Change global options
showdown.setOption('strikethrough', false);
// Existing converter retains its options
console.log(converter.getOption('strikethrough')); // true (unchanged)
// New converter inherits current global options
const newConverter = new showdown.Converter();
console.log(newConverter.getOption('strikethrough')); // falseOptions can be set during construction:
// Single option
const converter = new showdown.Converter({ tables: true });
// Multiple options
const converter2 = new showdown.Converter({
tables: true,
strikethrough: true,
ghCodeBlocks: true,
tasklists: true,
emoji: true
});
// Combining with extensions
const converter3 = new showdown.Converter({
tables: true,
extensions: ['highlight', 'mentions']
});Options can be changed after creation:
const converter = new showdown.Converter();
// Initial conversion
let html = converter.makeHtml('~~strikethrough~~');
console.log(html); // <p>~~strikethrough~~</p>
// Enable strikethrough
converter.setOption('strikethrough', true);
// Same text now processes differently
html = converter.makeHtml('~~strikethrough~~');
console.log(html); // <p><del>strikethrough</del></p>Invalid options are generally ignored:
const converter = new showdown.Converter();
// Invalid option name (ignored silently)
converter.setOption('nonExistentOption', true);
// Invalid option value (type coercion may occur)
converter.setOption('tables', 'yes'); // Treated as truthy
// Getting invalid option returns undefined
console.log(converter.getOption('nonExistentOption')); // undefined// Efficient: Set options once
const converter = new showdown.Converter({
tables: true,
strikethrough: true,
ghCodeBlocks: true
});
// Use for multiple conversions
const html1 = converter.makeHtml(markdown1);
const html2 = converter.makeHtml(markdown2);
// Less efficient: Changing options frequently
converter.setOption('tables', false);
const html3 = converter.makeHtml(markdown3);
converter.setOption('tables', true);
const html4 = converter.makeHtml(markdown4);Adds an extension to a specific Converter instance.
/**
* Add extension to this converter instance
* @param extension - Extension object or array of extensions
* @param name - Optional extension name
* @returns Converter instance for chaining
*/
converter.addExtension(extension: Extension, name?: string): showdown.ConverterUsage Examples:
const converter = new showdown.Converter();
// Add extension object directly
const highlightExt = {
type: 'output',
regex: /<pre><code>/g,
replace: '<pre class="highlighted"><code>'
};
converter.addExtension(highlightExt, 'highlight');
// Add registered extension by name
converter.addExtension('my-registered-extension');
// Chaining
converter
.addExtension(ext1, 'ext1')
.addExtension(ext2, 'ext2');Uses a globally registered extension in this converter instance.
/**
* Use a globally registered extension
* @param extensionName - Name of registered extension
* @returns Converter instance for chaining
*/
converter.useExtension(extensionName: string): showdown.ConverterUsage Examples:
// Register extension globally first
showdown.extension('highlight', highlightExtension);
// Use in specific converter instance
const converter = new showdown.Converter();
converter.useExtension('highlight');
// Multiple extensions
converter
.useExtension('highlight')
.useExtension('mentions')
.useExtension('alerts');Removes an extension from this converter instance.
/**
* Remove extension from this converter instance
* @param extension - Extension object, name, or array
* @returns Converter instance for chaining
*/
converter.removeExtension(extension: Extension | string): showdown.ConverterUsage Examples:
const converter = new showdown.Converter();
converter.addExtension(someExtension, 'temp');
// Remove by name
converter.removeExtension('temp');
// Remove by extension object
converter.removeExtension(someExtension);Gets all extensions for this converter instance.
/**
* Get all extensions for this converter instance
* @returns Array of extensions
*/
converter.getAllExtensions(): Extension[]Usage Examples:
const converter = new showdown.Converter();
converter.addExtension(ext1, 'ext1');
converter.addExtension(ext2, 'ext2');
// Get all extensions
const extensions = converter.getAllExtensions();
console.log('Converter has', extensions.length, 'extensions');
// Check if specific extension is loaded
const hasHighlight = converter.getAllExtensions().some(ext => ext.name === 'highlight');Gets document metadata parsed during conversion.
/**
* Get document metadata
* @param raw - If true, returns raw metadata string
* @returns Parsed metadata object or raw string
*/
converter.getMetadata(raw?: boolean): anyUsage Examples:
const converter = new showdown.Converter({ metadata: true });
const markdown = `---
title: My Document
author: John Doe
date: 2023-01-01
---
# Content`;
const html = converter.makeHtml(markdown);
// Get parsed metadata
const metadata = converter.getMetadata();
console.log(metadata); // { title: 'My Document', author: 'John Doe', date: '2023-01-01' }
// Get raw metadata
const rawMetadata = converter.getMetadata(true);
console.log(rawMetadata); // "title: My Document\nauthor: John Doe\ndate: 2023-01-01"Gets the format of the metadata (e.g., 'yaml', 'json').
/**
* Get metadata format
* @returns Metadata format string
*/
converter.getMetadataFormat(): stringUsage Examples:
const converter = new showdown.Converter({ metadata: true });
// Convert document with YAML frontmatter
const html = converter.makeHtml(markdownWithYamlFrontmatter);
// Check metadata format
const format = converter.getMetadataFormat();
console.log('Metadata format:', format); // 'yaml'Install with Tessl CLI
npx tessl i tessl/npm-showdown