-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(richtext-lexical): link markdown transformers (#6543)
Closes #6507 --------- Co-authored-by: ShawnVogt <[email protected]>
- Loading branch information
Showing
8 changed files
with
234 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
packages/richtext-lexical/src/field/features/link/markdownTransformer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* Code taken 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 type { TextMatchTransformer } from '@lexical/markdown' | ||
|
||
import { $createTextNode, $isTextNode } from 'lexical' | ||
|
||
import { $createLinkNode, $isLinkNode, LinkNode } from './nodes/LinkNode.js' | ||
|
||
// - then longer tags match (e.g. ** or __ should go before * or _) | ||
export const LinkMarkdownTransformer: TextMatchTransformer = { | ||
type: 'text-match', | ||
dependencies: [LinkNode], | ||
export: (_node, exportChildren, exportFormat) => { | ||
if (!$isLinkNode(_node)) { | ||
return null | ||
} | ||
const node: LinkNode = _node | ||
const { url } = node.getFields() | ||
const linkContent = `[${node.getTextContent()}](${url})` | ||
const firstChild = node.getFirstChild() | ||
// Add text styles only if link has single text node inside. If it's more | ||
// then one we ignore it as markdown does not support nested styles for links | ||
if (node.getChildrenSize() === 1 && $isTextNode(firstChild)) { | ||
return exportFormat(firstChild, linkContent) | ||
} else { | ||
return linkContent | ||
} | ||
}, | ||
importRegExp: /\[([^[]+)\]\(([^()\s]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?\)/, | ||
regExp: /\[([^[]+)\]\(([^()\s]+)(?:\s"((?:[^"]*\\")*[^"]*)"\s*)?\)$/, | ||
replace: (textNode, match) => { | ||
const [, linkText, linkUrl] = match | ||
const linkNode = $createLinkNode({ | ||
fields: { | ||
doc: null, | ||
linkType: 'custom', | ||
newTab: false, | ||
url: linkUrl, | ||
}, | ||
}) | ||
const linkTextNode = $createTextNode(linkText) | ||
linkTextNode.setFormat(textNode.getFormat()) | ||
linkNode.append(linkTextNode) | ||
textNode.replace(linkNode) | ||
}, | ||
trigger: ')', | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.