Skip to content
Open
Show file tree
Hide file tree
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: 15 additions & 0 deletions packages/lexical-markdown/src/MarkdownTransformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ const ORDERED_LIST_REGEX = /^(\s*)(\d{1,})\.\s/;
const UNORDERED_LIST_REGEX = /^(\s*)[-*+]\s/;
const CHECK_LIST_REGEX = /^(\s*)(?:[-*+]\s)?\s?(\[(\s|x)?\])\s/i;
const HEADING_REGEX = /^(#{1,6})\s/;
const HEADING_CLOSING_SEQUENCE_REGEX = /(?:^|[ \t]+)#+[ \t]*$/;
const QUOTE_REGEX = /^>\s/;
const CODE_START_REGEX = /^([ \t]*`{3,})([\w-]+)?[ \t]?/;
const CODE_END_REGEX = /^[ \t]*`{3,}$/;
Expand Down Expand Up @@ -807,6 +808,20 @@ export const HEADING: ElementTransformer = {
if (!isImport && $isUnreplaceableBlock(parentNode)) {
return false;
}
if (isImport) {
// An ATX heading may end with a closing sequence of `#`s, which is not
// part of the content: `## Hello ##` is a heading reading "Hello".
// The sequence must be preceded by a space or tab (or be the whole
// content); `# C#` keeps its `#`.
const lastChild = children[children.length - 1];
if ($isTextNode(lastChild)) {
const text = lastChild.getTextContent();
const stripped = text.replace(HEADING_CLOSING_SEQUENCE_REGEX, '');
if (stripped !== text) {
lastChild.setTextContent(stripped);
}
}
}
return $replaceWithHeading(parentNode, children, match, isImport);
},
triggerOnEnter: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,23 @@ describe('Markdown', () => {
html: '<h6><span style="white-space: pre-wrap;">Hello world</span></h6>',
md: '###### Hello world',
},
{
// ATX closing sequence: https://spec.commonmark.org/0.31.2/#example-71
html: '<h2><span style="white-space: pre-wrap;">Hello world</span></h2>',
md: '## Hello world ##',
skipExport: true,
},
{
// The closing sequence may be any length and followed by spaces
html: '<h1><span style="white-space: pre-wrap;">Hello world</span></h1>',
md: '# Hello world ##### ',
skipExport: true,
},
{
// A `#` not preceded by a space is content, not a closing sequence
html: '<h1><span style="white-space: pre-wrap;">Hello C#</span></h1>',
md: '# Hello C#',
},
{
// Multiline paragraphs: https://spec.commonmark.org/dingus/?text=Hello%0Aworld%0A!
html: '<p><span style="white-space: pre-wrap;">Hello world !</span></p>',
Expand Down
Loading