Skip to content

Type Editor


Type Editor / @type-editor/markdown / to-markdown/MarkdownSerializerState / MarkdownSerializerState

Class: MarkdownSerializerState

Defined in: to-markdown/MarkdownSerializerState.ts:13

This is an object used to track state and expose methods related to markdown serialization. Instances are passed to node and mark serialization methods (see toMarkdown).

Constructors

Constructor

ts
new MarkdownSerializerState(
   nodes,
   marks,
   options): MarkdownSerializerState;

Defined in: to-markdown/MarkdownSerializerState.ts:40

Create a new serializer state for tracking the markdown serialization process.

Parameters

ParameterTypeDescription
nodesRecord<string, NodeSerializerFunc>A record mapping node type names to their serializer functions
marksRecord<string, MarkSerializerSpec>A record mapping mark type names to their serializer specifications
optionsMarkdownSerializerOptionsConfiguration options for the serializer behavior

Returns

MarkdownSerializerState

Accessors

closed

Get Signature

ts
get closed(): Node_2;

Defined in: to-markdown/MarkdownSerializerState.ts:66

Returns

Node_2


Get Signature

ts
get inAutolink(): boolean;

Defined in: to-markdown/MarkdownSerializerState.ts:70

Returns

boolean

Set Signature

ts
set inAutolink(inAutolink): void;

Defined in: to-markdown/MarkdownSerializerState.ts:74

Parameters
ParameterType
inAutolinkboolean
Returns

void


out

Get Signature

ts
get out(): string;

Defined in: to-markdown/MarkdownSerializerState.ts:58

Returns

string

Set Signature

ts
set out(out): void;

Defined in: to-markdown/MarkdownSerializerState.ts:62

Parameters
ParameterType
outstring
Returns

void

Methods

atBlank()

ts
atBlank(): boolean;

Defined in: to-markdown/MarkdownSerializerState.ts:132

Check if the output is currently at a blank position (empty or ends with newline).

Returns

boolean

True if the output is empty or ends with a newline


closeBlock()

ts
closeBlock(node): void;

Defined in: to-markdown/MarkdownSerializerState.ts:168

Close the block for the given node.

Parameters

ParameterTypeDescription
nodeNode_2The node whose block is being closed

Returns

void


ensureNewLine()

ts
ensureNewLine(): void;

Defined in: to-markdown/MarkdownSerializerState.ts:139

Ensure the current content ends with a newline.

Returns

void


esc()

ts
esc(str, startOfLine?): string;

Defined in: to-markdown/MarkdownSerializerState.ts:422

Escape the given string so that it can safely appear in Markdown content. If startOfLine is true, also escape characters that have special meaning only at the start of the line.

Parameters

ParameterTypeDefault valueDescription
strstringundefinedThe string to escape
startOfLinebooleanfalseWhether to also escape start-of-line special characters

Returns

string

The escaped string


flushClose()

ts
flushClose(size?): void;

Defined in: to-markdown/MarkdownSerializerState.ts:83

Flush a closed block, adding blank lines as needed.

Parameters

ParameterTypeDefault valueDescription
sizenumber2The number of blank lines to add (default: 2)

Returns

void


getEnclosingWhitespace()

ts
getEnclosingWhitespace(text): {
  leading?: string;
  trailing?: string;
};

Defined in: to-markdown/MarkdownSerializerState.ts:502

Get leading and trailing whitespace from a string. Values of leading or trailing property of the return object will be undefined if there is no match.

Parameters

ParameterTypeDescription
textstringThe text to extract whitespace from

Returns

ts
{
  leading?: string;
  trailing?: string;
}

An object containing the leading and trailing whitespace (if any)

NameTypeDefined in
leading?stringto-markdown/MarkdownSerializerState.ts:502
trailing?stringto-markdown/MarkdownSerializerState.ts:502

markString()

ts
markString(
   mark,
   open,
   parent,
   index): string;

Defined in: to-markdown/MarkdownSerializerState.ts:485

Get the markdown string for a given opening or closing mark.

Parameters

ParameterTypeDescription
markMarkThe mark to get the string for
openbooleanWhether to get the opening (true) or closing (false) string
parentNode_2The parent node containing the marked content
indexnumberThe index of the marked content within its parent

Returns

string

The markdown string for the mark


quote()

ts
quote(str): string;

Defined in: to-markdown/MarkdownSerializerState.ts:455

Wrap a string with appropriate quote characters. Chooses double quotes, single quotes, or parentheses based on the content.

Parameters

ParameterTypeDescription
strstringThe string to quote

Returns

string

The quoted string


renderContent()

ts
renderContent(parent): void;

Defined in: to-markdown/MarkdownSerializerState.ts:202

Render the contents of parent as block nodes.

Parameters

ParameterTypeDescription
parentNode_2The parent node whose children will be rendered

Returns

void


renderInline()

ts
renderInline(parent, fromBlockStart?): void;

Defined in: to-markdown/MarkdownSerializerState.ts:214

Render the contents of parent as inline content.

Parameters

ParameterTypeDefault valueDescription
parentNode_2undefinedThe parent node whose inline content will be rendered
fromBlockStartbooleantrueWhether rendering starts from the beginning of a block

Returns

void


renderList()

ts
renderList(
   node,
   delim,
   firstDelim): void;

Defined in: to-markdown/MarkdownSerializerState.ts:388

Render a node's content as a list. delim should be the extra indentation added to all lines except the first in an item, firstDelim is a function going from an item index to a delimiter for the first line of the item.

Parameters

ParameterTypeDescription
nodeNode_2The list node to render
delimstringThe delimiter/indentation for continuation lines
firstDelim(index) => stringA function that returns the delimiter for the first line of each item

Returns

void


repeat()

ts
repeat(str, n): string;

Defined in: to-markdown/MarkdownSerializerState.ts:472

Repeat the given string n times.

Parameters

ParameterTypeDescription
strstringThe string to repeat
nnumberThe number of times to repeat the string

Returns

string

The repeated string


text()

ts
text(text, escape?): void;

Defined in: to-markdown/MarkdownSerializerState.ts:179

Add the given text to the document. When escape is not false, it will be escaped.

Parameters

ParameterTypeDefault valueDescription
textstringundefinedThe text content to add
escapebooleantrueWhether to escape special Markdown characters (default: true)

Returns

void


wrapBlock()

ts
wrapBlock(
   delim,
   firstDelim,
   node,
   callbackFunc): void;

Defined in: to-markdown/MarkdownSerializerState.ts:115

Render a block, prefixing each line with delim, and the first line in firstDelim. node should be the node that is closed at the end of the block, and callbackFunc is a function that renders the content of the block.

Parameters

ParameterTypeDescription
delimstringThe delimiter to prefix to each line
firstDelimstringThe delimiter for the first line (uses delim if null)
nodeNode_2The node being wrapped
callbackFunc() => voidA function that renders the block content

Returns

void


write()

ts
write(content?): void;

Defined in: to-markdown/MarkdownSerializerState.ts:152

Prepare the state for writing output (closing closed paragraphs, adding delimiters, and so on), and then optionally add content (unescaped) to the output.

Parameters

ParameterTypeDescription
content?stringOptional content to add to the output (unescaped)

Returns

void