Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@lexical/markdown Fix strikethrough TextFormatTransformer order #2304

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions packages/lexical-markdown/src/v2/MarkdownExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ function exportTextFormat(
let output = frozenString;

const applied = new Set();
let appliedOrder = [];

for (const transformer of textTransformers) {
const format = transformer.format[0];
Expand All @@ -139,21 +140,31 @@ function exportTextFormat(
if (hasFormat(node, format) && !applied.has(format)) {
// Multiple tags might be used for the same format (*, _)
applied.add(format);
// Add most recent textTransform to beginning of appliedOrder where
// we'll later iterate last-in-first-out when appending closing tags
appliedOrder = [transformer, ...appliedOrder];

// Prevent adding opening tag is already opened by the previous sibling
const previousNode = getTextSibling(node, true);

if (!hasFormat(previousNode, format)) {
output = tag + output;
}
}
}

appliedOrder.forEach((transformer) => {
const format = transformer.format[0];
const tag = transformer.tag;

if (hasFormat(node, format)) {
// Prevent adding closing tag if next sibling will do it
const nextNode = getTextSibling(node, false);

if (!hasFormat(nextNode, format)) {
output += tag;
}
}
}
});

// Replace trimmed version of textContent ensuring surrounding whitespace is not modified
return textContent.replace(frozenString, output);
Expand Down