Compare commits

...

12 Commits

Author SHA1 Message Date
German Jablonski
c26867709a fix exports 2025-09-04 18:40:26 +01:00
German Jablonski
f15aad98c7 fix exports 2025-09-04 18:37:52 +01:00
German Jablonski
aa63e50fe4 fix exports 2025-09-04 18:34:47 +01:00
German Jablonski
966f61643f fix exports 2025-09-04 18:24:52 +01:00
German Jablonski
c08f2c56ea fix exports 2025-09-04 18:18:34 +01:00
German Jablonski
d318c00993 re-export TEXT_FORMAT_TRANSFORMERS instead of copying them 2025-09-04 18:12:59 +01:00
German Jablonski
552dc81076 re-export ELEMENT_TRANSFORMERS instead of copying them 2025-09-04 18:11:03 +01:00
German Jablonski
fa0a0bd8c2 BREAKING: 4th parameter of $convertFromMarkdownString is now false 2025-09-04 18:09:23 +01:00
German Jablonski
831b8fa362 re-export ElementTransformer instead of copying them 2025-09-04 17:20:00 +01:00
German Jablonski
9349fe68b0 re-export transformers types instead of copying them 2025-09-04 17:09:47 +01:00
German Jablonski
685e83764e re-export TextFormatTransformers instead of copying them 2025-09-04 17:03:25 +01:00
German Jablonski
a5b44ebc35 rename link converter and include it in TEXT_MATCH_TRANSFORMERS 2025-09-04 16:42:27 +01:00
16 changed files with 30 additions and 2421 deletions

View File

@@ -370,6 +370,7 @@
"@lexical/link": "0.34.0",
"@lexical/list": "0.34.0",
"@lexical/mark": "0.34.0",
"@lexical/markdown": "0.34.0",
"@lexical/react": "0.34.0",
"@lexical/rich-text": "0.34.0",
"@lexical/selection": "0.34.0",

View File

