Skip to content

Commit 827c75a

Browse files
authored
fix(richtext-lexical): formatted inline code resulted in incorrect markdown export (#10413)
If the following markdown: ```md **`text`** ``` was imported and then re-exported, the result was ```md `**text**` ``` Which would have been rendered as ```html <code>**text**</code> ``` Instead of ```html <strong><code>text</code></strong> ```
1 parent 5991a2e commit 827c75a

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

packages/richtext-lexical/src/packages/@lexical/markdown/MarkdownExport.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,20 @@ export function createMarkdownExport(
3333

3434
// Export only uses text formats that are responsible for single format
3535
// e.g. it will filter out *** (bold, italic) and instead use separate ** and *
36-
const textFormatTransformers = byType.textFormat.filter(
37-
(transformer) => transformer.format.length === 1,
38-
)
36+
const textFormatTransformers = byType.textFormat
37+
.filter((transformer) => transformer.format.length === 1)
38+
// Make sure all text transformers that contain 'code' in their format are at the end of the array. Otherwise, formatted code like
39+
// <strong><code>code</code></strong> will be exported as `**Bold Code**`, as the code format will be applied first, and the bold format
40+
// will be applied second and thus skipped entirely, as the code format will prevent any further formatting.
41+
.sort((a, b) => {
42+
if (a.format.includes('code') && !b.format.includes('code')) {
43+
return 1
44+
} else if (!a.format.includes('code') && b.format.includes('code')) {
45+
return -1
46+
} else {
47+
return 0
48+
}
49+
})
3950

4051
return (node) => {
4152
const output = []

0 commit comments

Comments
 (0)