A custom renderer for marked to output markdown to the terminal with rich formatting and styling
npx @tessl/cli install tessl/npm-marked-terminal@7.3.0marked-terminal is a custom renderer for the marked markdown parser that enables rendering markdown content directly to the terminal with rich formatting and styling. It provides comprehensive terminal output capabilities including syntax highlighting, customizable color schemes, table rendering, emoji support, and configurable text reflow.
npm install marked marked-terminal (marked is a required peer dependency)marked-terminal provides dual package exports supporting both ESM and CommonJS:
ESM (ES modules):
import { marked } from 'marked';
import { markedTerminal } from 'marked-terminal';
// For direct Renderer class usage:
import TerminalRenderer from 'marked-terminal';CommonJS:
const { marked } = require('marked');
const { markedTerminal } = require('marked-terminal');
// For legacy Renderer class usage:
const TerminalRenderer = require('marked-terminal').default;import { marked } from 'marked';
import { markedTerminal } from 'marked-terminal';
// Modern approach with marked.use()
marked.use(markedTerminal());
// Render markdown to terminal
const output = marked.parse('# Hello \nThis is **markdown** printed in the `terminal`');
console.log(output);Legacy usage with Renderer class:
import TerminalRenderer from 'marked-terminal';
import { marked } from 'marked';
marked.setOptions({
renderer: new TerminalRenderer()
});
console.log(marked.parse('# Hello \nThis is **markdown** printed in the `terminal`'));marked-terminal provides two main interfaces:
markedTerminal() function creates a marked extension for use with marked.use()Renderer class that can be used directly with marked.setOptions()Main rendering functionality that converts markdown elements to styled terminal output.
/**
* Creates a marked extension for rendering markdown to terminal
* @param options - Renderer configuration options
* @param highlightOptions - Syntax highlighting options passed to cli-highlight
* @returns Marked extension object for use with marked.use()
*/
function markedTerminal(options?: RendererOptions, highlightOptions?: HighlightOptions): MarkedExtension;
/**
* Terminal renderer class for marked markdown parser
* @param options - Renderer configuration options
* @param highlightOptions - Syntax highlighting options passed to cli-highlight
*/
class Renderer {
constructor(options?: RendererOptions, highlightOptions?: HighlightOptions);
}
interface MarkedExtension {
renderer: Record<string, Function>;
useNewRenderer: boolean;
}Rendering methods for text content and inline markdown elements.
// Text rendering
text(text: string | {text: string}): string;
strong(text: string | {tokens: any[]}): string;
em(text: string | {tokens: any[]}): string;
codespan(text: string | {text: string}): string;
del(text: string | {tokens: any[]}): string;
br(): string;
// Link and image rendering
link(href: string | {href: string, title?: string, tokens?: any[]}, title?: string, text?: string): string;
image(href: string | {href: string, title?: string, text?: string}, title?: string, text?: string): string;Rendering methods for block-level markdown elements.
// Headings and paragraphs
heading(text: string | {depth: number, tokens: any[]}, level?: number): string;
paragraph(text: string | {tokens: any[]}): string;
hr(): string;
// Code blocks
code(code: string | {text: string, lang?: string, escaped?: boolean}, lang?: string, escaped?: boolean): string;
// Blockquotes
blockquote(quote: string | {tokens: any[]}): string;
// HTML
html(html: string | {text: string}): string;Rendering methods for structured content like lists and tables.
// List rendering
list(body: string | {ordered: boolean, start?: number, loose?: boolean, items: any[]}, ordered?: boolean): string;
listitem(text: string | {task?: boolean, checked?: boolean, loose?: boolean, tokens: any[]}): string;
checkbox(checked: boolean | {checked: boolean}): string;
// Table rendering
table(header: string | {header: any[], rows: any[][]}, body?: string): string;
tablerow(content: string | {text: string}): string;
tablecell(content: string | {tokens: any[]}): string;Helper methods for text processing and measurement available on Renderer instances.
/**
* Calculate text length excluding ANSI escape codes (static method on Renderer class)
* @param str - Input string that may contain ANSI codes
* @returns Length of visible text characters
*/
Renderer.prototype.textLength(str: string): number;
/**
* Returns empty string for spacing in renderer
* @returns Empty string
*/
space(): string;interface RendererOptions {
// Color and styling functions
code?: ChalkFunction;
blockquote?: ChalkFunction;
html?: ChalkFunction;
heading?: ChalkFunction;
firstHeading?: ChalkFunction;
hr?: ChalkFunction;
listitem?: ChalkFunction;
table?: ChalkFunction;
paragraph?: ChalkFunction;
strong?: ChalkFunction;
em?: ChalkFunction;
codespan?: ChalkFunction;
del?: ChalkFunction;
link?: ChalkFunction;
href?: ChalkFunction;
text?: (text: string) => string;
// List formatting function
list?: (body: string, ordered: boolean, indent: string) => string;
// Text processing options
width?: number;
reflowText?: boolean;
showSectionPrefix?: boolean;
unescape?: boolean;
emoji?: boolean;
tab?: number | string;
// Table configuration
tableOptions?: Record<string, any>;
// Custom image handler
image?: (href: string, title?: string, text?: string) => string;
}
interface HighlightOptions {
language?: string;
theme?: string;
[key: string]: any;
}
type ChalkFunction = (text: string) => string;