@@ -14,7 +14,7 @@ import { LinkIcon } from '../../../lexical/ui/icons/Link/index.js'
import { getSelectedNode } from '../../../lexical/utils/getSelectedNode.js'
import { createClientFeature } from '../../../utilities/createClientFeature.js'
import { toolbarFeatureButtonsGroupWithItems } from '../../shared/toolbar/featureButtonsGroup.js'
import { LinkMarkdownTransformer } from '../markdownTransformer.js'
import { PAYLOAD_LINK_TRANSFORMER } from '../markdownTransformer.js'
import { AutoLinkNode } from '../nodes/AutoLinkNode.js'
import { $isLinkNode, LinkNode, TOGGLE_LINK_COMMAND } from '../nodes/LinkNode.js'
import { AutoLinkPlugin } from './plugins/autoLink/index.js'
@@ -82,7 +82,7 @@ const toolbarGroups: ToolbarGroup[] = [
]
export const LinkFeatureClient = createClientFeature<ClientProps>(({ props }) => ({
markdownTransformers: [LinkMarkdownTransformer],
markdownTransformers: [PAYLOAD_LINK_TRANSFORMER],
nodes: [LinkNode, props?.disableAutoLinks === true ? null : AutoLinkNode].filter(
Boolean,
) as Array<Klass<LexicalNode>>,

View File

@@ -1,19 +1,22 @@
/**
* Code taken from https://github.com/facebook/lexical/blob/main/packages/lexical-markdown/src/MarkdownTransformers.ts#L357
* Code adapted from https://github.com/facebook/lexical/blob/main/packages/lexical-markdown/src/MarkdownTransformers.ts#L357
*/
// Order of text transformers matters:
//
// - code should go first as it prevents any transformations inside
import { $createTextNode, $isTextNode } from 'lexical'
import { $createTextNode } from 'lexical'
import type { TextMatchTransformer } from '../../packages/@lexical/markdown/MarkdownTransformers.js'
import { $createLinkNode, $isLinkNode, LinkNode } from './nodes/LinkNode.js'
// - then longer tags match (e.g. ** or __ should go before * or _)
export const LinkMarkdownTransformer: TextMatchTransformer = {
/**
* Unlike other transformers that use Lexicals defaults, Payload provides
* a custom transformer for links, since it relies on its own LinkNode.
*
* If youre working with internal links, youll need to customize the
* `export` and `replace` methods of this transformer, as Payload cannot
* infer the correct URL format on its own.
*/
export const PAYLOAD_LINK_TRANSFORMER: TextMatchTransformer = {
type: 'text-match',
dependencies: [LinkNode],
export: (_node, exportChildren) => {

View File

@@ -16,7 +16,7 @@ import type { ClientProps } from '../client/index.js'
import { createServerFeature } from '../../../utilities/createServerFeature.js'
import { convertLexicalNodesToHTML } from '../../converters/lexicalToHtml_deprecated/converter/index.js'
import { createNode } from '../../typeUtilities.js'
import { LinkMarkdownTransformer } from '../markdownTransformer.js'
import { PAYLOAD_LINK_TRANSFORMER } from '../markdownTransformer.js'
import { AutoLinkNode } from '../nodes/AutoLinkNode.js'
import { LinkNode } from '../nodes/LinkNode.js'
import { linkPopulationPromiseHOC } from './graphQLPopulationPromise.js'
@@ -158,7 +158,7 @@ export const LinkFeature = createServerFeature<
return schemaMap
},
i18n,
markdownTransformers: [LinkMarkdownTransformer],
markdownTransformers: [PAYLOAD_LINK_TRANSFORMER],
nodes: [
props?.disableAutoLinks === true
? null

View File

@@ -1 +1,7 @@
export * from '../packages/@lexical/markdown/index.js'
// https://stackoverflow.com/a/48953568
// https://stackoverflow.com/questions/48951687/override-an-export-from-another-library-es6
export { normalizeMarkdown } from '../packages/@lexical/markdown/MarkdownTransformers.js'
export * from '@lexical/markdown'

View File

@@ -1,21 +0,0 @@
MIT License
Copyright (c) Meta Platforms, Inc. and affiliates.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -1,331 +0,0 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import type { ElementNode, LexicalNode, TextFormatType, TextNode } from 'lexical'
import { $getRoot, $isDecoratorNode, $isElementNode, $isLineBreakNode, $isTextNode } from 'lexical'
import type {
ElementTransformer,
MultilineElementTransformer,
TextFormatTransformer,
TextMatchTransformer,
Transformer,
} from './MarkdownTransformers.js'
import { isEmptyParagraph, transformersByType } from './utils.js'
/**
* Renders string from markdown. The selection is moved to the start after the operation.
*/
export function createMarkdownExport(
transformers: Array<Transformer>,
shouldPreserveNewLines: boolean = false,
): (node?: ElementNode) => string {
const byType = transformersByType(transformers)
const elementTransformers = [...byType.multilineElement, ...byType.element]
const isNewlineDelimited = !shouldPreserveNewLines
// Export only uses text formats that are responsible for single format
// e.g. it will filter out *** (bold, italic) and instead use separate ** and *
const textFormatTransformers = byType.textFormat
.filter((transformer) => transformer.format.length === 1)
// Make sure all text transformers that contain 'code' in their format are at the end of the array. Otherwise, formatted code like
// <strong><code>code</code></strong> will be exported as `**Bold Code**`, as the code format will be applied first, and the bold format
// will be applied second and thus skipped entirely, as the code format will prevent any further formatting.
.sort((a, b) => {
if (a.format.includes('code') && !b.format.includes('code')) {
return 1
} else if (!a.format.includes('code') && b.format.includes('code')) {
return -1
} else {
return 0
}
})
return (node) => {
const output: string[] = []
const children = (node || $getRoot()).getChildren()
children.forEach((child, i) => {
const result = exportTopLevelElements(
child,
elementTransformers,
textFormatTransformers,
byType.textMatch,
)
if (result != null) {
output.push(
// separate consecutive group of texts with a line break: eg. ["hello", "world"] -> ["hello", "/nworld"]
isNewlineDelimited &&
i > 0 &&
!isEmptyParagraph(child) &&
!isEmptyParagraph(children[i - 1]!)
? '\n'.concat(result)
: result,
)
}
})
// Ensure consecutive groups of texts are at least \n\n apart while each empty paragraph render as a newline.
// Eg. ["hello", "", "", "hi", "\nworld"] -> "hello\n\n\nhi\n\nworld"
return output.join('\n')
}
}
function exportTopLevelElements(
node: LexicalNode,
elementTransformers: Array<ElementTransformer | MultilineElementTransformer>,
textTransformersIndex: Array<TextFormatTransformer>,
textMatchTransformers: Array<TextMatchTransformer>,
): null | string {
for (const transformer of elementTransformers) {
if (!transformer.export) {
continue
}
const result = transformer.export(node, (_node) =>
exportChildren(_node, textTransformersIndex, textMatchTransformers),
)
if (result != null) {
return result
}
}
if ($isElementNode(node)) {
return exportChildren(node, textTransformersIndex, textMatchTransformers)
} else if ($isDecoratorNode(node)) {
return node.getTextContent()
} else {
return null
}
}
function exportChildren(
node: ElementNode,
textTransformersIndex: Array<TextFormatTransformer>,
textMatchTransformers: Array<TextMatchTransformer>,
unclosedTags?: Array<{ format: TextFormatType; tag: string }>,
unclosableTags?: Array<{ format: TextFormatType; tag: string }>,
): string {
const output = []
const children = node.getChildren()
// keep track of unclosed tags from the very beginning
if (!unclosedTags) {
unclosedTags = []
}
if (!unclosableTags) {
unclosableTags = []
}
mainLoop: for (const child of children) {
for (const transformer of textMatchTransformers) {
if (!transformer.export) {
continue
}
const result = transformer.export(
child,
(parentNode) =>
exportChildren(
parentNode,
textTransformersIndex,
textMatchTransformers,
unclosedTags,
// Add current unclosed tags to the list of unclosable tags - we don't want nested tags from
// textmatch transformers to close the outer ones, as that may result in invalid markdown.
// E.g. **text [text**](https://lexical.io)
// is invalid markdown, as the closing ** is inside the link.
//
[...unclosableTags, ...unclosedTags],
),
(textNode, textContent) =>
exportTextFormat(
textNode,
textContent,
textTransformersIndex,
unclosedTags,
unclosableTags,
),
)
if (result != null) {
output.push(result)
continue mainLoop
}
}
if ($isLineBreakNode(child)) {
output.push('\n')
} else if ($isTextNode(child)) {
output.push(
exportTextFormat(
child,
child.getTextContent(),
textTransformersIndex,
unclosedTags,
unclosableTags,
),
)
} else if ($isElementNode(child)) {
// empty paragraph returns ""
output.push(
exportChildren(
child,
textTransformersIndex,
textMatchTransformers,
unclosedTags,
unclosableTags,
),
)
} else if ($isDecoratorNode(child)) {
output.push(child.getTextContent())
}
}
return output.join('')
}
function exportTextFormat(
node: TextNode,
textContent: string,
textTransformers: Array<TextFormatTransformer>,
// unclosed tags include the markdown tags that haven't been closed yet, and their associated formats
unclosedTags: Array<{ format: TextFormatType; tag: string }>,
unclosableTags?: Array<{ format: TextFormatType; tag: string }>,
): string {
// This function handles the case of a string looking like this: " foo "
// Where it would be invalid markdown to generate: "** foo **"
// We instead want to trim the whitespace out, apply formatting, and then
// bring the whitespace back. So our returned string looks like this: " **foo** "
const frozenString = textContent.trim()
let output = frozenString
if (!node.hasFormat('code')) {
// Escape any markdown characters in the text content
output = output.replace(/([*_`~\\])/g, '\\$1')
}
// the opening tags to be added to the result
let openingTags = ''
// the closing tags to be added to the result
let closingTagsBefore = ''
let closingTagsAfter = ''
const prevNode = getTextSibling(node, true)
const nextNode = getTextSibling(node, false)
const applied = new Set()
for (const transformer of textTransformers) {
const format = transformer.format[0]!
const tag = transformer.tag
// dedup applied formats
if (hasFormat(node, format) && !applied.has(format)) {
// Multiple tags might be used for the same format (*, _)
applied.add(format)
// append the tag to openningTags, if it's not applied to the previous nodes,
// or the nodes before that (which would result in an unclosed tag)
if (!hasFormat(prevNode, format) || !unclosedTags.find((element) => element.tag === tag)) {
unclosedTags.push({ format, tag })
openingTags += tag
}
}
}
// close any tags in the same order they were applied, if necessary
for (let i = 0; i < unclosedTags.length; i++) {
const unclosedTag = unclosedTags[i]!
const nodeHasFormat = hasFormat(node, unclosedTag.format)
const nextNodeHasFormat = hasFormat(nextNode, unclosedTag.format)
// prevent adding closing tag if next sibling will do it
if (nodeHasFormat && nextNodeHasFormat) {
continue
}
const unhandledUnclosedTags = [...unclosedTags] // Shallow copy to avoid modifying the original array
while (unhandledUnclosedTags.length > i) {
const unclosedTag = unhandledUnclosedTags.pop()
// If tag is unclosable, don't close it and leave it in the original array,
// So that it can be closed when it's no longer unclosable
if (
unclosableTags &&
unclosedTag &&
unclosableTags.find((element) => element.tag === unclosedTag.tag)
) {
continue
}
if (unclosedTag && typeof unclosedTag.tag === 'string') {
if (!nodeHasFormat) {
// Handles cases where the tag has not been closed before, e.g. if the previous node
// was a text match transformer that did not account for closing tags of the next node (e.g. a link)
closingTagsBefore += unclosedTag.tag
} else if (!nextNodeHasFormat) {
closingTagsAfter += unclosedTag.tag
}
}
// Mutate the original array to remove the closed tag
unclosedTags.pop()
}
break
}
output = openingTags + output + closingTagsAfter
// Replace trimmed version of textContent ensuring surrounding whitespace is not modified
return closingTagsBefore + textContent.replace(frozenString, () => output)
}
// Get next or previous text sibling a text node, including cases
// when it's a child of inline element (e.g. link)
function getTextSibling(node: TextNode, backward: boolean): null | TextNode {
let sibling = backward ? node.getPreviousSibling() : node.getNextSibling()
if (!sibling) {
const parent = node.getParentOrThrow()
if (parent.isInline()) {
sibling = backward ? parent.getPreviousSibling() : parent.getNextSibling()
}
}
while (sibling) {
if ($isElementNode(sibling)) {
if (!sibling.isInline()) {
break
}
const descendant = backward ? sibling.getLastDescendant() : sibling.getFirstDescendant()
if ($isTextNode(descendant)) {
return descendant
} else {
sibling = backward ? sibling.getPreviousSibling() : sibling.getNextSibling()
}
}
if ($isTextNode(sibling)) {
return sibling
}
if (!$isElementNode(sibling)) {
return null
}
}
return null
}
function hasFormat(node: LexicalNode | null | undefined, format: TextFormatType): boolean {
return $isTextNode(node) && node.hasFormat(format)
}

View File

@@ -1,279 +0,0 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import type { ListItemNode } from '@lexical/list'
import type { ElementNode } from 'lexical'
import { $isListItemNode, $isListNode } from '@lexical/list'
import { $isQuoteNode } from '@lexical/rich-text'
import { $findMatchingParent } from '@lexical/utils'
import {
$createLineBreakNode,
$createParagraphNode,
$createTextNode,
$getRoot,
$getSelection,
$isParagraphNode,
} from 'lexical'
import type {
ElementTransformer,
MultilineElementTransformer,
TextFormatTransformer,
TextMatchTransformer,
Transformer,
} from './MarkdownTransformers.js'
import { importTextTransformers } from './importTextTransformers.js'
import { isEmptyParagraph, transformersByType } from './utils.js'
export type TextFormatTransformersIndex = Readonly<{
fullMatchRegExpByTag: Readonly<Record<string, RegExp>>
openTagsRegExp: RegExp
transformersByTag: Readonly<Record<string, TextFormatTransformer>>
}>
/**
* Renders markdown from a string. The selection is moved to the start after the operation.
*/
export function createMarkdownImport(
transformers: Array<Transformer>,
shouldPreserveNewLines = false,
): (markdownString: string, node?: ElementNode) => void {
const byType = transformersByType(transformers)
const textFormatTransformersIndex = createTextFormatTransformersIndex(byType.textFormat)
return (markdownString, node) => {
const lines = markdownString.split('\n')
const linesLength = lines.length
const root = node || $getRoot()
root.clear()
for (let i = 0; i < linesLength; i++) {
const lineText = lines[i]!
const [imported, shiftedIndex] = $importMultiline(lines, i, byType.multilineElement, root)
if (imported) {
// If a multiline markdown element was imported, we don't want to process the lines that were part of it anymore.
// There could be other sub-markdown elements (both multiline and normal ones) matching within this matched multiline element's children.
// However, it would be the responsibility of the matched multiline transformer to decide how it wants to handle them.
// We cannot handle those, as there is no way for us to know how to maintain the correct order of generated lexical nodes for possible children.
i = shiftedIndex // Next loop will start from the line after the last line of the multiline element
continue
}
$importBlocks(lineText, root, byType.element, textFormatTransformersIndex, byType.textMatch)
}
// By default, removing empty paragraphs as md does not really
// allow empty lines and uses them as delimiter.
// If you need empty lines set shouldPreserveNewLines = true.
const children = root.getChildren()
for (const child of children) {
if (!shouldPreserveNewLines && isEmptyParagraph(child) && root.getChildrenSize() > 1) {
child.remove()
}
}
if ($getSelection() !== null) {
root.selectStart()
}
}
}
/**
*
* @returns 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.
*/
function $importMultiline(
lines: Array<string>,
startLineIndex: number,
multilineElementTransformers: Array<MultilineElementTransformer>,
rootNode: ElementNode,
): [boolean, number] {
for (const transformer of multilineElementTransformers) {
const { handleImportAfterStartMatch, regExpEnd, regExpStart, replace } = transformer
const startMatch = lines[startLineIndex]?.match(regExpStart)
if (!startMatch) {
continue // Try next transformer
}
if (handleImportAfterStartMatch) {
const result = handleImportAfterStartMatch({
lines,
rootNode,
startLineIndex,
startMatch,
transformer,
})
if (result === null) {
continue
} else if (result) {
return result
}
}
const regexpEndRegex: RegExp | undefined =
typeof regExpEnd === 'object' && 'regExp' in regExpEnd ? regExpEnd.regExp : regExpEnd
const isEndOptional =
regExpEnd && typeof regExpEnd === 'object' && 'optional' in regExpEnd
? regExpEnd.optional
: !regExpEnd
let endLineIndex = startLineIndex
const linesLength = lines.length
// check every single line for the closing match. It could also be on the same line as the opening match.
while (endLineIndex < linesLength) {
const endMatch = regexpEndRegex ? lines[endLineIndex]?.match(regexpEndRegex) : null
if (!endMatch) {
if (
!isEndOptional ||
(isEndOptional && endLineIndex < linesLength - 1) // Optional end, but didn't reach the end of the document yet => continue searching for potential closing match
) {
endLineIndex++
continue // Search next line for closing match
}
}
// Now, check if the closing match matched is the same as the opening match.
// If it is, we need to continue searching for the actual closing match.
if (endMatch && startLineIndex === endLineIndex && endMatch.index === startMatch.index) {
endLineIndex++
continue // Search next line for closing match
}
// At this point, we have found the closing match. Next: calculate the lines in between open and closing match
// This should not include the matches themselves, and be split up by lines
const linesInBetween: string[] = []
if (endMatch && startLineIndex === endLineIndex) {
linesInBetween.push(lines[startLineIndex]!.slice(startMatch[0].length, -endMatch[0].length))
} else {
for (let i = startLineIndex; i <= endLineIndex; i++) {
const line = lines[i]!
if (i === startLineIndex) {
const text = line.slice(startMatch[0].length)
linesInBetween.push(text) // Also include empty text
} else if (i === endLineIndex && endMatch) {
const text = line.slice(0, -endMatch[0].length)
linesInBetween.push(text) // Also include empty text
} else {
linesInBetween.push(line)
}
}
}
if (replace(rootNode, null, startMatch, endMatch!, linesInBetween, true) !== false) {
// Return here. This $importMultiline function is run line by line and should only process a single multiline element at a time.
return [true, endLineIndex]
}
// The replace function returned false, despite finding the matching open and close tags => this transformer does not want to handle it.
// Thus, we continue letting the remaining transformers handle the passed lines of text from the beginning
break
}
}
// No multiline transformer handled this line successfully
return [false, startLineIndex]
}
function $importBlocks(
lineText: string,
rootNode: ElementNode,
elementTransformers: Array<ElementTransformer>,
textFormatTransformersIndex: TextFormatTransformersIndex,
textMatchTransformers: Array<TextMatchTransformer>,
) {
const textNode = $createTextNode(lineText)
const elementNode = $createParagraphNode()
elementNode.append(textNode)
rootNode.append(elementNode)
for (const { regExp, replace } of elementTransformers) {
const match = lineText.match(regExp)
if (match) {
textNode.setTextContent(lineText.slice(match[0].length))
if (replace(elementNode, [textNode], match, true) !== false) {
break
}
}
}
importTextTransformers(textNode, textFormatTransformersIndex, textMatchTransformers)
// If no transformer found and we left with original paragraph node
// can check if its content can be appended to the previous node
// if it's a paragraph, quote or list
if (elementNode.isAttached() && lineText.length > 0) {
const previousNode = elementNode.getPreviousSibling()
if ($isParagraphNode(previousNode) || $isQuoteNode(previousNode) || $isListNode(previousNode)) {
let targetNode: ListItemNode | null | typeof previousNode = previousNode
if ($isListNode(previousNode)) {
const lastDescendant = previousNode.getLastDescendant()
if (lastDescendant == null) {
targetNode = null
} else {
targetNode = $findMatchingParent(lastDescendant, $isListItemNode)
}
}
if (targetNode != null && targetNode.getTextContentSize() > 0) {
targetNode.splice(targetNode.getChildrenSize(), 0, [
$createLineBreakNode(),
...elementNode.getChildren(),
])
elementNode.remove()
}
}
}
}
function createTextFormatTransformersIndex(
textTransformers: Array<TextFormatTransformer>,
): TextFormatTransformersIndex {
const transformersByTag: Record<string, TextFormatTransformer> = {}
const fullMatchRegExpByTag: Record<string, RegExp> = {}
const openTagsRegExp: string[] = []
const escapeRegExp = `(?<![\\\\])`
for (const transformer of textTransformers) {
const { tag } = transformer
transformersByTag[tag] = transformer
const tagRegExp = tag.replace(/([*^+])/g, '\\$1')
openTagsRegExp.push(tagRegExp)
// Single-char tag (e.g. "*"),
if (tag.length === 1) {
fullMatchRegExpByTag[tag] = new RegExp(
`(?<![\\\\${tagRegExp}])(${tagRegExp})((\\\\${tagRegExp})?.*?[^${tagRegExp}\\s](\\\\${tagRegExp})?)((?<!\\\\)|(?<=\\\\\\\\))(${tagRegExp})(?![\\\\${tagRegExp}])`,
)
} else {
// Multichar tags (e.g. "**")
fullMatchRegExpByTag[tag] = new RegExp(
`(?<!\\\\)(${tagRegExp})((\\\\${tagRegExp})?.*?[^\\s](\\\\${tagRegExp})?)((?<!\\\\)|(?<=\\\\\\\\))(${tagRegExp})(?!\\\\)`,
)
}
}
return {
// Reg exp to find open tag + content + close tag
fullMatchRegExpByTag,
// Regexp to locate *any* potential opening tag (longest first).
// eslint-disable-next-line regexp/no-useless-character-class, regexp/no-empty-capturing-group, regexp/no-empty-group
openTagsRegExp: new RegExp(`${escapeRegExp}(${openTagsRegExp.join('|')})`, 'g'),
transformersByTag,
}
}

View File

@@ -1,440 +0,0 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import type { ElementNode, LexicalEditor, TextNode } from 'lexical'
import {
$createRangeSelection,
$getSelection,
$isLineBreakNode,
$isRangeSelection,
$isRootOrShadowRoot,
$isTextNode,
$setSelection,
} from 'lexical'
import type {
ElementTransformer,
MultilineElementTransformer,
TextFormatTransformer,
TextMatchTransformer,
Transformer,
} from './MarkdownTransformers.js'
import { TRANSFORMERS } from './index.js'
import { indexBy, PUNCTUATION_OR_SPACE, transformersByType } from './utils.js'
function runElementTransformers(
parentNode: ElementNode,
anchorNode: TextNode,
anchorOffset: number,
elementTransformers: ReadonlyArray<ElementTransformer>,
): boolean {
const grandParentNode = parentNode.getParent()
if (!$isRootOrShadowRoot(grandParentNode) || parentNode.getFirstChild() !== anchorNode) {
return false
}
const textContent = anchorNode.getTextContent()
// Checking for anchorOffset position to prevent any checks for cases when caret is too far
// from a line start to be a part of block-level markdown trigger.
//
// TODO:
// Can have a quick check if caret is close enough to the beginning of the string (e.g. offset less than 10-20)
// since otherwise it won't be a markdown shortcut, but tables are exception
if (textContent[anchorOffset - 1] !== ' ') {
return false
}
for (const { regExp, replace } of elementTransformers) {
const match = textContent.match(regExp)
if (match && match[0].length === (match[0].endsWith(' ') ? anchorOffset : anchorOffset - 1)) {
const nextSiblings = anchorNode.getNextSiblings()
const [leadingNode, remainderNode] = anchorNode.splitText(anchorOffset)
leadingNode?.remove()
const siblings = remainderNode ? [remainderNode, ...nextSiblings] : nextSiblings
if (replace(parentNode, siblings, match, false) !== false) {
return true
}
}
}
return false
}
function runMultilineElementTransformers(
parentNode: ElementNode,
anchorNode: TextNode,
anchorOffset: number,
elementTransformers: ReadonlyArray<MultilineElementTransformer>,
): boolean {
const grandParentNode = parentNode.getParent()
if (!$isRootOrShadowRoot(grandParentNode) || parentNode.getFirstChild() !== anchorNode) {
return false
}
const textContent = anchorNode.getTextContent()
// Checking for anchorOffset position to prevent any checks for cases when caret is too far
// from a line start to be a part of block-level markdown trigger.
//
// TODO:
// Can have a quick check if caret is close enough to the beginning of the string (e.g. offset less than 10-20)
// since otherwise it won't be a markdown shortcut, but tables are exception
if (textContent[anchorOffset - 1] !== ' ') {
return false
}
for (const { regExpEnd, regExpStart, replace } of elementTransformers) {
if (
(regExpEnd && !('optional' in regExpEnd)) ||
(regExpEnd && 'optional' in regExpEnd && !regExpEnd.optional)
) {
continue
}
const match = textContent.match(regExpStart)
if (match && match[0].length === (match[0].endsWith(' ') ? anchorOffset : anchorOffset - 1)) {
const nextSiblings = anchorNode.getNextSiblings()
const [leadingNode, remainderNode] = anchorNode.splitText(anchorOffset)
leadingNode?.remove()
const siblings = remainderNode ? [remainderNode, ...nextSiblings] : nextSiblings
if (replace(parentNode, siblings, match, null, null, false) !== false) {
return true
}
}
}
return false
}
function runTextMatchTransformers(
anchorNode: TextNode,
anchorOffset: number,
transformersByTrigger: Readonly<Record<string, Array<TextMatchTransformer>>>,
): boolean {
let textContent = anchorNode.getTextContent()
const lastChar = textContent[anchorOffset - 1]!
const transformers = transformersByTrigger[lastChar]
if (transformers == null) {
return false
}
// If typing in the middle of content, remove the tail to do
// reg exp match up to a string end (caret position)
if (anchorOffset < textContent.length) {
textContent = textContent.slice(0, anchorOffset)
}
for (const transformer of transformers) {
if (!transformer.replace || !transformer.regExp) {
continue
}
const match = textContent.match(transformer.regExp)
if (match === null) {
continue
}
const startIndex = match.index || 0
const endIndex = startIndex + match[0].length
let replaceNode
if (startIndex === 0) {
;[replaceNode] = anchorNode.splitText(endIndex)
} else {
;[, replaceNode] = anchorNode.splitText(startIndex, endIndex)
}
if (replaceNode) {
replaceNode.selectNext(0, 0)
transformer.replace(replaceNode, match)
}
return true
}
return false
}
function $runTextFormatTransformers(
anchorNode: TextNode,
anchorOffset: number,
textFormatTransformers: Readonly<Record<string, ReadonlyArray<TextFormatTransformer>>>,
): boolean {
const textContent = anchorNode.getTextContent()
const closeTagEndIndex = anchorOffset - 1
const closeChar = textContent[closeTagEndIndex]!
// Quick check if we're possibly at the end of inline markdown style
const matchers = textFormatTransformers[closeChar]
if (!matchers) {
return false
}
for (const matcher of matchers) {
const { tag } = matcher
const tagLength = tag.length
const closeTagStartIndex = closeTagEndIndex - tagLength + 1
// If tag is not single char check if rest of it matches with text content
if (tagLength > 1) {
if (!isEqualSubString(textContent, closeTagStartIndex, tag, 0, tagLength)) {
continue
}
}
// Space before closing tag cancels inline markdown
if (textContent[closeTagStartIndex - 1] === ' ') {
continue
}
// Some tags can not be used within words, hence should have newline/space/punctuation after it
const afterCloseTagChar = textContent[closeTagEndIndex + 1]
if (
matcher.intraword === false &&
afterCloseTagChar &&
!PUNCTUATION_OR_SPACE.test(afterCloseTagChar)
) {
continue
}
const closeNode = anchorNode
let openNode = closeNode
let openTagStartIndex = getOpenTagStartIndex(textContent, closeTagStartIndex, tag)
// Go through text node siblings and search for opening tag
// if haven't found it within the same text node as closing tag
let sibling: null | TextNode = openNode
while (openTagStartIndex < 0 && (sibling = sibling.getPreviousSibling<TextNode>())) {
if ($isLineBreakNode(sibling)) {
break
}
if ($isTextNode(sibling)) {
const siblingTextContent = sibling.getTextContent()
openNode = sibling
openTagStartIndex = getOpenTagStartIndex(siblingTextContent, siblingTextContent.length, tag)
}
}
// Opening tag is not found
if (openTagStartIndex < 0) {
continue
}
// No content between opening and closing tag
if (openNode === closeNode && openTagStartIndex + tagLength === closeTagStartIndex) {
continue
}
// Checking longer tags for repeating chars (e.g. *** vs **)
const prevOpenNodeText = openNode.getTextContent()
if (openTagStartIndex > 0 && prevOpenNodeText[openTagStartIndex - 1] === closeChar) {
continue
}
// Some tags can not be used within words, hence should have newline/space/punctuation before it
const beforeOpenTagChar = prevOpenNodeText[openTagStartIndex - 1]
if (
matcher.intraword === false &&
beforeOpenTagChar &&
!PUNCTUATION_OR_SPACE.test(beforeOpenTagChar)
) {
continue
}
// Clean text from opening and closing tags (starting from closing tag
// to prevent any offset shifts if we start from opening one)
const prevCloseNodeText = closeNode.getTextContent()
const closeNodeText =
prevCloseNodeText.slice(0, closeTagStartIndex) + prevCloseNodeText.slice(closeTagEndIndex + 1)
closeNode.setTextContent(closeNodeText)
const openNodeText = openNode === closeNode ? closeNodeText : prevOpenNodeText
openNode.setTextContent(
openNodeText.slice(0, openTagStartIndex) + openNodeText.slice(openTagStartIndex + tagLength),
)
const selection = $getSelection()
const nextSelection = $createRangeSelection()
$setSelection(nextSelection)
// Adjust offset based on deleted chars
const newOffset = closeTagEndIndex - tagLength * (openNode === closeNode ? 2 : 1) + 1
nextSelection.anchor.set(openNode.__key, openTagStartIndex, 'text')
nextSelection.focus.set(closeNode.__key, newOffset, 'text')
// Apply formatting to selected text
for (const format of matcher.format) {
if (!nextSelection.hasFormat(format)) {
nextSelection.formatText(format)
}
}
// Collapse selection up to the focus point
nextSelection.anchor.set(
nextSelection.focus.key,
nextSelection.focus.offset,
nextSelection.focus.type,
)
// Remove formatting from collapsed selection
for (const format of matcher.format) {
if (nextSelection.hasFormat(format)) {
nextSelection.toggleFormat(format)
}
}
if ($isRangeSelection(selection)) {
nextSelection.format = selection.format
}
return true
}
return false
}
function getOpenTagStartIndex(string: string, maxIndex: number, tag: string): number {
const tagLength = tag.length
for (let i = maxIndex; i >= tagLength; i--) {
const startIndex = i - tagLength
if (
isEqualSubString(string, startIndex, tag, 0, tagLength) && // Space after opening tag cancels transformation
string[startIndex + tagLength] !== ' '
) {
return startIndex
}
}
return -1
}
function isEqualSubString(
stringA: string,
aStart: number,
stringB: string,
bStart: number,
length: number,
): boolean {
for (let i = 0; i < length; i++) {
if (stringA[aStart + i] !== stringB[bStart + i]) {
return false
}
}
return true
}
export function registerMarkdownShortcuts(
editor: LexicalEditor,
transformers: Array<Transformer> = TRANSFORMERS,
): () => void {
const byType = transformersByType(transformers)
const textFormatTransformersByTrigger = indexBy(
byType.textFormat,
({ tag }) => tag[tag.length - 1],
)
const textMatchTransformersByTrigger = indexBy(byType.textMatch, ({ trigger }) => trigger)
for (const transformer of transformers) {
const type = transformer.type
if (type === 'element' || type === 'text-match' || type === 'multiline-element') {
const dependencies = transformer.dependencies
for (const node of dependencies) {
if (!editor.hasNode(node)) {
throw new Error(
'MarkdownShortcuts: missing dependency %s for transformer. Ensure node dependency is included in editor initial config.' +
node.getType(),
)
}
}
}
}
const $transform = (parentNode: ElementNode, anchorNode: TextNode, anchorOffset: number) => {
if (runElementTransformers(parentNode, anchorNode, anchorOffset, byType.element)) {
return
}
if (
runMultilineElementTransformers(parentNode, anchorNode, anchorOffset, byType.multilineElement)
) {
return
}
if (runTextMatchTransformers(anchorNode, anchorOffset, textMatchTransformersByTrigger)) {
return
}
$runTextFormatTransformers(anchorNode, anchorOffset, textFormatTransformersByTrigger)
}
return editor.registerUpdateListener(({ dirtyLeaves, editorState, prevEditorState, tags }) => {
// Ignore updates from collaboration and undo/redo (as changes already calculated)
if (tags.has('collaboration') || tags.has('historic')) {
return
}
// If editor is still composing (i.e. backticks) we must wait before the user confirms the key
if (editor.isComposing()) {
return
}
const selection = editorState.read($getSelection)
const prevSelection = prevEditorState.read($getSelection)
// We expect selection to be a collapsed range and not match previous one (as we want
// to trigger transforms only as user types)
if (
!$isRangeSelection(prevSelection) ||
!$isRangeSelection(selection) ||
!selection.isCollapsed() ||
selection.is(prevSelection)
) {
return
}
const anchorKey = selection.anchor.key
const anchorOffset = selection.anchor.offset
const anchorNode = editorState._nodeMap.get(anchorKey)
if (
!$isTextNode(anchorNode) ||
!dirtyLeaves.has(anchorKey) ||
(anchorOffset !== 1 && anchorOffset > prevSelection.anchor.offset + 1)
) {
return
}
editor.update(() => {
// Markdown is not available inside code
if (anchorNode.hasFormat('code')) {
return
}
const parentNode = anchorNode.getParent()
if (parentNode === null) {
return
}
$transform(parentNode, anchorNode, selection.anchor.offset)
})
})
}

View File

@@ -1,189 +1,4 @@
/* eslint-disable regexp/no-unused-capturing-group */
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import type { ListType } from '@lexical/list'
import type { HeadingTagType } from '@lexical/rich-text'
import type { ElementNode, Klass, LexicalNode, TextFormatType, TextNode } from 'lexical'
import {
$createListItemNode,
$createListNode,
$isListItemNode,
$isListNode,
ListItemNode,
ListNode,
} from '@lexical/list'
import {
$createHeadingNode,
$createQuoteNode,
$isHeadingNode,
$isQuoteNode,
HeadingNode,
QuoteNode,
} from '@lexical/rich-text'
import { $createLineBreakNode } from 'lexical'
export type Transformer =
| ElementTransformer
| MultilineElementTransformer
| TextFormatTransformer
| TextMatchTransformer
export type ElementTransformer = {
dependencies: Array<Klass<LexicalNode>>
/**
* `export` is called when the `$convertToMarkdownString` is called to convert the editor state into markdown.
*
* @return return null to cancel the export, even though the regex matched. Lexical will then search for the next transformer.
*/
export: (
node: LexicalNode,
traverseChildren: (node: ElementNode) => string,
) => null | string
regExp: RegExp
/**
* `replace` is called when markdown is imported or typed in the editor
*
* @return return false to cancel the transform, even though the regex matched. Lexical will then search for the next transformer.
*/
replace: (
parentNode: ElementNode,
children: Array<LexicalNode>,
match: Array<string>,
/**
* Whether the match is from an import operation (e.g. through `$convertFromMarkdownString`) or not (e.g. through typing in the editor).
*/
isImport: boolean,
) => boolean | void
type: 'element'
}
export type MultilineElementTransformer = {
dependencies: Array<Klass<LexicalNode>>
/**
* `export` is called when the `$convertToMarkdownString` is called to convert the editor state into markdown.
*
* @return return null to cancel the export, even though the regex matched. Lexical will then search for the next transformer.
*/
export?: (
node: LexicalNode,
traverseChildren: (node: ElementNode) => string,
) => null | string
/**
* 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.
*
* @returns 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.
*/
handleImportAfterStartMatch?: (args: {
lines: Array<string>
rootNode: ElementNode
startLineIndex: number
startMatch: RegExpMatchArray
transformer: MultilineElementTransformer
}) => [boolean, number] | null | undefined
/**
* This regex determines when to stop matching. Anything in between regExpStart and regExpEnd will be matched
*/
regExpEnd?:
| {
/**
* 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.
*/
optional?: true
regExp: RegExp
}
| RegExp
/**
* This regex determines when to start matching
*/
regExpStart: RegExp
/**
* `replace` is called only when markdown is imported in the editor, not when it's typed
*
* @return return false to cancel the transform, even though the regex matched. Lexical will then search for the next transformer.
*/
replace: (
rootNode: ElementNode,
/**
* During markdown shortcut transforms, children nodes may be provided to the transformer. If this is the case, no `linesInBetween` will be provided and
* the children nodes should be used instead of the `linesInBetween` to create the new node.
*/
children: Array<LexicalNode> | null,
startMatch: Array<string>,
endMatch: Array<string> | null,
/**
* linesInBetween includes the text between the start & end matches, split up by lines, not including the matches themselves.
* This is null when the transformer is triggered through markdown shortcuts (by typing in the editor)
*/
linesInBetween: Array<string> | null,
/**
* Whether the match is from an import operation (e.g. through `$convertFromMarkdownString`) or not (e.g. through typing in the editor).
*/
isImport: boolean,
) => boolean | void
type: 'multiline-element'
}
export type TextFormatTransformer = Readonly<{
format: ReadonlyArray<TextFormatType>
intraword?: boolean
tag: string
type: 'text-format'
}>
export type TextMatchTransformer = Readonly<{
dependencies: Array<Klass<LexicalNode>>
/**
* Determines how a node should be exported to markdown
*/
export?: (
node: LexicalNode,
exportChildren: (node: ElementNode) => string,
exportFormat: (node: TextNode, textContent: string) => string,
) => null | string
/**
* For import operations, this function can be used to determine the end index of the match, after `importRegExp` has matched.
* Without this function, the end index will be determined by the length of the match from `importRegExp`. Manually determining the end index can be useful if
* the match from `importRegExp` is not the entire text content of the node. That way, `importRegExp` can be used to match only the start of the node, and `getEndIndex`
* can be used to match the end of the node.
*
* @returns The end index of the match, or false if the match was unsuccessful and a different transformer should be tried.
*/
getEndIndex?: (node: TextNode, match: RegExpMatchArray) => false | number
/**
* This regex determines what text is matched during markdown imports
*/
importRegExp?: RegExp
/**
* This regex determines what text is matched for markdown shortcuts while typing in the editor
*/
regExp: RegExp
/**
* Determines how the matched markdown text should be transformed into a node during the markdown import process
*
* @returns nothing, or a TextNode that may be a child of the new node that is created.
* If a TextNode is returned, text format matching will be applied to it (e.g. bold, italic, etc.)
*/
replace?: (node: TextNode, match: RegExpMatchArray) => TextNode | void
/**
* Single character that allows the transformer to trigger when typed in the editor. This does not affect markdown imports outside of the markdown shortcut plugin.
* If the trigger is matched, the `regExp` will be used to match the text in the second step.
*/
trigger?: string
type: 'text-match'
}>
const EMPTY_OR_WHITESPACE_ONLY = /^[\t ]*$/
const ORDERED_LIST_REGEX = /^(\s*)(\d+)\.\s/
@@ -199,243 +14,6 @@ const TABLE_ROW_DIVIDER_REG_EXP = /^(\| ?:?-*:? ?)+\|\s?$/
const TAG_START_REGEX = /^[ \t]*<[a-z_][\w-]*(?:\s[^<>]*)?\/?>/i
const TAG_END_REGEX = /^[ \t]*<\/[a-z_][\w-]*\s*>/i
const createBlockNode = (
createNode: (match: Array<string>) => ElementNode,
): ElementTransformer['replace'] => {
return (parentNode, children, match) => {
const node = createNode(match)
node.append(...children)
parentNode.replace(node)
node.select(0, 0)
}
}
// Amount of spaces that define indentation level
// TODO: should be an option
const LIST_INDENT_SIZE = 4
function getIndent(whitespaces: string): number {
const tabs = whitespaces.match(/\t/g)
const spaces = whitespaces.match(/ /g)
let indent = 0
if (tabs) {
indent += tabs.length
}
if (spaces) {
indent += Math.floor(spaces.length / LIST_INDENT_SIZE)
}
return indent
}
const listReplace = (listType: ListType): ElementTransformer['replace'] => {
return (parentNode, children, match) => {
const previousNode = parentNode.getPreviousSibling()
const nextNode = parentNode.getNextSibling()
const listItem = $createListItemNode(listType === 'check' ? match[3] === 'x' : undefined)
if ($isListNode(nextNode) && nextNode.getListType() === listType) {
const firstChild = nextNode.getFirstChild()
if (firstChild !== null) {
firstChild.insertBefore(listItem)
} else {
// should never happen, but let's handle gracefully, just in case.
nextNode.append(listItem)
}
parentNode.remove()
} else if ($isListNode(previousNode) && previousNode.getListType() === listType) {
previousNode.append(listItem)
parentNode.remove()
} else {
const list = $createListNode(listType, listType === 'number' ? Number(match[2]) : undefined)
list.append(listItem)
parentNode.replace(list)
}
listItem.append(...children)
listItem.select(0, 0)
const indent = getIndent(match[1]!)
if (indent) {
listItem.setIndent(indent)
}
}
}
const listExport = (
listNode: ListNode,
exportChildren: (node: ElementNode) => string,
depth: number,
): string => {
const output: string[] = []
const children = listNode.getChildren()
let index = 0
for (const listItemNode of children) {
if ($isListItemNode(listItemNode)) {
if (listItemNode.getChildrenSize() === 1) {
const firstChild = listItemNode.getFirstChild()
if ($isListNode(firstChild)) {
output.push(listExport(firstChild, exportChildren, depth + 1))
continue
}
}
const indent = ' '.repeat(depth * LIST_INDENT_SIZE)
const listType = listNode.getListType()
const prefix =
listType === 'number'
? `${listNode.getStart() + index}. `
: listType === 'check'
? `- [${listItemNode.getChecked() ? 'x' : ' '}] `
: '- '
output.push(indent + prefix + exportChildren(listItemNode))
index++
}
}
return output.join('\n')
}
export const HEADING: ElementTransformer = {
type: 'element',
dependencies: [HeadingNode],
export: (node, exportChildren) => {
if (!$isHeadingNode(node)) {
return null
}
const level = Number(node.getTag().slice(1))
return '#'.repeat(level) + ' ' + exportChildren(node)
},
regExp: HEADING_REGEX,
replace: createBlockNode((match) => {
const tag = ('h' + match[1]!.length) as HeadingTagType
return $createHeadingNode(tag)
}),
}
export const QUOTE: ElementTransformer = {
type: 'element',
dependencies: [QuoteNode],
export: (node, exportChildren) => {
if (!$isQuoteNode(node)) {
return null
}
const lines = exportChildren(node).split('\n')
const output: string[] = []
for (const line of lines) {
output.push('> ' + line)
}
return output.join('\n')
},
regExp: QUOTE_REGEX,
replace: (parentNode, children, _match, isImport) => {
if (isImport) {
const previousNode = parentNode.getPreviousSibling()
if ($isQuoteNode(previousNode)) {
previousNode.splice(previousNode.getChildrenSize(), 0, [
$createLineBreakNode(),
...children,
])
previousNode.select(0, 0)
parentNode.remove()
return
}
}
const node = $createQuoteNode()
node.append(...children)
parentNode.replace(node)
node.select(0, 0)
},
}
export const UNORDERED_LIST: ElementTransformer = {
type: 'element',
dependencies: [ListNode, ListItemNode],
export: (node, exportChildren) => {
return $isListNode(node) ? listExport(node, exportChildren, 0) : null
},
regExp: UNORDERED_LIST_REGEX,
replace: listReplace('bullet'),
}
export const CHECK_LIST: ElementTransformer = {
type: 'element',
dependencies: [ListNode, ListItemNode],
export: (node, exportChildren) => {
return $isListNode(node) ? listExport(node, exportChildren, 0) : null
},
regExp: CHECK_LIST_REGEX,
replace: listReplace('check'),
}
export const ORDERED_LIST: ElementTransformer = {
type: 'element',
dependencies: [ListNode, ListItemNode],
export: (node, exportChildren) => {
return $isListNode(node) ? listExport(node, exportChildren, 0) : null
},
regExp: ORDERED_LIST_REGEX,
replace: listReplace('number'),
}
export const INLINE_CODE: TextFormatTransformer = {
type: 'text-format',
format: ['code'],
tag: '`',
}
export const HIGHLIGHT: TextFormatTransformer = {
type: 'text-format',
format: ['highlight'],
tag: '==',
}
export const BOLD_ITALIC_STAR: TextFormatTransformer = {
type: 'text-format',
format: ['bold', 'italic'],
tag: '***',
}
export const BOLD_ITALIC_UNDERSCORE: TextFormatTransformer = {
type: 'text-format',
format: ['bold', 'italic'],
intraword: false,
tag: '___',
}
export const BOLD_STAR: TextFormatTransformer = {
type: 'text-format',
format: ['bold'],
tag: '**',
}
export const BOLD_UNDERSCORE: TextFormatTransformer = {
type: 'text-format',
format: ['bold'],
intraword: false,
tag: '__',
}
export const STRIKETHROUGH: TextFormatTransformer = {
type: 'text-format',
format: ['strikethrough'],
tag: '~~',
}
export const ITALIC_STAR: TextFormatTransformer = {
type: 'text-format',
format: ['italic'],
tag: '*',
}
export const ITALIC_UNDERSCORE: TextFormatTransformer = {
type: 'text-format',
format: ['italic'],
intraword: false,
tag: '_',
}
export function normalizeMarkdown(input: string, shouldMergeAdjacentLines: boolean): string {
const lines = input.split('\n')
let inCodeBlock = false

View File

@@ -1,143 +0,0 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import type { TextNode } from 'lexical'
import type { TextFormatTransformersIndex } from './MarkdownImport.js'
import type { TextFormatTransformer } from './MarkdownTransformers.js'
import { PUNCTUATION_OR_SPACE } from './utils.js'
export function findOutermostTextFormatTransformer(
textNode: TextNode,
textFormatTransformersIndex: TextFormatTransformersIndex,
): {
endIndex: number
match: RegExpMatchArray
startIndex: number
transformer: TextFormatTransformer
} | null {
const textContent = textNode.getTextContent()
const match = findOutermostMatch(textContent, textFormatTransformersIndex)
if (!match) {
return null
}
const textFormatMatchStart: number = match.index || 0
const textFormatMatchEnd = textFormatMatchStart + match[0].length
// @ts-expect-error - vestiges of when tsconfig was not strict. Feel free to improve
const transformer: TextFormatTransformer = textFormatTransformersIndex.transformersByTag[match[1]]
return {
endIndex: textFormatMatchEnd,
match,
startIndex: textFormatMatchStart,
transformer,
}
}
// Finds first "<tag>content<tag>" match that is not nested into another tag
function findOutermostMatch(
textContent: string,
textTransformersIndex: TextFormatTransformersIndex,
): null | RegExpMatchArray {
const openTagsMatch = textContent.match(textTransformersIndex.openTagsRegExp)
if (openTagsMatch == null) {
return null
}
for (const match of openTagsMatch) {
// Open tags reg exp might capture leading space so removing it
// before using match to find transformer
const tag = match.replace(/^\s/, '')
const fullMatchRegExp = textTransformersIndex.fullMatchRegExpByTag[tag]
if (fullMatchRegExp == null) {
continue
}
const fullMatch = textContent.match(fullMatchRegExp)
const transformer = textTransformersIndex.transformersByTag[tag]
if (fullMatch != null && transformer != null) {
if (transformer.intraword !== false) {
return fullMatch
}
// For non-intraword transformers checking if it's within a word
// or surrounded with space/punctuation/newline
const { index = 0 } = fullMatch
const beforeChar = textContent[index - 1]
const afterChar = textContent[index + fullMatch[0].length]
if (
(!beforeChar || PUNCTUATION_OR_SPACE.test(beforeChar)) &&
(!afterChar || PUNCTUATION_OR_SPACE.test(afterChar))
) {
return fullMatch
}
}
}
return null
}
export function importTextFormatTransformer(
textNode: TextNode,
startIndex: number,
endIndex: number,
transformer: TextFormatTransformer,
match: RegExpMatchArray,
): {
nodeAfter: TextNode | undefined // If split
nodeBefore: TextNode | undefined // If split
transformedNode: TextNode
} {
const textContent = textNode.getTextContent()
// No text matches - we can safely process the text format match
let nodeAfter: TextNode | undefined
let nodeBefore: TextNode | undefined
let transformedNode: TextNode
// If matching full content there's no need to run splitText and can reuse existing textNode
// to update its content and apply format. E.g. for **_Hello_** string after applying bold
// format (**) it will reuse the same text node to apply italic (_)
if (match[0] === textContent) {
transformedNode = textNode
} else {
if (startIndex === 0) {
;[transformedNode, nodeAfter] = textNode.splitText(endIndex) as [
TextNode,
TextNode | undefined,
]
} else {
;[nodeBefore, transformedNode, nodeAfter] = textNode.splitText(startIndex, endIndex) as [
TextNode,
TextNode,
TextNode | undefined,
]
}
}
transformedNode.setTextContent(match[2]!)
if (transformer) {
for (const format of transformer.format) {
if (!transformedNode.hasFormat(format)) {
transformedNode.toggleFormat(format)
}
}
}
return {
nodeAfter,
nodeBefore,
transformedNode,
}
}

View File

@@ -1,107 +0,0 @@
import { type TextNode } from 'lexical'
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import type { TextMatchTransformer } from './MarkdownTransformers.js'
export function findOutermostTextMatchTransformer(
textNode_: TextNode,
textMatchTransformers: Array<TextMatchTransformer>,
): {
endIndex: number
match: RegExpMatchArray
startIndex: number
transformer: TextMatchTransformer
} | null {
const textNode = textNode_
let foundMatchStartIndex: number | undefined = undefined
let foundMatchEndIndex: number | undefined = undefined
let foundMatchTransformer: TextMatchTransformer | undefined = undefined
let foundMatch: RegExpMatchArray | undefined = undefined
for (const transformer of textMatchTransformers) {
if (!transformer.replace || !transformer.importRegExp) {
continue
}
const match = textNode.getTextContent().match(transformer.importRegExp)
if (!match) {
continue
}
const startIndex = match.index || 0
const endIndex = transformer.getEndIndex
? transformer.getEndIndex(textNode, match)
: startIndex + match[0].length
if (endIndex === false) {
continue
}
if (
foundMatchStartIndex === undefined ||
foundMatchEndIndex === undefined ||
(startIndex < foundMatchStartIndex && endIndex > foundMatchEndIndex)
) {
foundMatchStartIndex = startIndex
foundMatchEndIndex = endIndex
foundMatchTransformer = transformer
foundMatch = match
}
}
if (
foundMatchStartIndex === undefined ||
foundMatchEndIndex === undefined ||
foundMatchTransformer === undefined ||
foundMatch === undefined
) {
return null
}
return {
endIndex: foundMatchEndIndex,
match: foundMatch,
startIndex: foundMatchStartIndex,
transformer: foundMatchTransformer,
}
}
export function importFoundTextMatchTransformer(
textNode: TextNode,
startIndex: number,
endIndex: number,
transformer: TextMatchTransformer,
match: RegExpMatchArray,
): {
nodeAfter: TextNode | undefined // If split
nodeBefore: TextNode | undefined // If split
transformedNode?: TextNode
} | null {
let nodeAfter, nodeBefore, transformedNode
if (startIndex === 0) {
;[transformedNode, nodeAfter] = textNode.splitText(endIndex)
} else {
;[nodeBefore, transformedNode, nodeAfter] = textNode.splitText(startIndex, endIndex)
}
if (!transformer.replace) {
return null
}
const potentialTransformedNode = transformedNode
? transformer.replace(transformedNode, match)
: undefined
return {
nodeAfter,
nodeBefore,
transformedNode: potentialTransformedNode || undefined,
}
}

View File

@@ -1,119 +0,0 @@
import { $isTextNode, type TextNode } from 'lexical'
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import type { TextFormatTransformersIndex } from './MarkdownImport.js'
import type { TextMatchTransformer } from './MarkdownTransformers.js'
import {
findOutermostTextFormatTransformer,
importTextFormatTransformer,
} from './importTextFormatTransformer.js'
import {
findOutermostTextMatchTransformer,
importFoundTextMatchTransformer,
} from './importTextMatchTransformer.js'
/**
* Handles applying both text format and text match transformers.
* It finds the outermost text format or text match and applies it,
* then recursively calls itself to apply the next outermost transformer,
* until there are no more transformers to apply.
*/
export function importTextTransformers(
textNode: TextNode,
textFormatTransformersIndex: TextFormatTransformersIndex,
textMatchTransformers: Array<TextMatchTransformer>,
) {
let foundTextFormat = findOutermostTextFormatTransformer(textNode, textFormatTransformersIndex)
let foundTextMatch = findOutermostTextMatchTransformer(textNode, textMatchTransformers)
if (foundTextFormat && foundTextMatch) {
// Find the outermost transformer
if (
foundTextFormat.startIndex <= foundTextMatch.startIndex &&
foundTextFormat.endIndex >= foundTextMatch.endIndex
) {
// foundTextFormat wraps foundTextMatch - apply foundTextFormat by setting foundTextMatch to null
foundTextMatch = null
} else {
// foundTextMatch wraps foundTextFormat - apply foundTextMatch by setting foundTextFormat to null
foundTextFormat = null
}
}
if (foundTextFormat) {
const result = importTextFormatTransformer(
textNode,
foundTextFormat.startIndex,
foundTextFormat.endIndex,
foundTextFormat.transformer,
foundTextFormat.match,
)
if (result.nodeAfter && $isTextNode(result.nodeAfter) && !result.nodeAfter.hasFormat('code')) {
importTextTransformers(result.nodeAfter, textFormatTransformersIndex, textMatchTransformers)
}
if (
result.nodeBefore &&
$isTextNode(result.nodeBefore) &&
!result.nodeBefore.hasFormat('code')
) {
importTextTransformers(result.nodeBefore, textFormatTransformersIndex, textMatchTransformers)
}
if (
result.transformedNode &&
$isTextNode(result.transformedNode) &&
!result.transformedNode.hasFormat('code')
) {
importTextTransformers(
result.transformedNode,
textFormatTransformersIndex,
textMatchTransformers,
)
}
} else if (foundTextMatch) {
const result = importFoundTextMatchTransformer(
textNode,
foundTextMatch.startIndex,
foundTextMatch.endIndex,
foundTextMatch.transformer,
foundTextMatch.match,
)
if (!result) {
return
}
if (result.nodeAfter && $isTextNode(result.nodeAfter) && !result.nodeAfter.hasFormat('code')) {
importTextTransformers(result.nodeAfter, textFormatTransformersIndex, textMatchTransformers)
}
if (
result.nodeBefore &&
$isTextNode(result.nodeBefore) &&
!result.nodeBefore.hasFormat('code')
) {
importTextTransformers(result.nodeBefore, textFormatTransformersIndex, textMatchTransformers)
}
if (
result.transformedNode &&
$isTextNode(result.transformedNode) &&
!result.transformedNode.hasFormat('code')
) {
importTextTransformers(
result.transformedNode,
textFormatTransformersIndex,
textMatchTransformers,
)
}
}
// Handle escape characters
const textContent = textNode.getTextContent()
const escapedText = textContent.replace(/\\([*_`~])/g, '$1')
textNode.setTextContent(escapedText)
}

View File

@@ -1,68 +1,16 @@
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import type { ElementNode } from 'lexical'
import type {
ElementTransformer,
MultilineElementTransformer,
TextFormatTransformer,
TextMatchTransformer,
Transformer,
} from './MarkdownTransformers.js'
} from '@lexical/markdown'
import { createMarkdownExport } from './MarkdownExport.js'
import { createMarkdownImport } from './MarkdownImport.js'
import { registerMarkdownShortcuts } from './MarkdownShortcuts.js'
import {
BOLD_ITALIC_STAR,
BOLD_ITALIC_UNDERSCORE,
BOLD_STAR,
BOLD_UNDERSCORE,
CHECK_LIST,
HEADING,
HIGHLIGHT,
INLINE_CODE,
ITALIC_STAR,
ITALIC_UNDERSCORE,
normalizeMarkdown,
ORDERED_LIST,
QUOTE,
STRIKETHROUGH,
UNORDERED_LIST,
} from './MarkdownTransformers.js'
import { ELEMENT_TRANSFORMERS, TEXT_FORMAT_TRANSFORMERS } from '@lexical/markdown'
const ELEMENT_TRANSFORMERS: Array<ElementTransformer> = [
HEADING,
QUOTE,
UNORDERED_LIST,
ORDERED_LIST,
]
import { PAYLOAD_LINK_TRANSFORMER } from '../../../features/link/markdownTransformer.js'
const MULTILINE_ELEMENT_TRANSFORMERS: Array<MultilineElementTransformer> = []
// Order of text format transformers matters:
//
// - code should go first as it prevents any transformations inside
// - then longer tags match (e.g. ** or __ should go before * or _)
const TEXT_FORMAT_TRANSFORMERS: Array<TextFormatTransformer> = [
INLINE_CODE,
BOLD_ITALIC_STAR,
BOLD_ITALIC_UNDERSCORE,
BOLD_STAR,
BOLD_UNDERSCORE,
HIGHLIGHT,
ITALIC_STAR,
ITALIC_UNDERSCORE,
STRIKETHROUGH,
]
const TEXT_MATCH_TRANSFORMERS: Array<TextMatchTransformer> = []
const TEXT_MATCH_TRANSFORMERS: Array<TextMatchTransformer> = [PAYLOAD_LINK_TRANSFORMER]
const TRANSFORMERS: Array<Transformer> = [
...ELEMENT_TRANSFORMERS,
@@ -71,64 +19,4 @@ const TRANSFORMERS: Array<Transformer> = [
...TEXT_MATCH_TRANSFORMERS,
]
/**
* Renders markdown from a string. The selection is moved to the start after the operation.
*
* @param {boolean} [shouldPreserveNewLines] By setting this to true, new lines will be preserved between conversions
* @param {boolean} [shouldMergeAdjacentLines] 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.
*/
function $convertFromMarkdownString(
markdown: string,
transformers: Array<Transformer> = TRANSFORMERS,
node?: ElementNode,
shouldPreserveNewLines = false,
shouldMergeAdjacentLines = true,
): void {
const sanitizedMarkdown = shouldPreserveNewLines
? markdown
: normalizeMarkdown(markdown, shouldMergeAdjacentLines)
const importMarkdown = createMarkdownImport(transformers, shouldPreserveNewLines)
return importMarkdown(sanitizedMarkdown, node)
}
/**
* Renders string from markdown. The selection is moved to the start after the operation.
*/
function $convertToMarkdownString(
transformers: Array<Transformer> = TRANSFORMERS,
node?: ElementNode,
shouldPreserveNewLines: boolean = false,
): string {
const exportMarkdown = createMarkdownExport(transformers, shouldPreserveNewLines)
return exportMarkdown(node)
}
export {
$convertFromMarkdownString,
$convertToMarkdownString,
BOLD_ITALIC_STAR,
BOLD_ITALIC_UNDERSCORE,
BOLD_STAR,
BOLD_UNDERSCORE,
CHECK_LIST,
ELEMENT_TRANSFORMERS,
type ElementTransformer,
HEADING,
HIGHLIGHT,
INLINE_CODE,
ITALIC_STAR,
ITALIC_UNDERSCORE,
MULTILINE_ELEMENT_TRANSFORMERS,
type MultilineElementTransformer,
ORDERED_LIST,
QUOTE,
registerMarkdownShortcuts,
STRIKETHROUGH,
TEXT_FORMAT_TRANSFORMERS,
TEXT_MATCH_TRANSFORMERS,
type TextFormatTransformer,
type TextMatchTransformer,
type Transformer,
TRANSFORMERS,
UNORDERED_LIST,
}
export { MULTILINE_ELEMENT_TRANSFORMERS, TEXT_MATCH_TRANSFORMERS, TRANSFORMERS }

View File

@@ -1,430 +0,0 @@
/* eslint-disable regexp/no-obscure-range */
/* eslint-disable regexp/no-empty-group */
/* eslint-disable regexp/no-empty-capturing-group */
/* eslint-disable regexp/optimal-quantifier-concatenation */
/* eslint-disable regexp/no-misleading-capturing-group */
/* eslint-disable regexp/no-contradiction-with-assertion */
/* eslint-disable regexp/no-super-linear-backtracking */
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import type { ListNode } from '@lexical/list'
import { $isListItemNode, $isListNode } from '@lexical/list'
import { $isHeadingNode, $isQuoteNode } from '@lexical/rich-text'
import {
$isParagraphNode,
$isTextNode,
type ElementNode,
type LexicalNode,
type TextFormatType,
} from 'lexical'
import type {
ElementTransformer,
MultilineElementTransformer,
TextFormatTransformer,
TextMatchTransformer,
Transformer,
} from './MarkdownTransformers.js'
type MarkdownFormatKind =
| 'bold'
| 'code'
| 'horizontalRule'
| 'italic'
| 'italic_bold'
| 'link'
| 'noTransformation'
| 'paragraphBlockQuote'
| 'paragraphCodeBlock'
| 'paragraphH1'
| 'paragraphH2'
| 'paragraphH3'
| 'paragraphH4'
| 'paragraphH5'
| 'paragraphH6'
| 'paragraphOrderedList'
| 'paragraphUnorderedList'
| 'strikethrough'
| 'strikethrough_bold'
| 'strikethrough_italic'
| 'strikethrough_italic_bold'
| 'underline'
type MarkdownCriteria = Readonly<{
export?: (
node: LexicalNode,
traverseChildren: (elementNode: ElementNode) => string,
) => null | string
exportFormat?: TextFormatType
exportTag?: string
exportTagClose?: string
markdownFormatKind: MarkdownFormatKind | null | undefined
regEx: RegExp
regExForAutoFormatting: RegExp
requiresParagraphStart: boolean | null | undefined
}>
type MarkdownCriteriaArray = Array<MarkdownCriteria>
const autoFormatBase: MarkdownCriteria = {
markdownFormatKind: null,
regEx: /(?:)/,
regExForAutoFormatting: /(?:)/,
requiresParagraphStart: false,
}
const paragraphStartBase: MarkdownCriteria = {
...autoFormatBase,
requiresParagraphStart: true,
}
const markdownHeader1: MarkdownCriteria = {
...paragraphStartBase,
export: createHeadingExport(1),
markdownFormatKind: 'paragraphH1',
regEx: /^# /,
regExForAutoFormatting: /^# /,
}
const markdownHeader2: MarkdownCriteria = {
...paragraphStartBase,
export: createHeadingExport(2),
markdownFormatKind: 'paragraphH2',
regEx: /^## /,
regExForAutoFormatting: /^## /,
}
const markdownHeader3: MarkdownCriteria = {
...paragraphStartBase,
export: createHeadingExport(3),
markdownFormatKind: 'paragraphH3',
regEx: /^### /,
regExForAutoFormatting: /^### /,
}
const markdownHeader4: MarkdownCriteria = {
...paragraphStartBase,
export: createHeadingExport(4),
markdownFormatKind: 'paragraphH4',
regEx: /^#### /,
regExForAutoFormatting: /^#### /,
}
const markdownHeader5: MarkdownCriteria = {
...paragraphStartBase,
export: createHeadingExport(5),
markdownFormatKind: 'paragraphH5',
regEx: /^##### /,
regExForAutoFormatting: /^##### /,
}
const markdownHeader6: MarkdownCriteria = {
...paragraphStartBase,
export: createHeadingExport(6),
markdownFormatKind: 'paragraphH6',
regEx: /^###### /,
regExForAutoFormatting: /^###### /,
}
const markdownBlockQuote: MarkdownCriteria = {
...paragraphStartBase,
export: blockQuoteExport,
markdownFormatKind: 'paragraphBlockQuote',
regEx: /^> /,
regExForAutoFormatting: /^> /,
}
const markdownUnorderedListDash: MarkdownCriteria = {
...paragraphStartBase,
export: listExport,
markdownFormatKind: 'paragraphUnorderedList',
regEx: /^(\s{0,10})- /,
regExForAutoFormatting: /^(\s{0,10})- /,
}
const markdownUnorderedListAsterisk: MarkdownCriteria = {
...paragraphStartBase,
export: listExport,
markdownFormatKind: 'paragraphUnorderedList',
regEx: /^(\s{0,10})\* /,
regExForAutoFormatting: /^(\s{0,10})\* /,
}
const markdownOrderedList: MarkdownCriteria = {
...paragraphStartBase,
export: listExport,
markdownFormatKind: 'paragraphOrderedList',
regEx: /^(\s{0,10})(\d+)\.\s/,
regExForAutoFormatting: /^(\s{0,10})(\d+)\.\s/,
}
const markdownHorizontalRule: MarkdownCriteria = {
...paragraphStartBase,
markdownFormatKind: 'horizontalRule',
regEx: /^\*\*\*$/,
regExForAutoFormatting: /^\*\*\* /,
}
const markdownHorizontalRuleUsingDashes: MarkdownCriteria = {
...paragraphStartBase,
markdownFormatKind: 'horizontalRule',
regEx: /^---$/,
regExForAutoFormatting: /^--- /,
}
const markdownInlineCode: MarkdownCriteria = {
...autoFormatBase,
exportFormat: 'code',
exportTag: '`',
markdownFormatKind: 'code',
regEx: /(`)(\s*)([^`]*)(\s*)(`)()/,
regExForAutoFormatting: /(`)(\s*\b)([^`]*)(\b\s*)(`)(\s)$/,
}
const markdownBold: MarkdownCriteria = {
...autoFormatBase,
exportFormat: 'bold',
exportTag: '**',
markdownFormatKind: 'bold',
regEx: /(\*\*)(\s*)([^*]*)(\s*)(\*\*)()/,
regExForAutoFormatting: /(\*\*)(\s*\b)([^*]*)(\b\s*)(\*\*)(\s)$/,
}
const markdownItalic: MarkdownCriteria = {
...autoFormatBase,
exportFormat: 'italic',
exportTag: '*',
markdownFormatKind: 'italic',
regEx: /(\*)(\s*)([^*]*)(\s*)(\*)()/,
regExForAutoFormatting: /(\*)(\s*\b)([^*]*)(\b\s*)(\*)(\s)$/,
}
const markdownBold2: MarkdownCriteria = {
...autoFormatBase,
exportFormat: 'bold',
exportTag: '_',
markdownFormatKind: 'bold',
regEx: /(__)(\s*)([^_]*)(\s*)(__)()/,
regExForAutoFormatting: /(__)(\s*)([^_]*)(\s*)(__)(\s)$/,
}
const markdownItalic2: MarkdownCriteria = {
...autoFormatBase,
exportFormat: 'italic',
exportTag: '_',
markdownFormatKind: 'italic',
regEx: /(_)()([^_]*)()(_)()/,
regExForAutoFormatting: /(_)()([^_]*)()(_)(\s)$/, // Maintain 7 groups.
}
const fakeMarkdownUnderline: MarkdownCriteria = {
...autoFormatBase,
exportFormat: 'underline',
exportTag: '<u>',
exportTagClose: '</u>',
markdownFormatKind: 'underline',
regEx: /(<u>)(\s*)([^<]*)(\s*)(<\/u>)()/,
regExForAutoFormatting: /(<u>)(\s*\b)([^<]*)(\b\s*)(<\/u>)(\s)$/,
}
const markdownStrikethrough: MarkdownCriteria = {
...autoFormatBase,
exportFormat: 'strikethrough',
exportTag: '~~',
markdownFormatKind: 'strikethrough',
regEx: /(~~)(\s*)([^~]*)(\s*)(~~)()/,
regExForAutoFormatting: /(~~)(\s*\b)([^~]*)(\b\s*)(~~)(\s)$/,
}
const markdownStrikethroughItalicBold: MarkdownCriteria = {
...autoFormatBase,
markdownFormatKind: 'strikethrough_italic_bold',
regEx: /(~~_\*\*)(\s*\b)([^*_~]+)(\b\s*)(\*\*_~~)()/,
regExForAutoFormatting: /(~~_\*\*)(\s*\b)([^*_~]+)(\b\s*)(\*\*_~~)(\s)$/,
}
const markdownItalicbold: MarkdownCriteria = {
...autoFormatBase,
markdownFormatKind: 'italic_bold',
regEx: /(_\*\*)(\s*\b)([^*_]+)(\b\s*)(\*\*_)/,
regExForAutoFormatting: /(_\*\*)(\s*\b)([^*_]+)(\b\s*)(\*\*_)(\s)$/,
}
const markdownStrikethroughItalic: MarkdownCriteria = {
...autoFormatBase,
markdownFormatKind: 'strikethrough_italic',
regEx: /(~~_)(\s*)([^_~]+)(\s*)(_~~)/,
regExForAutoFormatting: /(~~_)(\s*)([^_~]+)(\s*)(_~~)(\s)$/,
}
const markdownStrikethroughBold: MarkdownCriteria = {
...autoFormatBase,
markdownFormatKind: 'strikethrough_bold',
regEx: /(~~\*\*)(\s*\b)([^*~]+)(\b\s*)(\*\*~~)/,
regExForAutoFormatting: /(~~\*\*)(\s*\b)([^*~]+)(\b\s*)(\*\*~~)(\s)$/,
}
const markdownLink: MarkdownCriteria = {
...autoFormatBase,
markdownFormatKind: 'link',
regEx: /(\[)([^\]]*)(\]\()([^)]*)(\)*)()/,
regExForAutoFormatting: /(\[)([^\]]*)(\]\()([^)]*)(\)*)(\s)$/,
}
const allMarkdownCriteriaForTextNodes: MarkdownCriteriaArray = [
// Place the combination formats ahead of the individual formats.
// Combos
markdownStrikethroughItalicBold,
markdownItalicbold,
markdownStrikethroughItalic,
markdownStrikethroughBold, // Individuals
markdownInlineCode,
markdownBold,
markdownItalic, // Must appear after markdownBold
markdownBold2,
markdownItalic2, // Must appear after markdownBold2.
fakeMarkdownUnderline,
markdownStrikethrough,
markdownLink,
]
const allMarkdownCriteriaForParagraphs: MarkdownCriteriaArray = [
markdownHeader1,
markdownHeader2,
markdownHeader3,
markdownHeader4,
markdownHeader5,
markdownHeader6,
markdownBlockQuote,
markdownUnorderedListDash,
markdownUnorderedListAsterisk,
markdownOrderedList,
markdownHorizontalRule,
markdownHorizontalRuleUsingDashes,
]
export function getAllMarkdownCriteriaForParagraphs(): MarkdownCriteriaArray {
return allMarkdownCriteriaForParagraphs
}
export function getAllMarkdownCriteriaForTextNodes(): MarkdownCriteriaArray {
return allMarkdownCriteriaForTextNodes
}
type Block = (
node: LexicalNode,
exportChildren: (elementNode: ElementNode) => string,
) => null | string
function createHeadingExport(level: number): Block {
return (node, exportChildren) => {
return $isHeadingNode(node) && node.getTag() === 'h' + level
? '#'.repeat(level) + ' ' + exportChildren(node)
: null
}
}
function listExport(node: LexicalNode, exportChildren: (_node: ElementNode) => string) {
return $isListNode(node) ? processNestedLists(node, exportChildren, 0) : null
}
// TODO: should be param
const LIST_INDENT_SIZE = 4
function processNestedLists(
listNode: ListNode,
exportChildren: (node: ElementNode) => string,
depth: number,
): string {
const output: string[] = []
const children = listNode.getChildren()
let index = 0
for (const listItemNode of children) {
if ($isListItemNode(listItemNode)) {
if (listItemNode.getChildrenSize() === 1) {
const firstChild = listItemNode.getFirstChild()
if ($isListNode(firstChild)) {
output.push(processNestedLists(firstChild, exportChildren, depth + 1))
continue
}
}
const indent = ' '.repeat(depth * LIST_INDENT_SIZE)
const prefix = listNode.getListType() === 'bullet' ? '- ' : `${listNode.getStart() + index}. `
output.push(indent + prefix + exportChildren(listItemNode))
index++
}
}
return output.join('\n')
}
function blockQuoteExport(node: LexicalNode, exportChildren: (_node: ElementNode) => string) {
return $isQuoteNode(node) ? '> ' + exportChildren(node) : null
}
export function indexBy<T>(
list: Array<T>,
callback: (arg0: T) => string | undefined,
): Readonly<Record<string, Array<T>>> {
const index: Record<string, Array<T>> = {}
for (const item of list) {
const key = callback(item)
if (!key) {
continue
}
if (index[key]) {
index[key].push(item)
} else {
index[key] = [item]
}
}
return index
}
export function transformersByType(transformers: Array<Transformer>): Readonly<{
element: Array<ElementTransformer>
multilineElement: Array<MultilineElementTransformer>
textFormat: Array<TextFormatTransformer>
textMatch: Array<TextMatchTransformer>
}> {
const byType = indexBy(transformers, (t) => t.type)
return {
element: (byType.element || []) as Array<ElementTransformer>,
multilineElement: (byType['multiline-element'] || []) as Array<MultilineElementTransformer>,
textFormat: (byType['text-format'] || []) as Array<TextFormatTransformer>,
textMatch: (byType['text-match'] || []) as Array<TextMatchTransformer>,
}
}
export const PUNCTUATION_OR_SPACE = /[!-/:-@[-`{-~\s]/
const MARKDOWN_EMPTY_LINE_REG_EXP = /^\s{0,3}$/
export function isEmptyParagraph(node: LexicalNode): boolean {
if (!$isParagraphNode(node)) {
return false
}
const firstChild = node.getFirstChild()
return (
firstChild == null ||
(node.getChildrenSize() === 1 &&
$isTextNode(firstChild) &&
MARKDOWN_EMPTY_LINE_REG_EXP.test(firstChild.getTextContent()))
)
}

3
pnpm-lock.yaml generated
View File

@@ -1261,6 +1261,9 @@ importers:
'@lexical/mark':
specifier: 0.34.0
version: 0.34.0
'@lexical/markdown':
specifier: 0.34.0
version: 0.34.0
'@lexical/react':
specifier: 0.34.0
version: 0.34.0(react-dom@19.1.0(react@19.1.0))(react@19.1.0)(yjs@13.6.20)