testomatio-editor-blocks 0.4.50 → 0.4.51
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/package/editor/blocks/markdown.js +1 -1
- package/package/editor/blocks/stepField.js +1 -1
- package/package/editor/createMarkdownPasteHandler.js +18 -0
- package/package/editor/customMarkdownConverter.js +5 -7
- package/package/styles.css +4 -0
- package/package.json +1 -1
- package/src/editor/blocks/markdown.ts +1 -1
- package/src/editor/blocks/stepField.tsx +1 -1
- package/src/editor/blocks/stepFieldFormatting.test.ts +2 -2
- package/src/editor/createMarkdownPasteHandler.ts +16 -0
- package/src/editor/customMarkdownConverter.test.ts +3 -3
- package/src/editor/customMarkdownConverter.ts +5 -7
- package/src/editor/styles.css +4 -0
|
@@ -107,7 +107,7 @@ function fallbackHtmlToMarkdown(html) {
|
|
|
107
107
|
.replace(/<br\s*\/?>/gi, "\n")
|
|
108
108
|
.replace(/<\/?(div|p)>/gi, "\n")
|
|
109
109
|
.replace(/<strong>(.*?)<\/strong>/gis, (_m, content) => `**${content}**`)
|
|
110
|
-
.replace(/<(em|i)>(.*?)<\/(em|i)>/gis, (_m, _tag, content) =>
|
|
110
|
+
.replace(/<(em|i)>(.*?)<\/(em|i)>/gis, (_m, _tag, content) => `_${content}_`)
|
|
111
111
|
.replace(/<span[^>]*>/gi, "")
|
|
112
112
|
.replace(/<\/span>/gi, "")
|
|
113
113
|
.replace(/<u>(.*?)<\/u>/gis, (_m, content) => `<u>${content}</u>`);
|
|
@@ -185,7 +185,7 @@ export function buildFullMarkdown(plainText, links, formatting) {
|
|
|
185
185
|
closeMarker = isMultiline ? "\n```" : "`";
|
|
186
186
|
}
|
|
187
187
|
else {
|
|
188
|
-
openMarker = fmt.type === "bold" ? "**" : "
|
|
188
|
+
openMarker = fmt.type === "bold" ? "**" : "_";
|
|
189
189
|
closeMarker = openMarker;
|
|
190
190
|
}
|
|
191
191
|
// Opening: outer markers (bold) before inner (italic) → bold order=0, italic order=1
|
|
@@ -1,3 +1,18 @@
|
|
|
1
|
+
const BLOCK_MARKDOWN_PREFIX = /^(\s*)(#{1,6}\s|[-*+]\s|\d+[.)]\s|>\s|```|~~~|\||!\[)/;
|
|
2
|
+
function isInlineOnlyPaste(plainText, parsedBlocks) {
|
|
3
|
+
if (parsedBlocks.length !== 1)
|
|
4
|
+
return false;
|
|
5
|
+
const [block] = parsedBlocks;
|
|
6
|
+
if (block.type !== "paragraph")
|
|
7
|
+
return false;
|
|
8
|
+
if (block.children && block.children.length > 0)
|
|
9
|
+
return false;
|
|
10
|
+
if (/\r?\n/.test(plainText))
|
|
11
|
+
return false;
|
|
12
|
+
if (BLOCK_MARKDOWN_PREFIX.test(plainText))
|
|
13
|
+
return false;
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
1
16
|
export function createMarkdownPasteHandler(converter) {
|
|
2
17
|
return ({ event, editor, defaultPasteHandler }) => {
|
|
3
18
|
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
@@ -21,6 +36,9 @@ export function createMarkdownPasteHandler(converter) {
|
|
|
21
36
|
const parsedBlocks = converter(plainText);
|
|
22
37
|
if (parsedBlocks.length === 0)
|
|
23
38
|
return defaultPasteHandler();
|
|
39
|
+
if (isInlineOnlyPaste(plainText, parsedBlocks)) {
|
|
40
|
+
return defaultPasteHandler({ plainTextAsMarkdown: false });
|
|
41
|
+
}
|
|
24
42
|
const selection = editor.getSelection();
|
|
25
43
|
const selectedIds = (_h = (_g = selection === null || selection === void 0 ? void 0 : selection.blocks) === null || _g === void 0 ? void 0 : _g.map((block) => block.id).filter((id) => Boolean(id))) !== null && _h !== void 0 ? _h : [];
|
|
26
44
|
if (selectedIds.length > 0) {
|
|
@@ -100,7 +100,7 @@ function applyTextStyles(text, styles) {
|
|
|
100
100
|
wrappers.push({ prefix: "**", suffix: "**" });
|
|
101
101
|
}
|
|
102
102
|
if (styles.italic) {
|
|
103
|
-
wrappers.push({ prefix: "
|
|
103
|
+
wrappers.push({ prefix: "_", suffix: "_" });
|
|
104
104
|
}
|
|
105
105
|
if (styles.strike) {
|
|
106
106
|
wrappers.push({ prefix: "~~", suffix: "~~" });
|
|
@@ -511,10 +511,7 @@ function serializeBlocks(blocks, ctx) {
|
|
|
511
511
|
}
|
|
512
512
|
export function blocksToMarkdown(blocks) {
|
|
513
513
|
const lines = serializeBlocks(blocks, { listDepth: 0, insideQuote: false });
|
|
514
|
-
const cleaned = lines
|
|
515
|
-
.join("\n")
|
|
516
|
-
.replace(/\n{3,}/g, "\n\n")
|
|
517
|
-
.trimEnd();
|
|
514
|
+
const cleaned = lines.join("\n").trimEnd();
|
|
518
515
|
return cleaned;
|
|
519
516
|
}
|
|
520
517
|
function parseInlineMarkdown(text) {
|
|
@@ -592,8 +589,9 @@ function parseInlineMarkdown(text) {
|
|
|
592
589
|
continue;
|
|
593
590
|
}
|
|
594
591
|
}
|
|
595
|
-
if (cleaned
|
|
596
|
-
const
|
|
592
|
+
if (cleaned[i] === "*" || cleaned[i] === "_") {
|
|
593
|
+
const marker = cleaned[i];
|
|
594
|
+
const end = cleaned.indexOf(marker, i + 1);
|
|
597
595
|
if (end !== -1) {
|
|
598
596
|
pushPlain();
|
|
599
597
|
const inner = cleaned.slice(i + 1, end);
|
package/package/styles.css
CHANGED
package/package.json
CHANGED
|
@@ -140,7 +140,7 @@ function fallbackHtmlToMarkdown(html: string): string {
|
|
|
140
140
|
.replace(/<br\s*\/?>/gi, "\n")
|
|
141
141
|
.replace(/<\/?(div|p)>/gi, "\n")
|
|
142
142
|
.replace(/<strong>(.*?)<\/strong>/gis, (_m, content) => `**${content}**`)
|
|
143
|
-
.replace(/<(em|i)>(.*?)<\/(em|i)>/gis, (_m, _tag, content) =>
|
|
143
|
+
.replace(/<(em|i)>(.*?)<\/(em|i)>/gis, (_m, _tag, content) => `_${content}_`)
|
|
144
144
|
.replace(/<span[^>]*>/gi, "")
|
|
145
145
|
.replace(/<\/span>/gi, "")
|
|
146
146
|
.replace(/<u>(.*?)<\/u>/gis, (_m, content) => `<u>${content}</u>`);
|
|
@@ -286,7 +286,7 @@ export function buildFullMarkdown(plainText: string, links: LinkMeta[], formatti
|
|
|
286
286
|
openMarker = isMultiline ? "```\n" : "`";
|
|
287
287
|
closeMarker = isMultiline ? "\n```" : "`";
|
|
288
288
|
} else {
|
|
289
|
-
openMarker = fmt.type === "bold" ? "**" : "
|
|
289
|
+
openMarker = fmt.type === "bold" ? "**" : "_";
|
|
290
290
|
closeMarker = openMarker;
|
|
291
291
|
}
|
|
292
292
|
// Opening: outer markers (bold) before inner (italic) → bold order=0, italic order=1
|
|
@@ -9,7 +9,7 @@ describe("buildFullMarkdown formatting combinations", () => {
|
|
|
9
9
|
{ start: 0, end: 5, type: "bold" },
|
|
10
10
|
{ start: 0, end: 5, type: "italic" },
|
|
11
11
|
];
|
|
12
|
-
expect(buildFullMarkdown("hello", noLinks, formatting)).toBe("
|
|
12
|
+
expect(buildFullMarkdown("hello", noLinks, formatting)).toBe("_**hello**_");
|
|
13
13
|
});
|
|
14
14
|
|
|
15
15
|
it("preserves word-level bold when sentence-level bold is applied", () => {
|
|
@@ -31,7 +31,7 @@ describe("buildFullMarkdown formatting combinations", () => {
|
|
|
31
31
|
{ start: 0, end: 5, type: "bold" },
|
|
32
32
|
{ start: 0, end: 11, type: "italic" },
|
|
33
33
|
];
|
|
34
|
-
expect(buildFullMarkdown(text, noLinks, formatting)).toBe("
|
|
34
|
+
expect(buildFullMarkdown(text, noLinks, formatting)).toBe("_**hello** world_");
|
|
35
35
|
});
|
|
36
36
|
|
|
37
37
|
it("code formatting removes bold and italic", () => {
|
|
@@ -10,6 +10,18 @@ type PasteHandlerContext = {
|
|
|
10
10
|
}) => boolean | undefined;
|
|
11
11
|
};
|
|
12
12
|
|
|
13
|
+
const BLOCK_MARKDOWN_PREFIX = /^(\s*)(#{1,6}\s|[-*+]\s|\d+[.)]\s|>\s|```|~~~|\||!\[)/;
|
|
14
|
+
|
|
15
|
+
function isInlineOnlyPaste(plainText: string, parsedBlocks: CustomPartialBlock[]): boolean {
|
|
16
|
+
if (parsedBlocks.length !== 1) return false;
|
|
17
|
+
const [block] = parsedBlocks;
|
|
18
|
+
if (block.type !== "paragraph") return false;
|
|
19
|
+
if (block.children && block.children.length > 0) return false;
|
|
20
|
+
if (/\r?\n/.test(plainText)) return false;
|
|
21
|
+
if (BLOCK_MARKDOWN_PREFIX.test(plainText)) return false;
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
|
|
13
25
|
export function createMarkdownPasteHandler(
|
|
14
26
|
converter: (markdown: string) => CustomPartialBlock[],
|
|
15
27
|
) {
|
|
@@ -34,6 +46,10 @@ export function createMarkdownPasteHandler(
|
|
|
34
46
|
const parsedBlocks = converter(plainText);
|
|
35
47
|
if (parsedBlocks.length === 0) return defaultPasteHandler();
|
|
36
48
|
|
|
49
|
+
if (isInlineOnlyPaste(plainText, parsedBlocks)) {
|
|
50
|
+
return defaultPasteHandler({ plainTextAsMarkdown: false });
|
|
51
|
+
}
|
|
52
|
+
|
|
37
53
|
const selection = editor.getSelection();
|
|
38
54
|
const selectedIds = selection?.blocks
|
|
39
55
|
?.map((block: any) => block.id)
|
|
@@ -43,7 +43,7 @@ describe("blocksToMarkdown", () => {
|
|
|
43
43
|
},
|
|
44
44
|
];
|
|
45
45
|
|
|
46
|
-
expect(blocksToMarkdown(blocks)).toBe("Hello **world
|
|
46
|
+
expect(blocksToMarkdown(blocks)).toBe("Hello **world**_!_");
|
|
47
47
|
});
|
|
48
48
|
|
|
49
49
|
it("places bold markers outside leading/trailing spaces", () => {
|
|
@@ -76,7 +76,7 @@ describe("blocksToMarkdown", () => {
|
|
|
76
76
|
children: [],
|
|
77
77
|
},
|
|
78
78
|
];
|
|
79
|
-
expect(blocksToMarkdown(blocks)).toBe("
|
|
79
|
+
expect(blocksToMarkdown(blocks)).toBe("_word_ next");
|
|
80
80
|
});
|
|
81
81
|
|
|
82
82
|
it("places code backticks outside leading/trailing spaces", () => {
|
|
@@ -301,7 +301,7 @@ describe("blocksToMarkdown", () => {
|
|
|
301
301
|
];
|
|
302
302
|
|
|
303
303
|
const markdown = blocksToMarkdown(blocks);
|
|
304
|
-
expect(markdown).toBe("
|
|
304
|
+
expect(markdown).toBe("_**text**_");
|
|
305
305
|
});
|
|
306
306
|
|
|
307
307
|
it("keeps inline formatting inside step fields", () => {
|
|
@@ -152,7 +152,7 @@ function applyTextStyles(text: string, styles: EditorStyles | undefined): string
|
|
|
152
152
|
}
|
|
153
153
|
|
|
154
154
|
if (styles.italic) {
|
|
155
|
-
wrappers.push({ prefix: "
|
|
155
|
+
wrappers.push({ prefix: "_", suffix: "_" });
|
|
156
156
|
}
|
|
157
157
|
|
|
158
158
|
if (styles.strike) {
|
|
@@ -620,10 +620,7 @@ function serializeBlocks(blocks: CustomEditorBlock[], ctx: MarkdownContext): str
|
|
|
620
620
|
|
|
621
621
|
export function blocksToMarkdown(blocks: CustomEditorBlock[]): string {
|
|
622
622
|
const lines = serializeBlocks(blocks, { listDepth: 0, insideQuote: false });
|
|
623
|
-
const cleaned = lines
|
|
624
|
-
.join("\n")
|
|
625
|
-
.replace(/\n{3,}/g, "\n\n")
|
|
626
|
-
.trimEnd();
|
|
623
|
+
const cleaned = lines.join("\n").trimEnd();
|
|
627
624
|
|
|
628
625
|
return cleaned;
|
|
629
626
|
}
|
|
@@ -709,8 +706,9 @@ function parseInlineMarkdown(text: string): EditorInline[] {
|
|
|
709
706
|
}
|
|
710
707
|
}
|
|
711
708
|
|
|
712
|
-
if (cleaned
|
|
713
|
-
const
|
|
709
|
+
if (cleaned[i] === "*" || cleaned[i] === "_") {
|
|
710
|
+
const marker = cleaned[i];
|
|
711
|
+
const end = cleaned.indexOf(marker, i + 1);
|
|
714
712
|
if (end !== -1) {
|
|
715
713
|
pushPlain();
|
|
716
714
|
const inner = cleaned.slice(i + 1, end);
|