Skip to content
Draft
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
24 changes: 24 additions & 0 deletions packages/core/src/api/exporters/markdown/htmlToMarkdown.test.ts
Original file line number Diff line number Diff line change
@@ -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 `<br>` tag. A backslash
// directly before the tag would escape its `<`.
it.each(["<h1>a\\<br>b</h1>", "<ul><li><p>a\\<br>b</p></li></ul>"])(
"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("<ul><li><p>a</p><p>b<br>c</p></li></ul>")).toBe(
"* a\n\n b\\\n c\n",
);
});
});
34 changes: 30 additions & 4 deletions packages/core/src/api/exporters/markdown/htmlToMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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 `<br>` 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}<br>`,
);
}

function serializeInlineContent(el: Element): string {
let result = "";

Expand Down
Original file line number Diff line number Diff line change
@@ -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",
},
]
Original file line number Diff line number Diff line change
@@ -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",
},
]
Original file line number Diff line number Diff line change
@@ -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",
},
]
Loading
Loading