Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

fix(core): keep pasted HTML bullet lists tight with dash markers (#2779)

Nick Gomez2f1ee4763a55
diff --git a/.changeset/html-paste-tight-lists.md b/.changeset/html-paste-tight-lists.md
new file mode 100644
index 000000000..3fd48a242
--- /dev/null
+++ b/.changeset/html-paste-tight-lists.md
@@ -0,0 +1,7 @@
+---
+"@inkeep/open-knowledge": patch
+---
+
+Pasting bullet lists from rich-text sources no longer inserts blank lines before nested sections
+
+Copying a tight bullet list from a rendered surface (Google Docs, Notion, Slack, ChatGPT, a web page) and pasting it into the editor used to synthesize blank lines between every item and before each nested sub-list, and swapped the bullet markers to `*`. The HTML-to-markdown conversion now normalizes the rich-editor DOM shape (paragraph-wrapped list items, nested sub-lists) back to a tight list, and mints the editor's canonical `-` bullet marker. List items that genuinely contain multiple blocks (two paragraphs, a paragraph plus a code block) still paste as loose lists so their blocks stay separate.
diff --git a/packages/core/src/markdown/html-to-mdast.ts b/packages/core/src/markdown/html-to-mdast.ts
index 78fdfeb02..b1f252c31 100644
--- a/packages/core/src/markdown/html-to-mdast.ts
+++ b/packages/core/src/markdown/html-to-mdast.ts
@@ -1,5 +1,5 @@
import type { Root as HastRoot } from 'hast';
-import type { Root as MdastRoot } from 'mdast';
+import type { List, Root as MdastRoot } from 'mdast';
import rehypeParse from 'rehype-parse';
import rehypeRemark from 'rehype-remark';
import remarkGfm from 'remark-gfm';
@@ -69,6 +69,16 @@ function applyCanonicalSourceFormDefaults(tree: MdastRoot): void {
});
}
+function normalizeListSpread(tree: MdastRoot): void {
+ visit(tree, 'list', (list: List) => {
+ for (const item of list.children) {
+ const nonListBlocks = item.children.filter((child) => child.type !== 'list').length;
+ item.spread = item.spread === true && nonListBlocks > 1;
+ }
+ list.spread = list.children.some((item) => item.spread === true);
+ });
+}
+
export function htmlToMdast(html: string, options?: HtmlToMdastOptions): MdastRoot {
const maxBytes = options?.maxBytes ?? HTML_MAX_BYTES;
if (html.length > maxBytes) {
@@ -88,11 +98,17 @@ export function htmlToMdast(html: string, options?: HtmlToMdastOptions): MdastRo
const hastTree = processor.parse(html) as HastRoot;
const mdast = processor.runSync(hastTree) as unknown as MdastRoot;
+ normalizeListSpread(mdast);
applyCanonicalSourceFormDefaults(mdast);
return mdast;
}
export function mdastToMarkdown(tree: MdastRoot): string {
flattenTableCellsInTree(tree);
- return String(unified().use(remarkGfm).use(remarkStringify).stringify(tree));
+ return String(
+ unified()
+ .use(remarkGfm)
+ .use(remarkStringify, { bullet: '-', rule: '-', fences: true })
+ .stringify(tree),
+ );
}