diff --git a/packages/core/src/api/exporters/markdown/htmlToMarkdown.test.ts b/packages/core/src/api/exporters/markdown/htmlToMarkdown.test.ts
new file mode 100644
index 0000000000..b12caabb90
--- /dev/null
+++ b/packages/core/src/api/exporters/markdown/htmlToMarkdown.test.ts
@@ -0,0 +1,24 @@
+import { describe, expect, it } from "vite-plus/test";
+import { markdownToHtml } from "../../parsers/markdown/markdownToHtml.js";
+import { htmlToMarkdown } from "./htmlToMarkdown.js";
+
+describe("htmlToMarkdown", () => {
+ // A hard break in a heading or a list item is a `
` tag. A backslash
+ // directly before the tag would escape its `<`.
+ it.each(["
a\\
b
", ""])(
+ "keeps a backslash directly before a hard break in %s",
+ (html) => {
+ expect(markdownToHtml(htmlToMarkdown(html))).toBe(html);
+ },
+ );
+
+ // The markdown parser reads a line without this indent as a new paragraph
+ // after the list. BlockNote blocks give this HTML only for a toggle list
+ // item (the paragraph is then in a `details` element), so this test uses
+ // HTML.
+ it("indents the lines after a hard break in a paragraph of a list item", () => {
+ expect(htmlToMarkdown("")).toBe(
+ "* a\n\n b\\\n c\n",
+ );
+ });
+});
diff --git a/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts b/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts
index 531724bb87..44be7074dc 100644
--- a/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts
+++ b/packages/core/src/api/exporters/markdown/htmlToMarkdown.ts
@@ -123,7 +123,7 @@ function serializeParagraph(el: HTMLElement, ctx: SerializeContext): string {
function serializeHeading(el: HTMLElement, ctx: SerializeContext): string {
const level = parseInt(el.tagName[1], 10);
const prefix = "#".repeat(level) + " ";
- const content = serializeInlineContent(el);
+ const content = toSingleLine(serializeInlineContent(el));
return ctx.indent + prefix + content + "\n\n";
}
@@ -337,7 +337,7 @@ function serializeListItem(
// produce a "tight" list (no blank line between markers). Continuation
// content within the item (nested lists, continuation paragraphs, other
// blocks) injects its own spacing as needed.
- let result = ctx.indent + marker + inlineContent + "\n";
+ let result = ctx.indent + marker + toSingleLine(inlineContent) + "\n";
// Serialize child content (nested lists, continuation paragraphs, etc.)
const childIndent = ctx.indent + " ".repeat(markerWidth);
@@ -356,7 +356,7 @@ function serializeListItem(
// Continuation paragraph needs a blank line to separate it from the
// previous content; CommonMark would otherwise treat it as a soft
// wrap of that content.
- result += "\n" + childIndent + content + "\n";
+ result += "\n" + childIndent + indentLines(content, childIndent) + "\n";
} else {
result += serializeNode(child, childCtx);
}
@@ -383,7 +383,7 @@ function serializeListItem(
// Continuation paragraph within list item — requires blank line before
// so it isn't read as part of the marker line's text.
const content = serializeInlineContent(child as HTMLElement);
- result += "\n" + childIndent + content + "\n";
+ result += "\n" + childIndent + indentLines(content, childIndent) + "\n";
} else {
// Other block-level children (code blocks, blockquotes, etc.) already
// emit their own separating newlines; prefix with a blank line so they
@@ -395,6 +395,15 @@ function serializeListItem(
return result;
}
+/**
+ * Puts the indent at the start of each line after the first line. Without
+ * this indent, the markdown parser does not read the lines after a hard break
+ * as a part of a paragraph in a list item.
+ */
+function indentLines(content: string, indent: string): string {
+ return content.split("\n").join("\n" + indent);
+}
+
function getFirstContentElement(
li: HTMLElement,
checkbox: HTMLInputElement | null,
@@ -754,6 +763,23 @@ function serializeDetails(el: HTMLElement, ctx: SerializeContext): string {
// ─── Inline Content Serializer ───────────────────────────────────────────────
+/**
+ * Puts the text of a block on one line, for a block whose text must be one
+ * line in markdown: a heading or a list item. A backslash hard break needs a
+ * second line, and the markdown parser does not read that line as text of the
+ * block. Thus each hard break becomes the HTML `
` tag, which the parser
+ * reads as a hard break.
+ */
+function toSingleLine(content: string): string {
+ // `serializeInlineContent` writes a hard break as a backslash and a line
+ // end. Backslashes directly before the tag are doubled, so that they do not
+ // escape its `<`.
+ return content.replace(
+ /(\\*)\\\n/g,
+ (_, backslashes: string) => `${backslashes}${backslashes}
`,
+ );
+}
+
function serializeInlineContent(el: Element): string {
let result = "";
diff --git a/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/hardBreakBelowCheckListItem.json b/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/hardBreakBelowCheckListItem.json
new file mode 100644
index 0000000000..e2920ca0e9
--- /dev/null
+++ b/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/hardBreakBelowCheckListItem.json
@@ -0,0 +1,114 @@
+[
+ {
+ "children": [
+ {
+ "children": [
+ {
+ "children": [],
+ "content": [
+ {
+ "styles": {},
+ "text": "Nested 1
+Nested 2",
+ "type": "text",
+ },
+ ],
+ "id": "3",
+ "props": {
+ "backgroundColor": "default",
+ "textAlignment": "left",
+ "textColor": "default",
+ },
+ "type": "bulletListItem",
+ },
+ ],
+ "content": [
+ {
+ "styles": {},
+ "text": "Bullet 1
+Bullet 2",
+ "type": "text",
+ },
+ ],
+ "id": "2",
+ "props": {
+ "backgroundColor": "default",
+ "textAlignment": "left",
+ "textColor": "default",
+ },
+ "type": "bulletListItem",
+ },
+ {
+ "children": [],
+ "content": [
+ {
+ "styles": {},
+ "text": "Check 1
+Check 2",
+ "type": "text",
+ },
+ ],
+ "id": "4",
+ "props": {
+ "backgroundColor": "default",
+ "checked": false,
+ "textAlignment": "left",
+ "textColor": "default",
+ },
+ "type": "checkListItem",
+ },
+ ],
+ "content": [
+ {
+ "styles": {},
+ "text": "Check A",
+ "type": "text",
+ },
+ ],
+ "id": "1",
+ "props": {
+ "backgroundColor": "default",
+ "checked": false,
+ "textAlignment": "left",
+ "textColor": "default",
+ },
+ "type": "checkListItem",
+ },
+ {
+ "children": [
+ {
+ "children": [],
+ "content": [
+ {
+ "styles": {},
+ "text": "Numbered 1
+Numbered 2",
+ "type": "text",
+ },
+ ],
+ "id": "6",
+ "props": {
+ "backgroundColor": "default",
+ "textAlignment": "left",
+ "textColor": "default",
+ },
+ "type": "numberedListItem",
+ },
+ ],
+ "content": [
+ {
+ "styles": {},
+ "text": "Check B",
+ "type": "text",
+ },
+ ],
+ "id": "5",
+ "props": {
+ "backgroundColor": "default",
+ "checked": false,
+ "textAlignment": "left",
+ "textColor": "default",
+ },
+ "type": "checkListItem",
+ },
+]
\ No newline at end of file
diff --git a/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/hardBreakInHeading.json b/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/hardBreakInHeading.json
new file mode 100644
index 0000000000..b55324ebba
--- /dev/null
+++ b/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/hardBreakInHeading.json
@@ -0,0 +1,22 @@
+[
+ {
+ "children": [],
+ "content": [
+ {
+ "styles": {},
+ "text": "Line 1
+Line 2",
+ "type": "text",
+ },
+ ],
+ "id": "1",
+ "props": {
+ "backgroundColor": "default",
+ "isToggleable": false,
+ "level": 1,
+ "textAlignment": "left",
+ "textColor": "default",
+ },
+ "type": "heading",
+ },
+]
\ No newline at end of file
diff --git a/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/hardBreakInListItems.json b/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/hardBreakInListItems.json
new file mode 100644
index 0000000000..c2fe830dc0
--- /dev/null
+++ b/tests/src/unit/core/formatConversion/exportParseEquality/__snapshots__/markdown/markdown/hardBreakInListItems.json
@@ -0,0 +1,112 @@
+[
+ {
+ "children": [
+ {
+ "children": [],
+ "content": [
+ {
+ "styles": {},
+ "text": "Nested 1
+Nested 2",
+ "type": "text",
+ },
+ ],
+ "id": "2",
+ "props": {
+ "backgroundColor": "default",
+ "textAlignment": "left",
+ "textColor": "default",
+ },
+ "type": "bulletListItem",
+ },
+ ],
+ "content": [
+ {
+ "styles": {},
+ "text": "Bullet 1
+Bullet 2",
+ "type": "text",
+ },
+ ],
+ "id": "1",
+ "props": {
+ "backgroundColor": "default",
+ "textAlignment": "left",
+ "textColor": "default",
+ },
+ "type": "bulletListItem",
+ },
+ {
+ "children": [],
+ "content": [
+ {
+ "styles": {},
+ "text": "Numbered 1
+Numbered 2",
+ "type": "text",
+ },
+ ],
+ "id": "3",
+ "props": {
+ "backgroundColor": "default",
+ "textAlignment": "left",
+ "textColor": "default",
+ },
+ "type": "numberedListItem",
+ },
+ {
+ "children": [],
+ "content": [
+ {
+ "styles": {},
+ "text": "Check 1
+Check 2",
+ "type": "text",
+ },
+ ],
+ "id": "4",
+ "props": {
+ "backgroundColor": "default",
+ "checked": false,
+ "textAlignment": "left",
+ "textColor": "default",
+ },
+ "type": "checkListItem",
+ },
+ {
+ "children": [
+ {
+ "children": [],
+ "content": [
+ {
+ "styles": {},
+ "text": "Child 1
+Child 2",
+ "type": "text",
+ },
+ ],
+ "id": "6",
+ "props": {
+ "backgroundColor": "default",
+ "textAlignment": "left",
+ "textColor": "default",
+ },
+ "type": "paragraph",
+ },
+ ],
+ "content": [
+ {
+ "styles": {},
+ "text": "Toggle",
+ "type": "text",
+ },
+ ],
+ "id": "5",
+ "props": {
+ "backgroundColor": "default",
+ "textAlignment": "left",
+ "textColor": "default",
+ },
+ "type": "bulletListItem",
+ },
+]
\ No newline at end of file
diff --git a/tests/src/unit/core/formatConversion/exportParseEquality/exportParseEqualityTestInstances.ts b/tests/src/unit/core/formatConversion/exportParseEquality/exportParseEqualityTestInstances.ts
index 7cc07ee5ac..b351380003 100644
--- a/tests/src/unit/core/formatConversion/exportParseEquality/exportParseEqualityTestInstances.ts
+++ b/tests/src/unit/core/formatConversion/exportParseEquality/exportParseEqualityTestInstances.ts
@@ -936,6 +936,157 @@ export const exportParseEqualityTestInstancesMarkdown: TestInstance<
},
executeTest: testExportParseEqualityMarkdown,
},
+ {
+ testCase: {
+ name: "markdown/hardBreakInHeading",
+ content: [
+ {
+ type: "heading",
+ content: [
+ {
+ type: "text",
+ text: "Line 1\nLine 2",
+ styles: {},
+ },
+ ],
+ },
+ ],
+ },
+ executeTest: testExportParseEqualityMarkdown,
+ },
+ {
+ testCase: {
+ name: "markdown/hardBreakInListItems",
+ content: [
+ {
+ type: "bulletListItem",
+ content: [
+ {
+ type: "text",
+ text: "Bullet 1\nBullet 2",
+ styles: {},
+ },
+ ],
+ children: [
+ {
+ type: "bulletListItem",
+ content: [
+ {
+ type: "text",
+ text: "Nested 1\nNested 2",
+ styles: {},
+ },
+ ],
+ },
+ ],
+ },
+ {
+ type: "numberedListItem",
+ content: [
+ {
+ type: "text",
+ text: "Numbered 1\nNumbered 2",
+ styles: {},
+ },
+ ],
+ },
+ {
+ type: "checkListItem",
+ content: [
+ {
+ type: "text",
+ text: "Check 1\nCheck 2",
+ styles: {},
+ },
+ ],
+ },
+ // Markdown has no toggle, so this comes back as a bullet list item.
+ {
+ type: "toggleListItem",
+ content: "Toggle",
+ children: [
+ {
+ type: "paragraph",
+ content: [
+ {
+ type: "text",
+ text: "Child 1\nChild 2",
+ styles: {},
+ },
+ ],
+ },
+ ],
+ },
+ ],
+ },
+ executeTest: testExportParseEqualityMarkdown,
+ },
+ // The parser reads a line below a check list item as a part of the item
+ // only if it starts a list item, or if it has the indent of the text after
+ // the checkbox. Thus the text of a list item below a check list item must
+ // be one line.
+ {
+ testCase: {
+ name: "markdown/hardBreakBelowCheckListItem",
+ content: [
+ {
+ type: "checkListItem",
+ content: "Check A",
+ children: [
+ {
+ type: "bulletListItem",
+ content: [
+ {
+ type: "text",
+ text: "Bullet 1\nBullet 2",
+ styles: {},
+ },
+ ],
+ children: [
+ {
+ type: "bulletListItem",
+ content: [
+ {
+ type: "text",
+ text: "Nested 1\nNested 2",
+ styles: {},
+ },
+ ],
+ },
+ ],
+ },
+ {
+ type: "checkListItem",
+ content: [
+ {
+ type: "text",
+ text: "Check 1\nCheck 2",
+ styles: {},
+ },
+ ],
+ },
+ ],
+ },
+ {
+ type: "checkListItem",
+ content: "Check B",
+ children: [
+ {
+ type: "numberedListItem",
+ content: [
+ {
+ type: "text",
+ text: "Numbered 1\nNumbered 2",
+ styles: {},
+ },
+ ],
+ },
+ ],
+ },
+ ],
+ },
+ executeTest: testExportParseEqualityMarkdown,
+ },
{
testCase: {
name: "markdown/mixedStyles",
diff --git a/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/listItemIndentedTextLine.json b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/listItemIndentedTextLine.json
new file mode 100644
index 0000000000..165c5d9e31
--- /dev/null
+++ b/tests/src/unit/core/formatConversion/parse/__snapshots__/markdown/listItemIndentedTextLine.json
@@ -0,0 +1,74 @@
+[
+ {
+ "children": [
+ {
+ "children": [],
+ "content": [
+ {
+ "styles": {},
+ "text": "b",
+ "type": "text",
+ },
+ ],
+ "id": "2",
+ "props": {
+ "backgroundColor": "default",
+ "textAlignment": "left",
+ "textColor": "default",
+ },
+ "type": "paragraph",
+ },
+ ],
+ "content": [
+ {
+ "styles": {},
+ "text": "a",
+ "type": "text",
+ },
+ ],
+ "id": "1",
+ "props": {
+ "backgroundColor": "default",
+ "textAlignment": "left",
+ "textColor": "default",
+ },
+ "type": "bulletListItem",
+ },
+ {
+ "children": [
+ {
+ "children": [],
+ "content": [
+ {
+ "styles": {},
+ "text": "Description",
+ "type": "text",
+ },
+ ],
+ "id": "4",
+ "props": {
+ "backgroundColor": "default",
+ "textAlignment": "left",
+ "textColor": "default",
+ },
+ "type": "paragraph",
+ },
+ ],
+ "content": [
+ {
+ "styles": {
+ "bold": true,
+ },
+ "text": "Title",
+ "type": "text",
+ },
+ ],
+ "id": "3",
+ "props": {
+ "backgroundColor": "default",
+ "textAlignment": "left",
+ "textColor": "default",
+ },
+ "type": "numberedListItem",
+ },
+]
\ No newline at end of file
diff --git a/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts b/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts
index b74fc499cb..950603e76f 100644
--- a/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts
+++ b/tests/src/unit/core/formatConversion/parse/parseTestInstances.ts
@@ -1777,6 +1777,20 @@ Line three`,
},
executeTest: testParseMarkdown,
},
+ // An indented text line directly after the line of a list item is a child
+ // paragraph. The markdown export does not need a different result: it
+ // writes a hard break in a list item as a `
` tag in the line of the
+ // item.
+ {
+ testCase: {
+ name: "listItemIndentedTextLine",
+ content: `- a
+ b
+1. **Title**
+ Description`,
+ },
+ executeTest: testParseMarkdown,
+ },
// Backslash escapes
{
testCase: {