Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(md): fix space on empty line – add conversionOnly flag to appropriate markdown transformers (#1408)

Seamusbbe52fe9b443
diff --git a/js/app/packages/core/component/LexicalMarkdown/plugins/markdown-shortcuts/markdownShortcutsPlugin.ts b/js/app/packages/core/component/LexicalMarkdown/plugins/markdown-shortcuts/markdownShortcutsPlugin.ts
index 8ae2fda43b..5403e5edfe 100644
--- a/js/app/packages/core/component/LexicalMarkdown/plugins/markdown-shortcuts/markdownShortcutsPlugin.ts
+++ b/js/app/packages/core/component/LexicalMarkdown/plugins/markdown-shortcuts/markdownShortcutsPlugin.ts
@@ -1,3 +1,4 @@
+import { isConversionOnlyTransformer } from '@lexical-core';
import { registerMarkdownShortcuts, type Transformer } from '@lexical/markdown';
import { mergeRegister } from '@lexical/utils';
import {
@@ -34,6 +35,7 @@ function registerMarkdownShortcutsPlugins(
// Not all editor flavors support all nodes. Filter the available markdown shortcuts
// to only those with all dependencies available.
const transformers = props.transformers.filter((transformer) => {
+ if (isConversionOnlyTransformer(transformer)) return false;
if (
transformer.type === 'element' ||
transformer.type === 'multiline-element'
diff --git a/js/app/packages/lexical-core/transformers/index.ts b/js/app/packages/lexical-core/transformers/index.ts
index 3f2947761c..99a0c00265 100644
--- a/js/app/packages/lexical-core/transformers/index.ts
+++ b/js/app/packages/lexical-core/transformers/index.ts
@@ -29,6 +29,7 @@ import {
BR_TAG_TO_LINE_BREAK,
HR,
HTML_ENTITY_TRANSFORMERS,
+ isConversionOnlyTransformer,
LINK_XML,
MARK_XML,
PRESERVE_LINES,
@@ -37,6 +38,8 @@ import {
import { UNKNOWN_MENTION } from './unknownMention';
import { E_WATERMARK, I_WATERMARK } from './watermark';
+export { isConversionOnlyTransformer };
+
/**
* Internal transformers for converting markdown between Lexical state and markdown.
* The internal format uses XML-based syntax to represent nodes that fall outside of
diff --git a/js/app/packages/lexical-core/transformers/transformers.ts b/js/app/packages/lexical-core/transformers/transformers.ts
index 04f925d0ec..cec9dd2dda 100644
--- a/js/app/packages/lexical-core/transformers/transformers.ts
+++ b/js/app/packages/lexical-core/transformers/transformers.ts
@@ -11,6 +11,8 @@ import {
import { $createMarkNode, MarkNode } from '@lexical/mark';
import type {
ElementTransformer,
+ MultilineElementTransformer,
+ TextFormatTransformer,
TextMatchTransformer,
} from '@lexical/markdown';
import {
@@ -39,8 +41,24 @@ export function xmlMatcher(tag: string, flags?: string) {
return new RegExp(`<${tag}>(.*?)<\/${tag}>`, flags ?? 's');
}
+type LexicalTransformer =
+ | ElementTransformer
+ | MultilineElementTransformer
+ | TextMatchTransformer
+ | TextFormatTransformer;
+
+export type ConversionOnlyTransformer<T extends LexicalTransformer> = T & {
+ conversionOnly: true;
+};
+
+export function isConversionOnlyTransformer<T extends LexicalTransformer>(
+ transformer: T
+): transformer is ConversionOnlyTransformer<T> {
+ return 'conversionOnly' in transformer;
+}
+
// See https://github.com/facebook/lexical/issues/4271/ for the reasoning behind this.
-export const PRESERVE_LINES: ElementTransformer = {
+export const PRESERVE_LINES: ConversionOnlyTransformer<ElementTransformer> = {
type: 'element',
dependencies: [ParagraphNode],
export: (node) => {
@@ -58,45 +76,50 @@ export const PRESERVE_LINES: ElementTransformer = {
textNode.append($createTextNode(''));
}
},
+ conversionOnly: true,
};
// Strip br tags from external markdown.
-export const BR_TAG_TO_SPACE: TextMatchTransformer = {
- dependencies: [],
- export: () => {
- return null;
- },
- importRegExp: /<br\s*\/?>/i,
- regExp: /<br\s*\/?>/i,
- replace: (textNode, _) => {
- const fullText = textNode.getTextContent();
- const replacement = ' ';
- const newText = fullText.replace(/<br\s*\/?>/gi, replacement);
- if (newText !== fullText) {
- textNode.setTextContent(newText);
- }
- return textNode;
- },
- type: 'text-match',
-};
+export const BR_TAG_TO_SPACE: ConversionOnlyTransformer<TextMatchTransformer> =
+ {
+ dependencies: [],
+ export: () => {
+ return null;
+ },
+ importRegExp: /<br\s*\/?>/i,
+ regExp: /<br\s*\/?>/i,
+ replace: (textNode, _) => {
+ const fullText = textNode.getTextContent();
+ const replacement = ' ';
+ const newText = fullText.replace(/<br\s*\/?>/gi, replacement);
+ if (newText !== fullText) {
+ textNode.setTextContent(newText);
+ }
+ return textNode;
+ },
+ type: 'text-match',
+ conversionOnly: true,
+ };
-export const BR_TAG_TO_LINE_BREAK: TextMatchTransformer = {
- dependencies: [LineBreakNode],
- export: () => {
- return null;
- },
- importRegExp: /<br\s*\/?>/i,
- regExp: /<br\s*\/?>/i,
- replace: (textNode, _) => {
- textNode.replace($createLineBreakNode());
- },
- type: 'text-match',
-};
+export const BR_TAG_TO_LINE_BREAK: ConversionOnlyTransformer<TextMatchTransformer> =
+ {
+ dependencies: [LineBreakNode],
+ export: () => {
+ return null;
+ },
+ importRegExp: /<br\s*\/?>/i,
+ regExp: /<br\s*\/?>/i,
+ replace: (textNode, _) => {
+ textNode.replace($createLineBreakNode());
+ },
+ type: 'text-match',
+ conversionOnly: true,
+ };
function createEntityToUnicodeTransformer(
entity: string,
unicodeChar: string
-): TextMatchTransformer {
+): ConversionOnlyTransformer<TextMatchTransformer> {
const safeEntity = entity.replace(/([.*+?^=!:${}()|[\]/\\])/g, '\\$1');
const re = new RegExp(safeEntity, 'i');
return {
@@ -107,6 +130,7 @@ function createEntityToUnicodeTransformer(
textNode.replace($createTextNode(unicodeChar));
},
type: 'text-match',
+ conversionOnly: true,
};
}