Skip to content

escape

Escapes all Markdown special characters in a string so they render as literal text rather than formatting syntax.

Escaped characters: \ \ * _ { } [ ] ( ) # + - . !`

Pass { cell: true } to also escape pipe characters and replace newlines with spaces, making the result safe for embedding in a Markdown table cell.

Available since v2.0.0

import { escape } from '@helpers4/markdown';
// or, from the all-in-one package (same code, one install):
import { escape } from 'helpers4/markdown';
escape(str: string, options?: EscapeOptions): string
ParameterTypeDescription
strstringThe raw string to escape
optionsEscapeOptionsOptional escaping options (optional)

string — The escaped string

Prefixes every Markdown special character with a backslash.

escape('**bold** and _italic_')
// => '\\*\\*bold\\*\\* and \\_italic\\_'

Prevents user-supplied strings from breaking Markdown formatting.

const userInput = '(C) [helpers4]';
const safe = escape(userInput);
// => '\\(C\\) \\[helpers4\\]'

Options for escape.

interface EscapeOptions {
  cell?: boolean;
}

View source on GitHub