@astryxdesign/richtext 0.7.10 → 0.7.11-canary.3df65bf
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -0
- package/dist/CodeBackspacePlugin.d.ts +2 -0
- package/dist/CodeBackspacePlugin.d.ts.map +1 -0
- package/dist/CodeBackspacePlugin.js +70 -0
- package/dist/ImageSourceCaretPlugin.d.ts +3 -0
- package/dist/ImageSourceCaretPlugin.d.ts.map +1 -0
- package/dist/ImageSourceCaretPlugin.js +105 -0
- package/dist/RichTextEditor.d.ts.map +1 -1
- package/dist/RichTextEditor.js +4 -2
- package/dist/markdownCharacterReferences.d.ts +24 -1
- package/dist/markdownCharacterReferences.d.ts.map +1 -1
- package/dist/markdownCharacterReferences.js +141 -1
- package/dist/markdownSource.d.ts.map +1 -1
- package/dist/markdownSource.js +42 -7
- package/package.json +5 -5
- package/src/CodeBackspacePlugin.tsx +96 -0
- package/src/ImageSourceCaretPlugin.tsx +159 -0
- package/src/RichTextEditor.doc.mjs +5 -0
- package/src/RichTextEditor.tsx +6 -0
- package/src/markdownCharacterReferences.ts +135 -0
- package/src/markdownSource.ts +56 -6
package/README.md
CHANGED
|
@@ -69,6 +69,11 @@ so your theme's token overrides (`@layer astryx-theme`) win over it, as long
|
|
|
69
69
|
as Core's `reset.css` (or a `@layer reset, astryx-base, astryx-theme;` line,
|
|
70
70
|
if your app has its own reset) loads first.
|
|
71
71
|
|
|
72
|
+
## Editing code blocks
|
|
73
|
+
|
|
74
|
+
Backspace at the very start of a code block turns it into a paragraph of its
|
|
75
|
+
lines, instead of merging the code into the block above.
|
|
76
|
+
|
|
72
77
|
## Examples
|
|
73
78
|
|
|
74
79
|
Interactive examples live in Storybook under **RichText/RichTextEditor**
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CodeBackspacePlugin.d.ts","sourceRoot":"","sources":["../src/CodeBackspacePlugin.tsx"],"names":[],"mappings":"AAsEA,wBAAgB,mBAAmB,IAAI,IAAI,CAyB1C"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
'use client';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @file CodeBackspacePlugin.tsx
|
|
7
|
+
* @input Uses @lexical/react (composer context), @lexical/code (CodeNode), and
|
|
8
|
+
* lexical.
|
|
9
|
+
* @output Exports CodeBackspacePlugin, which makes Backspace at the very start
|
|
10
|
+
* of a code block turn the block into a paragraph holding its lines.
|
|
11
|
+
* @position Rendered by RichTextEditor; always on, not exported. Lexical's
|
|
12
|
+
* own Backspace there merges the code into the block above, and a heading
|
|
13
|
+
* above reads back as a longer heading and a stray paragraph.
|
|
14
|
+
*/
|
|
15
|
+
import { useEffect } from 'react';
|
|
16
|
+
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
|
|
17
|
+
import { $isCodeNode } from '@lexical/code';
|
|
18
|
+
import { $createLineBreakNode, $createParagraphNode, $createTextNode, $getSelection, $isRangeSelection, COMMAND_PRIORITY_LOW, DELETE_CHARACTER_COMMAND } from 'lexical';
|
|
19
|
+
|
|
20
|
+
/** Start is offset 0 of the block or of its first child, not the start of a later line. */
|
|
21
|
+
function $codeBlockAtCaretStart() {
|
|
22
|
+
const selection = $getSelection();
|
|
23
|
+
if (!$isRangeSelection(selection) || !selection.isCollapsed() || selection.anchor.offset !== 0) {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
const node = selection.anchor.getNode();
|
|
27
|
+
const code = $isCodeNode(node) ? node : node.getParent();
|
|
28
|
+
if (!$isCodeNode(code)) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
return node.is(code) || node.is(code.getFirstChild()) ? code : null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* A newline inside text exports as a soft break, and the lines read back as
|
|
36
|
+
* one. A line break node exports as a hard break. Empty lines at the end of
|
|
37
|
+
* the code are dropped: a hard break that ends a paragraph reads back as a
|
|
38
|
+
* literal backslash.
|
|
39
|
+
*/
|
|
40
|
+
function $paragraphOfLines(code) {
|
|
41
|
+
const paragraph = $createParagraphNode();
|
|
42
|
+
code.getTextContent().replace(/\n+$/, '').split('\n').forEach((line, index) => {
|
|
43
|
+
if (index > 0) {
|
|
44
|
+
paragraph.append($createLineBreakNode());
|
|
45
|
+
}
|
|
46
|
+
if (line !== '') {
|
|
47
|
+
paragraph.append($createTextNode(line));
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
return paragraph;
|
|
51
|
+
}
|
|
52
|
+
export function CodeBackspacePlugin() {
|
|
53
|
+
const [editor] = useLexicalComposerContext();
|
|
54
|
+
useEffect(() => editor.registerCommand(DELETE_CHARACTER_COMMAND, isBackward => {
|
|
55
|
+
if (!isBackward || !editor.isEditable()) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
const code = $codeBlockAtCaretStart();
|
|
59
|
+
if (code == null) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
const paragraph = $paragraphOfLines(code);
|
|
63
|
+
code.replace(paragraph);
|
|
64
|
+
paragraph.selectStart();
|
|
65
|
+
return true;
|
|
66
|
+
},
|
|
67
|
+
// Ahead of the rich-text deletion, which would do the merge.
|
|
68
|
+
COMMAND_PRIORITY_LOW), [editor]);
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ImageSourceCaretPlugin.d.ts","sourceRoot":"","sources":["../src/ImageSourceCaretPlugin.tsx"],"names":[],"mappings":"AAyJA,yEAAyE;AACzE,wBAAgB,sBAAsB,IAAI,IAAI,CAI7C"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
'use client';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @file ImageSourceCaretPlugin.tsx
|
|
7
|
+
* @input Uses @lexical/react (composer context), lexical (selection, the
|
|
8
|
+
* selection-change and text-insertion commands, the root element), and
|
|
9
|
+
* $isImageSource from markdownCharacterReferences.ts.
|
|
10
|
+
* @output Exports ImageSourceCaretPlugin.
|
|
11
|
+
* @position Rendered by RichTextEditor, always on. Lexical replaces a whole
|
|
12
|
+
* token with text typed inside it. So a caret inside an image's source
|
|
13
|
+
* moves to an edge: past the image for an arrow key, to the nearer edge
|
|
14
|
+
* for a click or any other move.
|
|
15
|
+
*/
|
|
16
|
+
import { useEffect } from 'react';
|
|
17
|
+
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
|
|
18
|
+
import { mergeRegister } from '@lexical/utils';
|
|
19
|
+
import { $getNodeByKey, $getSelection, $isRangeSelection, COMMAND_PRIORITY_CRITICAL, CONTROLLED_TEXT_INSERTION_COMMAND, SELECTION_CHANGE_COMMAND } from 'lexical';
|
|
20
|
+
import { $isImageSource } from "./markdownCharacterReferences.js";
|
|
21
|
+
/** The collapsed caret, when it is in text. */
|
|
22
|
+
function $caretPoint() {
|
|
23
|
+
const selection = $getSelection();
|
|
24
|
+
return $isRangeSelection(selection) && selection.isCollapsed() && selection.anchor.type === 'text' ? {
|
|
25
|
+
key: selection.anchor.key,
|
|
26
|
+
offset: selection.anchor.offset
|
|
27
|
+
} : null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The edge of `token` a caret at `offset` inside it moves to. An arrow key
|
|
32
|
+
* that moved the caret in from one edge, or from the text beside it, moves
|
|
33
|
+
* it on to the other edge.
|
|
34
|
+
*/
|
|
35
|
+
function $edgeFor(token, offset, arrowFrom) {
|
|
36
|
+
const size = token.getTextContentSize();
|
|
37
|
+
const nearer = offset < size / 2 ? 0 : size;
|
|
38
|
+
if (arrowFrom == null) {
|
|
39
|
+
return nearer;
|
|
40
|
+
}
|
|
41
|
+
if (arrowFrom.key === token.getKey()) {
|
|
42
|
+
return arrowFrom.offset === 0 ? size : arrowFrom.offset === size ? 0 : nearer;
|
|
43
|
+
}
|
|
44
|
+
const before = token.getPreviousSibling();
|
|
45
|
+
if (before != null && before.getKey() === arrowFrom.key && arrowFrom.offset === before.getTextContentSize()) {
|
|
46
|
+
return size;
|
|
47
|
+
}
|
|
48
|
+
const after = token.getNextSibling();
|
|
49
|
+
if (after != null && after.getKey() === arrowFrom.key && arrowFrom.offset === 0) {
|
|
50
|
+
return 0;
|
|
51
|
+
}
|
|
52
|
+
return nearer;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Moves a collapsed caret inside an image's source to an edge of it. */
|
|
56
|
+
function $moveCaretOutOfImage(arrowFrom) {
|
|
57
|
+
const caret = $caretPoint();
|
|
58
|
+
if (caret == null) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const node = $getNodeByKey(caret.key);
|
|
62
|
+
if (node == null || !$isImageSource(node) || caret.offset === 0 || caret.offset === node.getTextContentSize()) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const edge = $edgeFor(node, caret.offset, arrowFrom);
|
|
66
|
+
const selection = $getSelection();
|
|
67
|
+
if ($isRangeSelection(selection)) {
|
|
68
|
+
selection.anchor.set(caret.key, edge, 'text');
|
|
69
|
+
selection.focus.set(caret.key, edge, 'text');
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function registerImageSourceCaret(editor) {
|
|
73
|
+
// Where the caret was when an arrow key went down; the next selection
|
|
74
|
+
// change reads it once. A press is a click, never an arrow key.
|
|
75
|
+
let arrowFrom = null;
|
|
76
|
+
const onKeyDown = event => {
|
|
77
|
+
arrowFrom = event.key === 'ArrowLeft' || event.key === 'ArrowRight' ? editor.getEditorState().read($caretPoint) : null;
|
|
78
|
+
};
|
|
79
|
+
const onPointerDown = () => {
|
|
80
|
+
arrowFrom = null;
|
|
81
|
+
};
|
|
82
|
+
return mergeRegister(editor.registerRootListener((root, previousRoot) => {
|
|
83
|
+
previousRoot?.removeEventListener('keydown', onKeyDown, true);
|
|
84
|
+
previousRoot?.removeEventListener('pointerdown', onPointerDown, true);
|
|
85
|
+
root?.addEventListener('keydown', onKeyDown, true);
|
|
86
|
+
root?.addEventListener('pointerdown', onPointerDown, true);
|
|
87
|
+
}), editor.registerCommand(SELECTION_CHANGE_COMMAND, () => {
|
|
88
|
+
$moveCaretOutOfImage(arrowFrom);
|
|
89
|
+
arrowFrom = null;
|
|
90
|
+
return false;
|
|
91
|
+
}, COMMAND_PRIORITY_CRITICAL),
|
|
92
|
+
// A key typed before the browser reports a caret move reaches the editor
|
|
93
|
+
// with the caret still inside the source.
|
|
94
|
+
editor.registerCommand(CONTROLLED_TEXT_INSERTION_COMMAND, () => {
|
|
95
|
+
$moveCaretOutOfImage(null);
|
|
96
|
+
return false;
|
|
97
|
+
}, COMMAND_PRIORITY_CRITICAL));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Keeps a collapsed caret out of the text holding an image's source. */
|
|
101
|
+
export function ImageSourceCaretPlugin() {
|
|
102
|
+
const [editor] = useLexicalComposerContext();
|
|
103
|
+
useEffect(() => registerImageSourceCaret(editor), [editor]);
|
|
104
|
+
return null;
|
|
105
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RichTextEditor.d.ts","sourceRoot":"","sources":["../src/RichTextEditor.tsx"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAOL,KAAK,SAAS,EACd,KAAK,GAAG,EACT,MAAM,OAAO,CAAC;AASf,OAAO,EAML,KAAK,kBAAkB,EACxB,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EAAC,SAAS,EAAC,MAAM,oBAAoB,CAAC;AAIlD,OAAO,EAIL,KAAK,SAAS,EACf,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"RichTextEditor.d.ts","sourceRoot":"","sources":["../src/RichTextEditor.tsx"],"names":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CAAC;IAC/B;;;;;;;;;;;;;;;;;OAiBG;IACH,YAAY,CAAC,EAAE,aAAa,CAAC,WAAW,CAAC,CAAC;IAC1C;;;;;;;OAOG;IACH,kBAAkB,CAAC,EAAE,aAAa,CAAC,yBAAyB,CAAC,CAAC;IAC9D;;;;;;OAMG;IACH,aAAa,CAAC,EAAE,aAAa,CAAC,YAAY,CAAC,CAAC;IAC5C,yEAAyE;IACzE,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB;;;;;;;OAOG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;;;;;OAMG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,eAAO,MAAM,cAAc;2ZAiCxB,mBAAmB;;CA0PrB,CAAC"}
|
package/dist/RichTextEditor.js
CHANGED
|
@@ -49,11 +49,13 @@ import { TablePlugin } from '@lexical/react/LexicalTablePlugin';
|
|
|
49
49
|
import { HorizontalRuleExtension } from '@lexical/extension';
|
|
50
50
|
import { TextSemanticsExtension } from "./textSemantics.js";
|
|
51
51
|
import { TableColumnFloorPlugin } from "./TableColumnFloorPlugin.js";
|
|
52
|
+
import { CodeBackspacePlugin } from "./CodeBackspacePlugin.js";
|
|
52
53
|
import { CodeBlockHeaderPlugin } from "./CodeBlockHeaderPlugin.js";
|
|
53
54
|
import { CodeSyntaxPlugin } from "./CodeSyntaxPlugin.js";
|
|
54
55
|
import { MarkdownExtensionsPlugin } from "./MarkdownExtensionsPlugin.js";
|
|
55
56
|
import { TaskCheckboxPlugin } from "./TaskCheckboxPlugin.js";
|
|
56
57
|
import { SelectAllScopePlugin } from "./SelectAllScopePlugin.js";
|
|
58
|
+
import { ImageSourceCaretPlugin } from "./ImageSourceCaretPlugin.js";
|
|
57
59
|
import { $generateHtmlFromNodes } from '@lexical/html';
|
|
58
60
|
import { DEFAULT_NODES } from "./editorNodes.js";
|
|
59
61
|
import { $exportMarkdownKeepingSource } from "./markdownSource.js";
|
|
@@ -359,10 +361,10 @@ export const RichTextEditor = function RichTextEditor({
|
|
|
359
361
|
codeLanguages: codeLanguages
|
|
360
362
|
}), /*#__PURE__*/_jsx(CodeSyntaxPlugin, {
|
|
361
363
|
codeLanguages: codeLanguages
|
|
362
|
-
}), markdownExtensions != null && markdownExtensions.length > 0 ? /*#__PURE__*/_jsx(MarkdownExtensionsPlugin, {
|
|
364
|
+
}), /*#__PURE__*/_jsx(CodeBackspacePlugin, {}), markdownExtensions != null && markdownExtensions.length > 0 ? /*#__PURE__*/_jsx(MarkdownExtensionsPlugin, {
|
|
363
365
|
extensions: markdownExtensions,
|
|
364
366
|
transformers: markdownTransformers
|
|
365
|
-
}) : null, /*#__PURE__*/_jsx(TabIndentationPlugin, {}), /*#__PURE__*/_jsx(TabFocusEscapePlugin, {}), /*#__PURE__*/_jsx(SelectAllScopePlugin, {}), hasMarkdownShortcuts && /*#__PURE__*/_jsx(MarkdownShortcutPlugin, {
|
|
367
|
+
}) : null, /*#__PURE__*/_jsx(TabIndentationPlugin, {}), /*#__PURE__*/_jsx(TabFocusEscapePlugin, {}), /*#__PURE__*/_jsx(SelectAllScopePlugin, {}), /*#__PURE__*/_jsx(ImageSourceCaretPlugin, {}), hasMarkdownShortcuts && /*#__PURE__*/_jsx(MarkdownShortcutPlugin, {
|
|
366
368
|
transformers: markdownTransformers
|
|
367
369
|
}), hasAutoFocus && /*#__PURE__*/_jsx(AutoFocusOnMount, {}), onChange && /*#__PURE__*/_jsx(OnChangePlugin, {
|
|
368
370
|
onChange: onChange,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type ElementNode } from 'lexical';
|
|
1
|
+
import { type ElementNode, type LexicalNode } from 'lexical';
|
|
2
2
|
/**
|
|
3
3
|
* What a stand-in replaced: a reference as written, an escaped `&`, or a
|
|
4
4
|
* backslash-escaped character.
|
|
@@ -54,6 +54,29 @@ export declare function coreRefusesUrl(url: string): boolean;
|
|
|
54
54
|
* `$unwrapRefusedLinks` catches any link the editor still makes.
|
|
55
55
|
*/
|
|
56
56
|
export declare function protectRefusedLinks(markdown: string): ProtectedMarkdown;
|
|
57
|
+
/** Whether `node` is text that holds an image's Markdown source. */
|
|
58
|
+
export declare function $isImageSource(node: LexicalNode): boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Returns `markdown` with every image outside code replaced by a private-use
|
|
61
|
+
* stand-in, and the source each stand-in replaced. An image starts with a `!`
|
|
62
|
+
* that is not escaped and the paired brackets of its text. An inline image
|
|
63
|
+
* (``) ends with the destination and title that close it, read whole
|
|
64
|
+
* by core's inline parser. A reference image (`![a][b]`, `![a][]`, `![a]`)
|
|
65
|
+
* ends with its label; its definition can sit in another block, so it is kept
|
|
66
|
+
* whether or not one exists. A link around an image keeps its own brackets,
|
|
67
|
+
* so the image stays inside the link's text.
|
|
68
|
+
*/
|
|
69
|
+
export declare function protectImages(markdown: string): {
|
|
70
|
+
readonly markdown: string;
|
|
71
|
+
readonly images: ReadonlyMap<string, string>;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Puts each image's source back where its stand-in sits under `node`, as
|
|
75
|
+
* text marked as an image's source. The text is a token: a person deletes it
|
|
76
|
+
* whole, and autolinking leaves its URL alone. ImageSourceCaretPlugin keeps
|
|
77
|
+
* the caret out of it, so typing never replaces it.
|
|
78
|
+
*/
|
|
79
|
+
export declare function $restoreImages(node: ElementNode, images: ReadonlyMap<string, string>): void;
|
|
57
80
|
/**
|
|
58
81
|
* Takes every link under `node` whose URL core Markdown refuses out of its
|
|
59
82
|
* link, leaving its text in place, so no form of link text or destination
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"markdownCharacterReferences.d.ts","sourceRoot":"","sources":["../src/markdownCharacterReferences.ts"],"names":[],"mappings":"AAiCA,OAAO,
|
|
1
|
+
{"version":3,"file":"markdownCharacterReferences.d.ts","sourceRoot":"","sources":["../src/mark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lexical/link';
|
|
32
32
|
import { $isTableCellNode } from '@lexical/table';
|
|
33
33
|
import { $dfs, $findMatchingParent } from '@lexical/utils';
|
|
34
|
-
import { $isElementNode, $isTextNode } from 'lexical';
|
|
34
|
+
import { $getState, $isElementNode, $isTextNode, $setState, createState } from 'lexical';
|
|
35
35
|
import { decodeMarkdownCharacterReferences, parseInlineAst } from '@astryxdesign/core/Markdown/parser';
|
|
36
36
|
|
|
37
37
|
/**
|
|
@@ -469,6 +469,146 @@ export function protectRefusedLinks(markdown) {
|
|
|
469
469
|
};
|
|
470
470
|
}
|
|
471
471
|
|
|
472
|
+
/**
|
|
473
|
+
* Marks text that holds one image's Markdown source. Export writes it
|
|
474
|
+
* unescaped, so it stays an image.
|
|
475
|
+
*/
|
|
476
|
+
const imageSourceState = createState('astryxMdImage', {
|
|
477
|
+
parse: value => value === true
|
|
478
|
+
});
|
|
479
|
+
|
|
480
|
+
/** Whether `node` is text that holds an image's Markdown source. */
|
|
481
|
+
export function $isImageSource(node) {
|
|
482
|
+
return $isTextNode(node) && $getState(node, imageSourceState);
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
* Returns `markdown` with every image outside code replaced by a private-use
|
|
487
|
+
* stand-in, and the source each stand-in replaced. An image starts with a `!`
|
|
488
|
+
* that is not escaped and the paired brackets of its text. An inline image
|
|
489
|
+
* (``) ends with the destination and title that close it, read whole
|
|
490
|
+
* by core's inline parser. A reference image (`![a][b]`, `![a][]`, `![a]`)
|
|
491
|
+
* ends with its label; its definition can sit in another block, so it is kept
|
|
492
|
+
* whether or not one exists. A link around an image keeps its own brackets,
|
|
493
|
+
* so the image stays inside the link's text.
|
|
494
|
+
*/
|
|
495
|
+
export function protectImages(markdown) {
|
|
496
|
+
const images = new Map();
|
|
497
|
+
if (!markdown.includes('![')) {
|
|
498
|
+
return {
|
|
499
|
+
markdown,
|
|
500
|
+
images
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
const pair = pairedDelimiters(markdown, codeRanges(markdown));
|
|
504
|
+
const found = [];
|
|
505
|
+
for (let start = markdown.indexOf('!['); start !== -1; start = markdown.indexOf('![', start + 1)) {
|
|
506
|
+
const open = start + 1;
|
|
507
|
+
const textClose = pair[open];
|
|
508
|
+
if (textClose === -1 || textClose < open) {
|
|
509
|
+
continue;
|
|
510
|
+
}
|
|
511
|
+
// An image inside another image's text belongs to the outer one.
|
|
512
|
+
if (found.length > 0 && start < found[found.length - 1].end) {
|
|
513
|
+
continue;
|
|
514
|
+
}
|
|
515
|
+
let backslashes = 0;
|
|
516
|
+
while (markdown[start - 1 - backslashes] === '\\') {
|
|
517
|
+
backslashes++;
|
|
518
|
+
}
|
|
519
|
+
if (backslashes % 2 === 1) {
|
|
520
|
+
continue;
|
|
521
|
+
}
|
|
522
|
+
const after = markdown[textClose + 1];
|
|
523
|
+
if (after === '(') {
|
|
524
|
+
const angleClose = angleDestinationEnd(markdown, textClose + 1);
|
|
525
|
+
const close = typeof angleClose === 'number' ? angleClose : pair[textClose + 1];
|
|
526
|
+
if (close === -1 || angleClose === 'refused') {
|
|
527
|
+
continue;
|
|
528
|
+
}
|
|
529
|
+
const [only, ...rest] = parseInlineAst(markdown.slice(start, close + 1));
|
|
530
|
+
if (rest.length > 0 || only?.type !== 'image') {
|
|
531
|
+
continue;
|
|
532
|
+
}
|
|
533
|
+
found.push({
|
|
534
|
+
start,
|
|
535
|
+
end: close + 1
|
|
536
|
+
});
|
|
537
|
+
} else if (after === '[' && pair[textClose + 1] > textClose + 1) {
|
|
538
|
+
found.push({
|
|
539
|
+
start,
|
|
540
|
+
end: pair[textClose + 1] + 1
|
|
541
|
+
});
|
|
542
|
+
} else {
|
|
543
|
+
found.push({
|
|
544
|
+
start,
|
|
545
|
+
end: textClose + 1
|
|
546
|
+
});
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
if (found.length === 0) {
|
|
550
|
+
return {
|
|
551
|
+
markdown,
|
|
552
|
+
images
|
|
553
|
+
};
|
|
554
|
+
}
|
|
555
|
+
const available = absentCharacters(markdown);
|
|
556
|
+
let output = '';
|
|
557
|
+
let copied = 0;
|
|
558
|
+
for (const {
|
|
559
|
+
start,
|
|
560
|
+
end
|
|
561
|
+
} of found) {
|
|
562
|
+
const next = available.next();
|
|
563
|
+
if (next.done === true) {
|
|
564
|
+
break;
|
|
565
|
+
}
|
|
566
|
+
images.set(next.value, markdown.slice(start, end));
|
|
567
|
+
output += markdown.slice(copied, start) + next.value;
|
|
568
|
+
copied = end;
|
|
569
|
+
}
|
|
570
|
+
return {
|
|
571
|
+
markdown: output + markdown.slice(copied),
|
|
572
|
+
images
|
|
573
|
+
};
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* Puts each image's source back where its stand-in sits under `node`, as
|
|
578
|
+
* text marked as an image's source. The text is a token: a person deletes it
|
|
579
|
+
* whole, and autolinking leaves its URL alone. ImageSourceCaretPlugin keeps
|
|
580
|
+
* the caret out of it, so typing never replaces it.
|
|
581
|
+
*/
|
|
582
|
+
export function $restoreImages(node, images) {
|
|
583
|
+
if (images.size === 0) {
|
|
584
|
+
return;
|
|
585
|
+
}
|
|
586
|
+
for (const text of node.getAllTextNodes()) {
|
|
587
|
+
const content = text.getTextContent();
|
|
588
|
+
const offsets = [];
|
|
589
|
+
let offset = 0;
|
|
590
|
+
for (const character of content) {
|
|
591
|
+
if (images.has(character)) {
|
|
592
|
+
offsets.push(offset, offset + character.length);
|
|
593
|
+
}
|
|
594
|
+
offset += character.length;
|
|
595
|
+
}
|
|
596
|
+
if (offsets.length === 0) {
|
|
597
|
+
continue;
|
|
598
|
+
}
|
|
599
|
+
const pieces = text.splitText(...offsets.filter(point => point > 0 && point < content.length));
|
|
600
|
+
for (const piece of pieces) {
|
|
601
|
+
const source = images.get(piece.getTextContent());
|
|
602
|
+
if (source == null) {
|
|
603
|
+
continue;
|
|
604
|
+
}
|
|
605
|
+
piece.setTextContent(source);
|
|
606
|
+
piece.setMode('token');
|
|
607
|
+
$setState(piece, imageSourceState, true);
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
|
|
472
612
|
/**
|
|
473
613
|
* Takes every link under `node` whose URL core Markdown refuses out of its
|
|
474
614
|
* link, leaving its text in place, so no form of link text or destination
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"markdownSource.d.ts","sourceRoot":"","sources":["../src/markdownSource.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,OAAO,EAA6B,KAAK,WAAW,EAAC,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"markdownSource.d.ts","sourceRoot":"","sources":["../src/markdownSource.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AAEH,OAAO,EAA6B,KAAK,WAAW,EAAC,MAAM,mBAAmB,CAAC;AAI/E,OAAO,EAUL,KAAK,WAAW,EAChB,KAAK,aAAa,EAGnB,MAAM,SAAS,CAAC;AAyBjB,OAAO,KAAK,EAAC,mBAAmB,EAAC,MAAM,qCAAqC,CAAC;AAE7E,8EAA8E;AAC9E,MAAM,WAAW,aAAa;IAC5B,kEAAkE;IAClE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,yEAAyE;IACzE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,wEAAwE;IACxE,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;CAC3B;AA0JD,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,GAAG,KAAK,CAAC,aAAa,CAAC,CA4E1E;AA8CD;;;;;;GAMG;AACH,wBAAgB,2BAA2B,CACzC,MAAM,EAAE,aAAa,EACrB,QAAQ,EAAE,MAAM,EAChB,YAAY,EAAE,KAAK,CAAC,WAAW,CAAC,EAChC,OAAO,GAAE,aAAa,CAAC,mBAAmB,CAAM,GAC/C,IAAI,CAgGN;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,WAAW,GAAG,IAAI,CA6B9D;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,WAAW,GAAG,IAAI,CAoB3D;AAsLD;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAqBhD;AAmOD,wBAAgB,4BAA4B,CAC1C,YAAY,EAAE,aAAa,CAAC,WAAW,CAAC,GACvC,MAAM,CA0ER"}
|
package/dist/markdownSource.js
CHANGED
|
@@ -29,11 +29,12 @@
|
|
|
29
29
|
|
|
30
30
|
import { $convertFromMarkdownString } from '@lexical/markdown';
|
|
31
31
|
import { $isCodeNode } from '@lexical/code';
|
|
32
|
+
import { $isLinkNode } from '@lexical/link';
|
|
32
33
|
import { $isListItemNode, $isListNode } from '@lexical/list';
|
|
33
34
|
import { $createParagraphNode, $createTextNode, $getRoot, $getState, $isElementNode, $isLineBreakNode, $isTextNode, $setState, createState } from 'lexical';
|
|
34
35
|
import { isMarkedHardLineBreak } from "./markdownHardLineBreak.js";
|
|
35
36
|
import { normalizeListIndentation } from "./markdownListIndentation.js";
|
|
36
|
-
import { $restoreCharacterReferences, $restoreCodeSpans, protectBackslashEscapes, protectCharacterReferences, protectCodeSpans, protectLinkDestinationParentheses, protectRefusedLinks, $unwrapRefusedLinks } from "./markdownCharacterReferences.js";
|
|
37
|
+
import { $isImageSource, $restoreCharacterReferences, $restoreCodeSpans, $restoreImages, protectBackslashEscapes, protectCharacterReferences, protectCodeSpans, protectImages, protectLinkDestinationParentheses, protectRefusedLinks, $unwrapRefusedLinks } from "./markdownCharacterReferences.js";
|
|
37
38
|
import { $restoreExtensionSources, absentCharacters, extensionTextView, shieldExtensionSources, withExtensionExport } from "./markdownExtensions.js";
|
|
38
39
|
import { $isRichTextExtensionNode } from "./markdownExtensionNode.js";
|
|
39
40
|
import { $convertToMarkdownKeepingTimeLinear } from "./markdownSpaceRuns.js";
|
|
@@ -299,10 +300,13 @@ export function importMarkdownKeepingSource(editor, markdown, transformers, plug
|
|
|
299
300
|
// source, escapes and parentheses as the literal characters,
|
|
300
301
|
// references decoded.
|
|
301
302
|
const shielded = shieldExtensionSources(withoutCarriageReturns(importChunks[index]?.content ?? chunk.content), plugins);
|
|
302
|
-
//
|
|
303
|
-
//
|
|
304
|
-
//
|
|
305
|
-
|
|
303
|
+
// Images go first: RichText has no image node, so each comes back as
|
|
304
|
+
// its source, whole. Links core refuses go next, on the source as
|
|
305
|
+
// written, so one whose text holds code (`[`x`](javascript:y)`)
|
|
306
|
+
// shows whole as core shows it; code spans next, so a safe link's
|
|
307
|
+
// code still reads as code.
|
|
308
|
+
const images = protectImages(shielded.markdown);
|
|
309
|
+
const refused = protectRefusedLinks(images.markdown);
|
|
306
310
|
const code = protectCodeSpans(refused.markdown);
|
|
307
311
|
const escaped = protectBackslashEscapes(code.markdown);
|
|
308
312
|
const destinations = protectLinkDestinationParentheses(escaped.markdown);
|
|
@@ -310,6 +314,7 @@ export function importMarkdownKeepingSource(editor, markdown, transformers, plug
|
|
|
310
314
|
$convertFromMarkdownString(referenced.markdown, transformers, holder);
|
|
311
315
|
$restoreCharacterReferences(holder, new Map([...refused.standIns, ...code.standIns, ...escaped.standIns, ...destinations.standIns, ...referenced.standIns]));
|
|
312
316
|
$restoreCodeSpans(holder, code.spans);
|
|
317
|
+
$restoreImages(holder, images.images);
|
|
313
318
|
$unwrapRefusedLinks(holder);
|
|
314
319
|
$restoreExtensionSources(holder, shielded.standIns);
|
|
315
320
|
$joinSoftLineBreaks(holder);
|
|
@@ -584,6 +589,11 @@ function markedView(node, token, placeholders) {
|
|
|
584
589
|
if (placeholder != null && $isRichTextExtensionNode(node)) {
|
|
585
590
|
return extensionTextView(node, placeholder);
|
|
586
591
|
}
|
|
592
|
+
if (placeholder != null && $isTextNode(node)) {
|
|
593
|
+
const view = Object.create(node);
|
|
594
|
+
view.getTextContent = () => placeholder;
|
|
595
|
+
return view;
|
|
596
|
+
}
|
|
587
597
|
if ($isTextNode(node)) {
|
|
588
598
|
if (node.hasFormat('code') || $isCodeNode(node.getParent())) {
|
|
589
599
|
return node;
|
|
@@ -592,6 +602,16 @@ function markedView(node, token, placeholders) {
|
|
|
592
602
|
return Object.create(node);
|
|
593
603
|
}
|
|
594
604
|
let text = node.getTextContent().replace(INLINE_SYNTAX, match => token + match);
|
|
605
|
+
// A `!` right before a link would make the link an image.
|
|
606
|
+
if (text.endsWith('!') && $isLinkNode(node.getNextSibling())) {
|
|
607
|
+
text = text.slice(0, -1) + token + '!';
|
|
608
|
+
}
|
|
609
|
+
// A `(` right after an image that ends in `]` would become its
|
|
610
|
+
// destination.
|
|
611
|
+
const before = node.getPreviousSibling();
|
|
612
|
+
if (text.startsWith('(') && before != null && $isImageSource(before) && before.getTextContent().endsWith(']')) {
|
|
613
|
+
text = token + text;
|
|
614
|
+
}
|
|
595
615
|
const previous = node.getPreviousSibling();
|
|
596
616
|
if (previous == null || $isLineBreakNode(previous)) {
|
|
597
617
|
const ordered = ORDERED_LIST_START.exec(text);
|
|
@@ -625,6 +645,20 @@ function markedView(node, token, placeholders) {
|
|
|
625
645
|
return node;
|
|
626
646
|
}
|
|
627
647
|
|
|
648
|
+
// A reference image's label at the end of its source: `[b]` in `![a][b]`.
|
|
649
|
+
const IMAGE_LABEL_END = /\]\[(?:[^\\\]]|\\.)*\]$/;
|
|
650
|
+
|
|
651
|
+
/**
|
|
652
|
+
* An image's source as an export writes it. A shortcut reference image
|
|
653
|
+
* (`![a]`) right before a link writes in its collapsed form (`![a][]`), so
|
|
654
|
+
* the link's text never reads as its label.
|
|
655
|
+
*/
|
|
656
|
+
function $imageSourceForExport(node) {
|
|
657
|
+
const source = node.getTextContent();
|
|
658
|
+
const isShortcut = source.endsWith(']') && !IMAGE_LABEL_END.test(source);
|
|
659
|
+
return isShortcut && $isLinkNode(node.getNextSibling()) ? `${source}[]` : source;
|
|
660
|
+
}
|
|
661
|
+
|
|
628
662
|
/**
|
|
629
663
|
* A placeholder for each inline plugin node among `nodes`, absent from
|
|
630
664
|
* `text`, and the source each placeholder stands for.
|
|
@@ -634,13 +668,14 @@ function extensionPlaceholders(nodes, text) {
|
|
|
634
668
|
const sources = new Map();
|
|
635
669
|
const free = absentCharacters(text);
|
|
636
670
|
for (const node of nodes) {
|
|
637
|
-
|
|
671
|
+
const source = $isRichTextExtensionNode(node) && node.isInline() ? node.getSource() : $isImageSource(node) ? $imageSourceForExport(node) : null;
|
|
672
|
+
if (source != null) {
|
|
638
673
|
const placeholder = free.next().value;
|
|
639
674
|
if (placeholder == null) {
|
|
640
675
|
break;
|
|
641
676
|
}
|
|
642
677
|
placeholders.set(node.getKey(), placeholder);
|
|
643
|
-
sources.set(placeholder,
|
|
678
|
+
sources.set(placeholder, source);
|
|
644
679
|
}
|
|
645
680
|
}
|
|
646
681
|
return {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astryxdesign/richtext",
|
|
3
3
|
"displayName": "Rich Text",
|
|
4
|
-
"version": "0.7.
|
|
4
|
+
"version": "0.7.11-canary.3df65bf",
|
|
5
5
|
"description": "Astryx rich text — a Lexical-based rich text editor and viewer, styled with Astryx design tokens.",
|
|
6
6
|
"author": "Meta Open Source",
|
|
7
7
|
"license": "MIT",
|
|
@@ -66,7 +66,7 @@
|
|
|
66
66
|
},
|
|
67
67
|
"peerDependencies": {
|
|
68
68
|
"@stylexjs/stylex": ">=0.10.0",
|
|
69
|
-
"@astryxdesign/core": "0.7.
|
|
69
|
+
"@astryxdesign/core": "0.7.11-canary.3df65bf",
|
|
70
70
|
"react": ">=19.0.0",
|
|
71
71
|
"react-dom": ">=19.0.0",
|
|
72
72
|
"lexical": "^0.52.0",
|
|
@@ -125,6 +125,8 @@
|
|
|
125
125
|
}
|
|
126
126
|
},
|
|
127
127
|
"devDependencies": {
|
|
128
|
+
"@astryxdesign/a11y-spec": "0.7.11-canary.3df65bf",
|
|
129
|
+
"@astryxdesign/cli": "0.7.11-canary.3df65bf",
|
|
128
130
|
"@babel/cli": "^7.29.7",
|
|
129
131
|
"@babel/core": "^7.29.7",
|
|
130
132
|
"@babel/preset-react": "^7.29.7",
|
|
@@ -143,9 +145,7 @@
|
|
|
143
145
|
"@lexical/utils": "^0.52.0",
|
|
144
146
|
"@stylexjs/babel-plugin": "^0.19.0",
|
|
145
147
|
"@testing-library/react": "^16.3.2",
|
|
146
|
-
"lexical": "^0.52.0"
|
|
147
|
-
"@astryxdesign/a11y-spec": "0.0.0",
|
|
148
|
-
"@astryxdesign/cli": "0.7.10"
|
|
148
|
+
"lexical": "^0.52.0"
|
|
149
149
|
},
|
|
150
150
|
"scripts": {
|
|
151
151
|
"api:report": "node ../../scripts/api-report/cli.mjs --package .",
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
'use client';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @file CodeBackspacePlugin.tsx
|
|
7
|
+
* @input Uses @lexical/react (composer context), @lexical/code (CodeNode), and
|
|
8
|
+
* lexical.
|
|
9
|
+
* @output Exports CodeBackspacePlugin, which makes Backspace at the very start
|
|
10
|
+
* of a code block turn the block into a paragraph holding its lines.
|
|
11
|
+
* @position Rendered by RichTextEditor; always on, not exported. Lexical's
|
|
12
|
+
* own Backspace there merges the code into the block above, and a heading
|
|
13
|
+
* above reads back as a longer heading and a stray paragraph.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import {useEffect} from 'react';
|
|
17
|
+
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
|
|
18
|
+
import {$isCodeNode, type CodeNode} from '@lexical/code';
|
|
19
|
+
import {
|
|
20
|
+
$createLineBreakNode,
|
|
21
|
+
$createParagraphNode,
|
|
22
|
+
$createTextNode,
|
|
23
|
+
$getSelection,
|
|
24
|
+
$isRangeSelection,
|
|
25
|
+
COMMAND_PRIORITY_LOW,
|
|
26
|
+
DELETE_CHARACTER_COMMAND,
|
|
27
|
+
type ParagraphNode,
|
|
28
|
+
} from 'lexical';
|
|
29
|
+
|
|
30
|
+
/** Start is offset 0 of the block or of its first child, not the start of a later line. */
|
|
31
|
+
function $codeBlockAtCaretStart(): CodeNode | null {
|
|
32
|
+
const selection = $getSelection();
|
|
33
|
+
if (
|
|
34
|
+
!$isRangeSelection(selection) ||
|
|
35
|
+
!selection.isCollapsed() ||
|
|
36
|
+
selection.anchor.offset !== 0
|
|
37
|
+
) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
const node = selection.anchor.getNode();
|
|
41
|
+
const code = $isCodeNode(node) ? node : node.getParent();
|
|
42
|
+
if (!$isCodeNode(code)) {
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
return node.is(code) || node.is(code.getFirstChild()) ? code : null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* A newline inside text exports as a soft break, and the lines read back as
|
|
50
|
+
* one. A line break node exports as a hard break. Empty lines at the end of
|
|
51
|
+
* the code are dropped: a hard break that ends a paragraph reads back as a
|
|
52
|
+
* literal backslash.
|
|
53
|
+
*/
|
|
54
|
+
function $paragraphOfLines(code: CodeNode): ParagraphNode {
|
|
55
|
+
const paragraph = $createParagraphNode();
|
|
56
|
+
code
|
|
57
|
+
.getTextContent()
|
|
58
|
+
.replace(/\n+$/, '')
|
|
59
|
+
.split('\n')
|
|
60
|
+
.forEach((line, index) => {
|
|
61
|
+
if (index > 0) {
|
|
62
|
+
paragraph.append($createLineBreakNode());
|
|
63
|
+
}
|
|
64
|
+
if (line !== '') {
|
|
65
|
+
paragraph.append($createTextNode(line));
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
return paragraph;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function CodeBackspacePlugin(): null {
|
|
72
|
+
const [editor] = useLexicalComposerContext();
|
|
73
|
+
useEffect(
|
|
74
|
+
() =>
|
|
75
|
+
editor.registerCommand(
|
|
76
|
+
DELETE_CHARACTER_COMMAND,
|
|
77
|
+
isBackward => {
|
|
78
|
+
if (!isBackward || !editor.isEditable()) {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
const code = $codeBlockAtCaretStart();
|
|
82
|
+
if (code == null) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
const paragraph = $paragraphOfLines(code);
|
|
86
|
+
code.replace(paragraph);
|
|
87
|
+
paragraph.selectStart();
|
|
88
|
+
return true;
|
|
89
|
+
},
|
|
90
|
+
// Ahead of the rich-text deletion, which would do the merge.
|
|
91
|
+
COMMAND_PRIORITY_LOW,
|
|
92
|
+
),
|
|
93
|
+
[editor],
|
|
94
|
+
);
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
+
|
|
3
|
+
'use client';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @file ImageSourceCaretPlugin.tsx
|
|
7
|
+
* @input Uses @lexical/react (composer context), lexical (selection, the
|
|
8
|
+
* selection-change and text-insertion commands, the root element), and
|
|
9
|
+
* $isImageSource from markdownCharacterReferences.ts.
|
|
10
|
+
* @output Exports ImageSourceCaretPlugin.
|
|
11
|
+
* @position Rendered by RichTextEditor, always on. Lexical replaces a whole
|
|
12
|
+
* token with text typed inside it. So a caret inside an image's source
|
|
13
|
+
* moves to an edge: past the image for an arrow key, to the nearer edge
|
|
14
|
+
* for a click or any other move.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import {useEffect} from 'react';
|
|
18
|
+
import {useLexicalComposerContext} from '@lexical/react/LexicalComposerContext';
|
|
19
|
+
import {mergeRegister} from '@lexical/utils';
|
|
20
|
+
import {
|
|
21
|
+
$getNodeByKey,
|
|
22
|
+
$getSelection,
|
|
23
|
+
$isRangeSelection,
|
|
24
|
+
COMMAND_PRIORITY_CRITICAL,
|
|
25
|
+
CONTROLLED_TEXT_INSERTION_COMMAND,
|
|
26
|
+
SELECTION_CHANGE_COMMAND,
|
|
27
|
+
type LexicalEditor,
|
|
28
|
+
type NodeKey,
|
|
29
|
+
type TextNode,
|
|
30
|
+
} from 'lexical';
|
|
31
|
+
import {$isImageSource} from './markdownCharacterReferences';
|
|
32
|
+
|
|
33
|
+
interface CaretPoint {
|
|
34
|
+
readonly key: NodeKey;
|
|
35
|
+
readonly offset: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The collapsed caret, when it is in text. */
|
|
39
|
+
function $caretPoint(): CaretPoint | null {
|
|
40
|
+
const selection = $getSelection();
|
|
41
|
+
return $isRangeSelection(selection) &&
|
|
42
|
+
selection.isCollapsed() &&
|
|
43
|
+
selection.anchor.type === 'text'
|
|
44
|
+
? {key: selection.anchor.key, offset: selection.anchor.offset}
|
|
45
|
+
: null;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The edge of `token` a caret at `offset` inside it moves to. An arrow key
|
|
50
|
+
* that moved the caret in from one edge, or from the text beside it, moves
|
|
51
|
+
* it on to the other edge.
|
|
52
|
+
*/
|
|
53
|
+
function $edgeFor(
|
|
54
|
+
token: TextNode,
|
|
55
|
+
offset: number,
|
|
56
|
+
arrowFrom: CaretPoint | null,
|
|
57
|
+
): number {
|
|
58
|
+
const size = token.getTextContentSize();
|
|
59
|
+
const nearer = offset < size / 2 ? 0 : size;
|
|
60
|
+
if (arrowFrom == null) {
|
|
61
|
+
return nearer;
|
|
62
|
+
}
|
|
63
|
+
if (arrowFrom.key === token.getKey()) {
|
|
64
|
+
return arrowFrom.offset === 0
|
|
65
|
+
? size
|
|
66
|
+
: arrowFrom.offset === size
|
|
67
|
+
? 0
|
|
68
|
+
: nearer;
|
|
69
|
+
}
|
|
70
|
+
const before = token.getPreviousSibling();
|
|
71
|
+
if (
|
|
72
|
+
before != null &&
|
|
73
|
+
before.getKey() === arrowFrom.key &&
|
|
74
|
+
arrowFrom.offset === before.getTextContentSize()
|
|
75
|
+
) {
|
|
76
|
+
return size;
|
|
77
|
+
}
|
|
78
|
+
const after = token.getNextSibling();
|
|
79
|
+
if (
|
|
80
|
+
after != null &&
|
|
81
|
+
after.getKey() === arrowFrom.key &&
|
|
82
|
+
arrowFrom.offset === 0
|
|
83
|
+
) {
|
|
84
|
+
return 0;
|
|
85
|
+
}
|
|
86
|
+
return nearer;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Moves a collapsed caret inside an image's source to an edge of it. */
|
|
90
|
+
function $moveCaretOutOfImage(arrowFrom: CaretPoint | null): void {
|
|
91
|
+
const caret = $caretPoint();
|
|
92
|
+
if (caret == null) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
const node = $getNodeByKey<TextNode>(caret.key);
|
|
96
|
+
if (
|
|
97
|
+
node == null ||
|
|
98
|
+
!$isImageSource(node) ||
|
|
99
|
+
caret.offset === 0 ||
|
|
100
|
+
caret.offset === node.getTextContentSize()
|
|
101
|
+
) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
const edge = $edgeFor(node, caret.offset, arrowFrom);
|
|
105
|
+
const selection = $getSelection();
|
|
106
|
+
if ($isRangeSelection(selection)) {
|
|
107
|
+
selection.anchor.set(caret.key, edge, 'text');
|
|
108
|
+
selection.focus.set(caret.key, edge, 'text');
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function registerImageSourceCaret(editor: LexicalEditor): () => void {
|
|
113
|
+
// Where the caret was when an arrow key went down; the next selection
|
|
114
|
+
// change reads it once. A press is a click, never an arrow key.
|
|
115
|
+
let arrowFrom: CaretPoint | null = null;
|
|
116
|
+
const onKeyDown = (event: KeyboardEvent) => {
|
|
117
|
+
arrowFrom =
|
|
118
|
+
event.key === 'ArrowLeft' || event.key === 'ArrowRight'
|
|
119
|
+
? editor.getEditorState().read($caretPoint)
|
|
120
|
+
: null;
|
|
121
|
+
};
|
|
122
|
+
const onPointerDown = () => {
|
|
123
|
+
arrowFrom = null;
|
|
124
|
+
};
|
|
125
|
+
return mergeRegister(
|
|
126
|
+
editor.registerRootListener((root, previousRoot) => {
|
|
127
|
+
previousRoot?.removeEventListener('keydown', onKeyDown, true);
|
|
128
|
+
previousRoot?.removeEventListener('pointerdown', onPointerDown, true);
|
|
129
|
+
root?.addEventListener('keydown', onKeyDown, true);
|
|
130
|
+
root?.addEventListener('pointerdown', onPointerDown, true);
|
|
131
|
+
}),
|
|
132
|
+
editor.registerCommand(
|
|
133
|
+
SELECTION_CHANGE_COMMAND,
|
|
134
|
+
() => {
|
|
135
|
+
$moveCaretOutOfImage(arrowFrom);
|
|
136
|
+
arrowFrom = null;
|
|
137
|
+
return false;
|
|
138
|
+
},
|
|
139
|
+
COMMAND_PRIORITY_CRITICAL,
|
|
140
|
+
),
|
|
141
|
+
// A key typed before the browser reports a caret move reaches the editor
|
|
142
|
+
// with the caret still inside the source.
|
|
143
|
+
editor.registerCommand(
|
|
144
|
+
CONTROLLED_TEXT_INSERTION_COMMAND,
|
|
145
|
+
() => {
|
|
146
|
+
$moveCaretOutOfImage(null);
|
|
147
|
+
return false;
|
|
148
|
+
},
|
|
149
|
+
COMMAND_PRIORITY_CRITICAL,
|
|
150
|
+
),
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Keeps a collapsed caret out of the text holding an image's source. */
|
|
155
|
+
export function ImageSourceCaretPlugin(): null {
|
|
156
|
+
const [editor] = useLexicalComposerContext();
|
|
157
|
+
useEffect(() => registerImageSourceCaret(editor), [editor]);
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
@@ -238,6 +238,11 @@ export const docs = {
|
|
|
238
238
|
description:
|
|
239
239
|
'GFM pipe tables import, edit, and export by default. The first row renders as header cells, delimiter-row colons set each column alignment, rows shorter than the widest row gain empty cells, and a wide table scrolls inside its own wrapper. Arrow keys move between cells; Tab keeps its editor meaning, so Escape then Tab still leaves the editor. Tables export in the canonical form: outer pipes and three-dash delimiters. RichTextView renders the same tables. Install @lexical/table with the other lexical peers.',
|
|
240
240
|
},
|
|
241
|
+
{
|
|
242
|
+
guidance: true,
|
|
243
|
+
description:
|
|
244
|
+
'Backspace at the very start of a code block turns it into a paragraph holding its lines, each on its own line, with the caret at its start. The code never merges into the block above, so a heading above keeps its own text. Backspace anywhere else in a code block deletes as usual.',
|
|
245
|
+
},
|
|
241
246
|
{
|
|
242
247
|
guidance: true,
|
|
243
248
|
description:
|
package/src/RichTextEditor.tsx
CHANGED
|
@@ -77,6 +77,7 @@ import {TablePlugin} from '@lexical/react/LexicalTablePlugin';
|
|
|
77
77
|
import {HorizontalRuleExtension} from '@lexical/extension';
|
|
78
78
|
import {TextSemanticsExtension} from './textSemantics';
|
|
79
79
|
import {TableColumnFloorPlugin} from './TableColumnFloorPlugin';
|
|
80
|
+
import {CodeBackspacePlugin} from './CodeBackspacePlugin';
|
|
80
81
|
import {CodeBlockHeaderPlugin} from './CodeBlockHeaderPlugin';
|
|
81
82
|
import {CodeSyntaxPlugin} from './CodeSyntaxPlugin';
|
|
82
83
|
import {MarkdownExtensionsPlugin} from './MarkdownExtensionsPlugin';
|
|
@@ -84,6 +85,7 @@ import type {RichTextMarkdownExtension} from './markdownExtensions';
|
|
|
84
85
|
import type {CodeLanguage} from '@astryxdesign/core/CodeBlock';
|
|
85
86
|
import {TaskCheckboxPlugin} from './TaskCheckboxPlugin';
|
|
86
87
|
import {SelectAllScopePlugin} from './SelectAllScopePlugin';
|
|
88
|
+
import {ImageSourceCaretPlugin} from './ImageSourceCaretPlugin';
|
|
87
89
|
import {type Transformer} from '@lexical/markdown';
|
|
88
90
|
import {$generateHtmlFromNodes} from '@lexical/html';
|
|
89
91
|
import {DEFAULT_NODES} from './editorNodes';
|
|
@@ -723,6 +725,9 @@ export const RichTextEditor = function RichTextEditor({
|
|
|
723
725
|
<TableColumnFloorPlugin />
|
|
724
726
|
<CodeBlockHeaderPlugin codeLanguages={codeLanguages} />
|
|
725
727
|
<CodeSyntaxPlugin codeLanguages={codeLanguages} />
|
|
728
|
+
{/* Backspace at the very start of a code block makes it a
|
|
729
|
+
paragraph, instead of merging it into the block above. */}
|
|
730
|
+
<CodeBackspacePlugin />
|
|
726
731
|
{markdownExtensions != null && markdownExtensions.length > 0 ? (
|
|
727
732
|
<MarkdownExtensionsPlugin
|
|
728
733
|
extensions={markdownExtensions}
|
|
@@ -732,6 +737,7 @@ export const RichTextEditor = function RichTextEditor({
|
|
|
732
737
|
<TabIndentationPlugin />
|
|
733
738
|
<TabFocusEscapePlugin />
|
|
734
739
|
<SelectAllScopePlugin />
|
|
740
|
+
<ImageSourceCaretPlugin />
|
|
735
741
|
{hasMarkdownShortcuts && (
|
|
736
742
|
<MarkdownShortcutPlugin transformers={markdownTransformers} />
|
|
737
743
|
)}
|
|
@@ -32,8 +32,11 @@ import {$isLinkNode} from '@lexical/link';
|
|
|
32
32
|
import {$isTableCellNode} from '@lexical/table';
|
|
33
33
|
import {$dfs, $findMatchingParent} from '@lexical/utils';
|
|
34
34
|
import {
|
|
35
|
+
$getState,
|
|
35
36
|
$isElementNode,
|
|
36
37
|
$isTextNode,
|
|
38
|
+
$setState,
|
|
39
|
+
createState,
|
|
37
40
|
type ElementNode,
|
|
38
41
|
type LexicalNode,
|
|
39
42
|
} from 'lexical';
|
|
@@ -486,6 +489,138 @@ export function protectRefusedLinks(markdown: string): ProtectedMarkdown {
|
|
|
486
489
|
return {markdown: output + markdown.slice(copied), standIns};
|
|
487
490
|
}
|
|
488
491
|
|
|
492
|
+
/**
|
|
493
|
+
* Marks text that holds one image's Markdown source. Export writes it
|
|
494
|
+
* unescaped, so it stays an image.
|
|
495
|
+
*/
|
|
496
|
+
const imageSourceState = createState('astryxMdImage', {
|
|
497
|
+
parse: (value: unknown): boolean => value === true,
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
/** Whether `node` is text that holds an image's Markdown source. */
|
|
501
|
+
export function $isImageSource(node: LexicalNode): boolean {
|
|
502
|
+
return $isTextNode(node) && $getState(node, imageSourceState);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* Returns `markdown` with every image outside code replaced by a private-use
|
|
507
|
+
* stand-in, and the source each stand-in replaced. An image starts with a `!`
|
|
508
|
+
* that is not escaped and the paired brackets of its text. An inline image
|
|
509
|
+
* (``) ends with the destination and title that close it, read whole
|
|
510
|
+
* by core's inline parser. A reference image (`![a][b]`, `![a][]`, `![a]`)
|
|
511
|
+
* ends with its label; its definition can sit in another block, so it is kept
|
|
512
|
+
* whether or not one exists. A link around an image keeps its own brackets,
|
|
513
|
+
* so the image stays inside the link's text.
|
|
514
|
+
*/
|
|
515
|
+
export function protectImages(markdown: string): {
|
|
516
|
+
readonly markdown: string;
|
|
517
|
+
readonly images: ReadonlyMap<string, string>;
|
|
518
|
+
} {
|
|
519
|
+
const images = new Map<string, string>();
|
|
520
|
+
if (!markdown.includes('![')) {
|
|
521
|
+
return {markdown, images};
|
|
522
|
+
}
|
|
523
|
+
const pair = pairedDelimiters(markdown, codeRanges(markdown));
|
|
524
|
+
const found: Array<{readonly start: number; readonly end: number}> = [];
|
|
525
|
+
for (
|
|
526
|
+
let start = markdown.indexOf('![');
|
|
527
|
+
start !== -1;
|
|
528
|
+
start = markdown.indexOf('![', start + 1)
|
|
529
|
+
) {
|
|
530
|
+
const open = start + 1;
|
|
531
|
+
const textClose = pair[open];
|
|
532
|
+
if (textClose === -1 || textClose < open) {
|
|
533
|
+
continue;
|
|
534
|
+
}
|
|
535
|
+
// An image inside another image's text belongs to the outer one.
|
|
536
|
+
if (found.length > 0 && start < found[found.length - 1].end) {
|
|
537
|
+
continue;
|
|
538
|
+
}
|
|
539
|
+
let backslashes = 0;
|
|
540
|
+
while (markdown[start - 1 - backslashes] === '\\') {
|
|
541
|
+
backslashes++;
|
|
542
|
+
}
|
|
543
|
+
if (backslashes % 2 === 1) {
|
|
544
|
+
continue;
|
|
545
|
+
}
|
|
546
|
+
const after = markdown[textClose + 1];
|
|
547
|
+
if (after === '(') {
|
|
548
|
+
const angleClose = angleDestinationEnd(markdown, textClose + 1);
|
|
549
|
+
const close =
|
|
550
|
+
typeof angleClose === 'number' ? angleClose : pair[textClose + 1];
|
|
551
|
+
if (close === -1 || angleClose === 'refused') {
|
|
552
|
+
continue;
|
|
553
|
+
}
|
|
554
|
+
const [only, ...rest] = parseInlineAst(markdown.slice(start, close + 1));
|
|
555
|
+
if (rest.length > 0 || only?.type !== 'image') {
|
|
556
|
+
continue;
|
|
557
|
+
}
|
|
558
|
+
found.push({start, end: close + 1});
|
|
559
|
+
} else if (after === '[' && pair[textClose + 1] > textClose + 1) {
|
|
560
|
+
found.push({start, end: pair[textClose + 1] + 1});
|
|
561
|
+
} else {
|
|
562
|
+
found.push({start, end: textClose + 1});
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
if (found.length === 0) {
|
|
566
|
+
return {markdown, images};
|
|
567
|
+
}
|
|
568
|
+
const available = absentCharacters(markdown);
|
|
569
|
+
let output = '';
|
|
570
|
+
let copied = 0;
|
|
571
|
+
for (const {start, end} of found) {
|
|
572
|
+
const next = available.next();
|
|
573
|
+
if (next.done === true) {
|
|
574
|
+
break;
|
|
575
|
+
}
|
|
576
|
+
images.set(next.value, markdown.slice(start, end));
|
|
577
|
+
output += markdown.slice(copied, start) + next.value;
|
|
578
|
+
copied = end;
|
|
579
|
+
}
|
|
580
|
+
return {markdown: output + markdown.slice(copied), images};
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
/**
|
|
584
|
+
* Puts each image's source back where its stand-in sits under `node`, as
|
|
585
|
+
* text marked as an image's source. The text is a token: a person deletes it
|
|
586
|
+
* whole, and autolinking leaves its URL alone. ImageSourceCaretPlugin keeps
|
|
587
|
+
* the caret out of it, so typing never replaces it.
|
|
588
|
+
*/
|
|
589
|
+
export function $restoreImages(
|
|
590
|
+
node: ElementNode,
|
|
591
|
+
images: ReadonlyMap<string, string>,
|
|
592
|
+
): void {
|
|
593
|
+
if (images.size === 0) {
|
|
594
|
+
return;
|
|
595
|
+
}
|
|
596
|
+
for (const text of node.getAllTextNodes()) {
|
|
597
|
+
const content = text.getTextContent();
|
|
598
|
+
const offsets: number[] = [];
|
|
599
|
+
let offset = 0;
|
|
600
|
+
for (const character of content) {
|
|
601
|
+
if (images.has(character)) {
|
|
602
|
+
offsets.push(offset, offset + character.length);
|
|
603
|
+
}
|
|
604
|
+
offset += character.length;
|
|
605
|
+
}
|
|
606
|
+
if (offsets.length === 0) {
|
|
607
|
+
continue;
|
|
608
|
+
}
|
|
609
|
+
const pieces = text.splitText(
|
|
610
|
+
...offsets.filter(point => point > 0 && point < content.length),
|
|
611
|
+
);
|
|
612
|
+
for (const piece of pieces) {
|
|
613
|
+
const source = images.get(piece.getTextContent());
|
|
614
|
+
if (source == null) {
|
|
615
|
+
continue;
|
|
616
|
+
}
|
|
617
|
+
piece.setTextContent(source);
|
|
618
|
+
piece.setMode('token');
|
|
619
|
+
$setState(piece, imageSourceState, true);
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
|
|
489
624
|
/**
|
|
490
625
|
* Takes every link under `node` whose URL core Markdown refuses out of its
|
|
491
626
|
* link, leaving its text in place, so no form of link text or destination
|
package/src/markdownSource.ts
CHANGED
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
|
|
30
30
|
import {$convertFromMarkdownString, type Transformer} from '@lexical/markdown';
|
|
31
31
|
import {$isCodeNode} from '@lexical/code';
|
|
32
|
+
import {$isLinkNode} from '@lexical/link';
|
|
32
33
|
import {$isListItemNode, $isListNode} from '@lexical/list';
|
|
33
34
|
import {
|
|
34
35
|
$createParagraphNode,
|
|
@@ -48,11 +49,14 @@ import {
|
|
|
48
49
|
import {isMarkedHardLineBreak} from './markdownHardLineBreak';
|
|
49
50
|
import {normalizeListIndentation} from './markdownListIndentation';
|
|
50
51
|
import {
|
|
52
|
+
$isImageSource,
|
|
51
53
|
$restoreCharacterReferences,
|
|
52
54
|
$restoreCodeSpans,
|
|
55
|
+
$restoreImages,
|
|
53
56
|
protectBackslashEscapes,
|
|
54
57
|
protectCharacterReferences,
|
|
55
58
|
protectCodeSpans,
|
|
59
|
+
protectImages,
|
|
56
60
|
protectLinkDestinationParentheses,
|
|
57
61
|
protectRefusedLinks,
|
|
58
62
|
$unwrapRefusedLinks,
|
|
@@ -396,10 +400,13 @@ export function importMarkdownKeepingSource(
|
|
|
396
400
|
withoutCarriageReturns(importChunks[index]?.content ?? chunk.content),
|
|
397
401
|
plugins,
|
|
398
402
|
);
|
|
399
|
-
//
|
|
400
|
-
//
|
|
401
|
-
//
|
|
402
|
-
|
|
403
|
+
// Images go first: RichText has no image node, so each comes back as
|
|
404
|
+
// its source, whole. Links core refuses go next, on the source as
|
|
405
|
+
// written, so one whose text holds code (`[`x`](javascript:y)`)
|
|
406
|
+
// shows whole as core shows it; code spans next, so a safe link's
|
|
407
|
+
// code still reads as code.
|
|
408
|
+
const images = protectImages(shielded.markdown);
|
|
409
|
+
const refused = protectRefusedLinks(images.markdown);
|
|
403
410
|
const code = protectCodeSpans(refused.markdown);
|
|
404
411
|
const escaped = protectBackslashEscapes(code.markdown);
|
|
405
412
|
const destinations = protectLinkDestinationParentheses(
|
|
@@ -418,6 +425,7 @@ export function importMarkdownKeepingSource(
|
|
|
418
425
|
]),
|
|
419
426
|
);
|
|
420
427
|
$restoreCodeSpans(holder, code.spans);
|
|
428
|
+
$restoreImages(holder, images.images);
|
|
421
429
|
$unwrapRefusedLinks(holder);
|
|
422
430
|
$restoreExtensionSources(holder, shielded.standIns);
|
|
423
431
|
$joinSoftLineBreaks(holder);
|
|
@@ -749,6 +757,11 @@ function markedView(
|
|
|
749
757
|
if (placeholder != null && $isRichTextExtensionNode(node)) {
|
|
750
758
|
return extensionTextView(node, placeholder);
|
|
751
759
|
}
|
|
760
|
+
if (placeholder != null && $isTextNode(node)) {
|
|
761
|
+
const view = Object.create(node) as typeof node;
|
|
762
|
+
view.getTextContent = () => placeholder;
|
|
763
|
+
return view;
|
|
764
|
+
}
|
|
752
765
|
if ($isTextNode(node)) {
|
|
753
766
|
if (node.hasFormat('code') || $isCodeNode(node.getParent())) {
|
|
754
767
|
return node;
|
|
@@ -759,6 +772,21 @@ function markedView(
|
|
|
759
772
|
let text = node
|
|
760
773
|
.getTextContent()
|
|
761
774
|
.replace(INLINE_SYNTAX, match => token + match);
|
|
775
|
+
// A `!` right before a link would make the link an image.
|
|
776
|
+
if (text.endsWith('!') && $isLinkNode(node.getNextSibling())) {
|
|
777
|
+
text = text.slice(0, -1) + token + '!';
|
|
778
|
+
}
|
|
779
|
+
// A `(` right after an image that ends in `]` would become its
|
|
780
|
+
// destination.
|
|
781
|
+
const before = node.getPreviousSibling();
|
|
782
|
+
if (
|
|
783
|
+
text.startsWith('(') &&
|
|
784
|
+
before != null &&
|
|
785
|
+
$isImageSource(before) &&
|
|
786
|
+
before.getTextContent().endsWith(']')
|
|
787
|
+
) {
|
|
788
|
+
text = token + text;
|
|
789
|
+
}
|
|
762
790
|
const previous = node.getPreviousSibling();
|
|
763
791
|
if (previous == null || $isLineBreakNode(previous)) {
|
|
764
792
|
const ordered = ORDERED_LIST_START.exec(text);
|
|
@@ -798,6 +826,22 @@ function markedView(
|
|
|
798
826
|
return node;
|
|
799
827
|
}
|
|
800
828
|
|
|
829
|
+
// A reference image's label at the end of its source: `[b]` in `![a][b]`.
|
|
830
|
+
const IMAGE_LABEL_END = /\]\[(?:[^\\\]]|\\.)*\]$/;
|
|
831
|
+
|
|
832
|
+
/**
|
|
833
|
+
* An image's source as an export writes it. A shortcut reference image
|
|
834
|
+
* (`![a]`) right before a link writes in its collapsed form (`![a][]`), so
|
|
835
|
+
* the link's text never reads as its label.
|
|
836
|
+
*/
|
|
837
|
+
function $imageSourceForExport(node: LexicalNode): string {
|
|
838
|
+
const source = node.getTextContent();
|
|
839
|
+
const isShortcut = source.endsWith(']') && !IMAGE_LABEL_END.test(source);
|
|
840
|
+
return isShortcut && $isLinkNode(node.getNextSibling())
|
|
841
|
+
? `${source}[]`
|
|
842
|
+
: source;
|
|
843
|
+
}
|
|
844
|
+
|
|
801
845
|
/**
|
|
802
846
|
* A placeholder for each inline plugin node among `nodes`, absent from
|
|
803
847
|
* `text`, and the source each placeholder stands for.
|
|
@@ -813,13 +857,19 @@ function extensionPlaceholders(
|
|
|
813
857
|
const sources = new Map<string, string>();
|
|
814
858
|
const free = absentCharacters(text);
|
|
815
859
|
for (const node of nodes) {
|
|
816
|
-
|
|
860
|
+
const source =
|
|
861
|
+
$isRichTextExtensionNode(node) && node.isInline()
|
|
862
|
+
? node.getSource()
|
|
863
|
+
: $isImageSource(node)
|
|
864
|
+
? $imageSourceForExport(node)
|
|
865
|
+
: null;
|
|
866
|
+
if (source != null) {
|
|
817
867
|
const placeholder = free.next().value;
|
|
818
868
|
if (placeholder == null) {
|
|
819
869
|
break;
|
|
820
870
|
}
|
|
821
871
|
placeholders.set(node.getKey(), placeholder);
|
|
822
|
-
sources.set(placeholder,
|
|
872
|
+
sources.set(placeholder, source);
|
|
823
873
|
}
|
|
824
874
|
}
|
|
825
875
|
return {placeholders, sources};
|