Desktop: Fixes #14542: Fix Prevent unclosed frontmatter from breaking Markdown rendering (#14563)

Signed-off-by: justin212407 <charlesjustin2124@gmail.com>
This commit is contained in:
Justin Charles
2026-03-10 17:01:38 +05:30
committed by GitHub
parent d046bfa14b
commit f05fe5754d
2 changed files with 38 additions and 29 deletions

View File

@@ -54,6 +54,17 @@ describe('MarkdownFrontMatterExtension', () => {
expect(frontMatterNodes.length).toBe(0);
});
it('should treat the entire document as frontmatter when closing delimiter is missing (issue #14542)', async () => {
const documentText = '---\nsome: frontmatter\n--\n\n# Hey';
const editor = await createEditorState(documentText, [frontMatterTagName]);
const frontMatterNodes = findNodesWithName(editor, frontMatterTagName);
// Frontmatter block must be recognised and span the entire document
expect(frontMatterNodes.length).toBe(1);
expect(frontMatterNodes[0].from).toBe(0);
expect(frontMatterNodes[0].to).toBe(documentText.length);
});
it('should handle empty FrontMatter block', async () => {
const documentText = '---\n---\n\n# Heading';
const editor = await createEditorState(documentText, [frontMatterTagName, 'ATXHeading1']);

View File

@@ -66,14 +66,15 @@ const frontMatterConfig: MarkdownConfig = {
return false;
}
// Store the opening delimiter position
// If the document starts with --- always claim it as a frontmatter block,
// even when the closing delimiter is absent.
const openingMarkerStart = cx.lineStart;
const openingMarkerEnd = cx.lineStart + line.text.length;
const contentStart = openingMarkerEnd + 1;
const contentStart = openingMarkerEnd + 1; // Start after the opening --- and newline
let foundEnd = false;
// Consume lines until we find the closing ---
// Consume lines until we find the closing --- or reach end of document.
while (cx.nextLine()) {
if (frontMatterDelimiterRegex.test(line.text)) {
foundEnd = true;
@@ -81,37 +82,34 @@ const frontMatterConfig: MarkdownConfig = {
}
}
if (!foundEnd) {
// No closing delimiter found - not a valid FrontMatter block
return false;
}
// cx.lineStart now points to the closing --- (if found) or end of document (if not).
const contentEnd = cx.lineStart;
// The content is between the two --- delimiters
const contentEnd = cx.lineStart; // Start of the closing --- line
// Closing delimiter positions
const closingMarkerStart = cx.lineStart;
const closingMarkerEnd = cx.lineStart + line.text.length;
// Create marker elements for the --- delimiters
const openingMarkerElem = cx.elt(frontMatterMarkerTagName, openingMarkerStart, openingMarkerEnd);
const closingMarkerElem = cx.elt(frontMatterMarkerTagName, closingMarkerStart, closingMarkerEnd);
// Create the content element (the YAML content between delimiters)
const contentElem = cx.elt(frontMatterContentTagName, contentStart, contentEnd);
// Create the container element spanning from start of first --- to end of last ---
const containerElement = cx.elt(
frontMatterTagName,
0, // Start at document beginning
closingMarkerEnd, // End after closing ---
[openingMarkerElem, contentElem, closingMarkerElem],
);
if (foundEnd) {
const closingMarkerStart = cx.lineStart;
const closingMarkerEnd = cx.lineStart + line.text.length;
const closingMarkerElem = cx.elt(frontMatterMarkerTagName, closingMarkerStart, closingMarkerEnd);
cx.addElement(containerElement);
// Move past the closing delimiter
cx.nextLine();
const containerElement = cx.elt(
frontMatterTagName,
0,
closingMarkerEnd,
[openingMarkerElem, contentElem, closingMarkerElem],
);
cx.addElement(containerElement);
cx.nextLine();
} else {
const containerElement = cx.elt(
frontMatterTagName,
0,
contentEnd,
[openingMarkerElem, contentElem],
);
cx.addElement(containerElement);
}
return true;
},