CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-showdown

A Markdown to HTML converter written in Javascript

Pending
Overview
Eval results
Files

instance-configuration.mddocs/

Instance Configuration

Methods for managing options on individual Converter instances, allowing per-instance customization that overrides global settings.

Capabilities

setOption

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.Converter

Usage 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|----------|----------|');

getOption

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): any

Usage 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);

getOptions

Gets all options for a specific Converter instance.

/**
 * Get all options for this converter instance
 * @returns Object containing all converter options
 */
converter.getOptions(): ConverterOptions

Usage 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());

setFlavor

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.Converter

Usage 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');

getFlavor

Gets the flavor for a specific Converter instance.

/**
 * Get flavor for this converter instance
 * @returns Current flavor name for this instance
 */
converter.getFlavor(): string

Usage 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 vs Global Configuration

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')); // false

Configuration Inheritance

Options 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')); // false

Constructor Options

Options 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']
});

Dynamic Configuration

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>

Option Validation

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

Performance Considerations

// 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);

Instance Extension Methods

addExtension

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.Converter

Usage 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');

useExtension

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.Converter

Usage 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');

removeExtension

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.Converter

Usage Examples:

const converter = new showdown.Converter();
converter.addExtension(someExtension, 'temp');

// Remove by name
converter.removeExtension('temp');

// Remove by extension object
converter.removeExtension(someExtension);

getAllExtensions

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');

Instance Metadata Methods

getMetadata

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): any

Usage 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"

getMetadataFormat

Gets the format of the metadata (e.g., 'yaml', 'json').

/**
 * Get metadata format
 * @returns Metadata format string
 */
converter.getMetadataFormat(): string

Usage 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

docs

cli.md

core-conversion.md

event-system.md

extension-system.md

flavor-management.md

global-configuration.md

index.md

instance-configuration.md

tile.json