@lexical/markdown
Interfaces
MarkdownExportOptions
Defined in: packages/lexical-markdown/src/index.ts:87
Options for Markdown export.
Properties
punctuation?
optionalpunctuation?:PunctuationMode
Defined in: packages/lexical-markdown/src/index.ts:107
Which readers the emphasis markers are written for. Defaults to
'strict-commonmark', which writes a character reference where a marker
beside punctuation would otherwise not open or close (**你好。**世界),
so that every CommonMark, GFM and CJK-friendly reader reads it the same
way, but for a run of three or more markers that a pair uses only part
of (***a*b*c***, ***x****y*), which commonmark.js and markdown-it read differently from
micromark, and a selector after a tab, a line ending, a vertical tab or a
form feed that ends a run (**。\v︎**), which markdown-it-cjk-friendly
reads as that whitespace, leaving those markers literal, and a vertical
tab beside a marker (p\v**.**\vq), which cmark and cmark-gfm don't read
as whitespace and no reference can stand for. 'cjk-friendly' writes
such CJK text as typed (**你好。**世界), for readers that follow the
CJK-friendly amendment, such as import here.
shouldPreserveNewLines?
optionalshouldPreserveNewLines?:boolean
Defined in: packages/lexical-markdown/src/index.ts:91
Preserve new lines between conversions. Defaults to false.
MarkdownImportOptions
Defined in: packages/lexical-markdown/src/index.ts:65
Options for Markdown import.
Properties
punctuation?
optionalpunctuation?:PunctuationMode
Defined in: packages/lexical-markdown/src/index.ts:81
How emphasis markers beside punctuation are read. Defaults to
'cjk-friendly', which reads Markdown such as **你好。**世界 as its CJK
writer meant and everything else as CommonMark does.
shouldMergeAdjacentLines?
optionalshouldMergeAdjacentLines?:boolean
Defined in: packages/lexical-markdown/src/index.ts:75
Merge adjacent non-empty lines into one paragraph, as CommonMark does
(https://spec.commonmark.org/0.31.2/#paragraphs). Not applicable with
shouldPreserveNewLines. Defaults to false.
shouldPreserveNewLines?
optionalshouldPreserveNewLines?:boolean
Defined in: packages/lexical-markdown/src/index.ts:69
Preserve new lines between conversions. Defaults to false.
Type Aliases
ElementTransformer
ElementTransformer =
object
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:73
Properties
dependencies
dependencies:
Klass<LexicalNode>[]
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:74
export
export: (
node,traverseChildren,selection?,traverseBlocks?) =>string|null
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:81
export is called when the $convertToMarkdownString is called to convert the editor state into markdown.
Parameters
node
traverseChildren
(node) => string
selection?
BaseSelection | null
Optional selection to filter exported content. When provided, only selected content should be included.
traverseBlocks?
(node) => string
Returns
string | null
return null to cancel the export, even though the regex matched. Lexical will then search for the next transformer.
regExp
regExp:
RegExp
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:96
replace
replace: (
parentNode,children,match,isImport) =>boolean|void
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:102
replace is called when markdown is imported or typed in the editor
Parameters
parentNode
children
match
string[]
isImport
boolean
Returns
boolean | void
return false to cancel the transform, even though the regex matched. Lexical will then search for the next transformer.
triggerOnEnter?
optionaltriggerOnEnter?:boolean
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:117
When set, registerMarkdownShortcuts may run this transformer from KEY_ENTER_COMMAND
at end-of-line without requiring a trailing space (the update listener still uses the
space after the markdown token). Omit or false to disable Enter-triggered shortcuts.
type
type:
"element"
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:111
MultilineElementTransformer
MultilineElementTransformer =
object
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:120
Properties
dependencies
dependencies:
Klass<LexicalNode>[]
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:134
export?
optionalexport?: (node,traverseChildren,selection?,traverseBlocks?) =>string|null
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:141
export is called when the $convertToMarkdownString is called to convert the editor state into markdown.
Parameters
node
traverseChildren
(node) => string
selection?
BaseSelection | null
Optional selection to filter exported content. When provided, only selected content should be included.
traverseBlocks?
(node) => string
Returns
string | null
return null to cancel the export, even though the regex matched. Lexical will then search for the next transformer.
handleImportAfterStartMatch?
optionalhandleImportAfterStartMatch?: (args) => [boolean,number] |null|undefined
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:127
Use this function to manually handle the import process, once the regExpStart has matched successfully.
Without providing this function, the default behavior is to match until regExpEnd is found, or until the end of the document if regExpEnd.optional is true.
Parameters
args
lines
string[]
rootNode
startLineIndex
number
startMatch
transformer
Returns
[boolean, number] | null | undefined
a tuple or null. The first element of the returned tuple is a boolean indicating if a multiline element was imported. The second element is the index of the last line that was processed. If null is returned, the next multilineElementTransformer will be tried. If undefined is returned, the default behavior will be used.
regExpEnd?
optionalregExpEnd?:RegExp| {optional?:true;regExp:RegExp; }
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:161
This regex determines when to stop matching. Anything in between regExpStart and regExpEnd will be matched
Union Members
Type Literal
{ optional?: true; regExp: RegExp; }
optional?
optionaloptional?:true
Whether the end match is optional. If true, the end match is not required to match for the transformer to be triggered. The entire text from regexpStart to the end of the document will then be matched.
regExp
regExp:
RegExp
regExpStart
regExpStart:
RegExp
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:157
This regex determines when to start matching
replace
replace: (
rootNode,children,startMatch,endMatch,linesInBetween,isImport) =>boolean|void
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:176
replace is called only when markdown is imported in the editor, not when it's typed
Parameters
rootNode
children
LexicalNode[] | null
startMatch
string[]
endMatch
string[] | null
linesInBetween
string[] | null
isImport
boolean
Returns
boolean | void
return false to cancel the transform, even though the regex matched. Lexical will then search for the next transformer.
type
type:
"multiline-element"
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:195
PunctuationMode
PunctuationMode =
"cjk-friendly"|"strict-commonmark"
Defined in: packages/lexical-markdown/src/utils.ts:95
How emphasis markers read the punctuation beside them.
'strict-commonmark'follows CommonMark 0.31.2 and GFM: a marker between a letter and punctuation, such as the closing**of**你好。**世界, does not open or close, so CJK text needs a space or a character reference there.'cjk-friendly'follows the CommonMark CJK-friendly amendment (https://github.com/tats-u/markdown-cjk-friendly), asmarkdown-it-cjk-friendlyandmicromark-extension-cjk-friendlydo: it reads such a marker as CommonMark does everywhere except beside Chinese, Japanese and Korean characters and punctuation, where it opens and closes as a writer of those languages expects. A variation selector before a marker reads as the character it follows, but after whitespace as itself, asmicromark-extension-cjk-friendlyreads it, and after a U+0000 as the U+FFFD that readers put in its place, asmarkdown-it-cjk-friendlyreads it.
Both read a vertical tab beside a marker as whitespace, as commonmark.js, markdown-it and micromark do, though cmark and cmark-gfm don't.
TextFormatTransformer
TextFormatTransformer =
Readonly<{format: readonlyTextFormatType[];intraword?:boolean;tag:string;type:"text-format"; }>
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:198
TextMatchTransformer
TextMatchTransformer =
Readonly<{dependencies:Klass<LexicalNode>[];export?: (node,exportChildren,exportFormat) =>string|null;getEndIndex?: (node,match) =>number|false;importRegExp?:RegExp;regExp:RegExp;replace?: (node,match) =>void|TextNode;trigger?:string;type:"text-match"; }>
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:205
Transformer
Transformer =
ElementTransformer|MultilineElementTransformer|TextFormatTransformer|TextMatchTransformer
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:67
Variables
BOLD_ITALIC_STAR
constBOLD_ITALIC_STAR:TextFormatTransformer
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:1297
BOLD_ITALIC_UNDERSCORE
constBOLD_ITALIC_UNDERSCORE:TextFormatTransformer
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:1303
BOLD_STAR
constBOLD_STAR:TextFormatTransformer
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:1310
BOLD_UNDERSCORE
constBOLD_UNDERSCORE:TextFormatTransformer
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:1316
CHECK_LIST
constCHECK_LIST:ElementTransformer
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:1238
CODE
constCODE:MultilineElementTransformer
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:1033
ELEMENT_TRANSFORMERS
constELEMENT_TRANSFORMERS:ElementTransformer[]
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:1545
HEADING
constHEADING:ElementTransformer
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:844
HIGHLIGHT
constHIGHLIGHT:TextFormatTransformer
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:1291
INLINE_CODE
constINLINE_CODE:TextFormatTransformer
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:1264
ITALIC_STAR
constITALIC_STAR:TextFormatTransformer
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:1329
ITALIC_UNDERSCORE
constITALIC_UNDERSCORE:TextFormatTransformer
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:1335
LINK
constLINK:TextMatchTransformer
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:1426
MULTILINE_ELEMENT_TRANSFORMERS
constMULTILINE_ELEMENT_TRANSFORMERS:MultilineElementTransformer[]
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:1552
ORDERED_LIST
constORDERED_LIST:ElementTransformer
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:1251
QUOTE
constQUOTE:ElementTransformer
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:984
QUOTE_CONTAINER
constQUOTE_CONTAINER:ElementTransformer
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:1016
Experimental
QUOTE for an editor that registers QuoteContainerNode next to
QuoteNode: > makes a quote container, a quote that holds blocks. Put
it in place of QUOTE in the transformers; it exports both kinds of quote.
An editor that replaces QuoteNode with QuoteContainerNode
(QuoteContainerReplacementExtension) does not need it: there QUOTE
makes a container already.
Known limit: the importer reads a quote line by line, so it reads only
paragraphs into a container (lines that follow one another are one
paragraph, > alone starts the next), and any other block inside the
quote (> - item, > # title, a fenced code block) as a paragraph of
text. Export writes every block. To import the blocks of a quote, use
@lexical/mdast (MdastQuoteContainerExtension), which follows
CommonMark: a block quote holds any blocks.
STRIKETHROUGH
constSTRIKETHROUGH:TextFormatTransformer
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:1323
TEXT_FORMAT_TRANSFORMERS
constTEXT_FORMAT_TRANSFORMERS:TextFormatTransformer[]
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:1560
TEXT_MATCH_TRANSFORMERS
constTEXT_MATCH_TRANSFORMERS:TextMatchTransformer[]
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:1572
TRANSFORMERS
constTRANSFORMERS:Transformer[]
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:1588
UNORDERED_LIST
constUNORDERED_LIST:ElementTransformer
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:1225
Functions
$convertFromMarkdownString()
$convertFromMarkdownString(
markdown,transformers?,node?,shouldPreserveNewLines?,shouldMergeAdjacentLines?):void
Defined in: packages/lexical-markdown/src/index.ts:153
Renders markdown from a string. The selection is moved to the start after the operation.
Parameters
markdown
string
transformers?
Transformer[] = TRANSFORMERS
node?
shouldPreserveNewLines?
boolean | MarkdownImportOptions
By setting this to true, new lines will be preserved between conversions. MarkdownImportOptions can be passed here instead, in place of this; lines are merged when either it or shouldMergeAdjacentLines asks for that.
shouldMergeAdjacentLines?
boolean = false
By setting this to true, adjacent non empty lines will be merged according to commonmark spec: https://spec.commonmark.org/0.24/#example-177. Not applicable if shouldPreserveNewLines = true.
Returns
void
$convertSelectionToMarkdownString()
$convertSelectionToMarkdownString(
transformers?,selection,shouldPreserveNewLines?):string
Defined in: packages/lexical-markdown/src/index.ts:226
Converts the selected content to a markdown string.
Parameters
transformers?
Transformer[] = TRANSFORMERS
selection
BaseSelection | null
shouldPreserveNewLines?
boolean | MarkdownExportOptions
By setting this to true, new lines will be preserved between conversions; or MarkdownExportOptions.
Returns
string
$convertToMarkdownString()
$convertToMarkdownString(
transformers?,node?,shouldPreserveNewLines?):string
Defined in: packages/lexical-markdown/src/index.ts:210
Renders string from markdown. The selection is moved to the start after the operation.
Parameters
transformers?
Transformer[] = TRANSFORMERS
node?
shouldPreserveNewLines?
boolean | MarkdownExportOptions
By setting this to true, new lines will be preserved between conversions; or MarkdownExportOptions.
Returns
string
$generateNodesFromMarkdownString()
$generateNodesFromMarkdownString(
markdown,transformers?,shouldPreserveNewLines?,shouldMergeAdjacentLines?):LexicalNode[]
Defined in: packages/lexical-markdown/src/index.ts:181
Parses a markdown string and returns the resulting nodes as an array,
without modifying the document tree or selection. The returned nodes can be
inserted at an arbitrary position via selection.insertNodes().
Parameters
markdown
string
transformers?
Transformer[] = TRANSFORMERS
shouldPreserveNewLines?
boolean | MarkdownImportOptions
By setting this to true, new lines will be preserved between conversions. MarkdownImportOptions can be passed here instead, in place of this; lines are merged when either it or shouldMergeAdjacentLines asks for that.
shouldMergeAdjacentLines?
boolean = false
By setting this to true, adjacent non empty lines will be merged according to commonmark spec: https://spec.commonmark.org/0.24/#example-177. Not applicable if shouldPreserveNewLines = true.
Returns
isTableRowDivider()
isTableRowDivider(
line):boolean
Defined in: packages/lexical-markdown/src/MarkdownTransformers.ts:288
Whether line is a Markdown table delimiter row such as | --- | :--: |.
This is the linear-time equivalent of /^(\| ?:?-*:? ?)+\|\s?$/. That
pattern nests -* inside a (...)+ group, a shape that backtracking regexp
engines (e.g. Safari/JavaScriptCore) may run in super-linear time. A manual
scan is guaranteed O(n).
Parameters
line
string
Returns
boolean
registerMarkdownShortcuts()
registerMarkdownShortcuts(
editor,transformers?): () =>void
Defined in: packages/lexical-markdown/src/MarkdownShortcuts.ts:490
Parameters
editor
transformers?
Transformer[] = TRANSFORMERS
Returns
() => void