Use the filename, not the first heading, as the title for Slab imports

In Slab a document's leading heading is real content, not its title — the
filename is authoritative. Add an `extractTitle` option to
DocumentConverter.convert (default true, preserving existing callers) that,
when false, skips lifting a leading H1 into the title and leaves it in the
body. Expose it through a `shouldExtractTitleFromHeading` seam on the
Markdown task and override it in SlabAPIImportTask. With no extracted title,
the page falls back to the filename-derived title.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AC5Ho2SHBjNpxEAMr5uL6o
This commit is contained in:
Claude committed 2026-06-29 20:27:45 +00:00
1 parent bcc69c23c0
commit 5f86f595ff
5 files changed
+61 -6

No files matched your search

+15 -1
View File
@@ -166,6 +166,19 @@ export default class MarkdownAPIImportTask extends APIImportTask<Markdown> {
return true;
}
/**
* Whether a document's leading H1 heading should be lifted out as its title
* (and removed from the body). Outline's own Markdown export writes the
* title as a leading H1, so this defaults to true. Sources where the
* filename is authoritative and the first heading is real content (e.g.
* Slab) override this to keep the heading in the body.
*
* @returns true to derive the title from a leading H1 heading.
*/
protected shouldExtractTitleFromHeading(): boolean {
return true;
}
protected async scheduleNextTask(importTask: ImportTask<Markdown>) {
await new MarkdownAPIImportTask().schedule({ importTaskId: importTask.id });
}
@@ -432,7 +445,8 @@ export default class MarkdownAPIImportTask extends APIImportTask<Markdown> {
const { doc, title, icon } = await DocumentConverter.convert(
transformedMarkdown,
path.basename(item.path),
"text/markdown"
"text/markdown",
{ extractTitle: this.shouldExtractTitleFromHeading() }
);
taskOutput.push({
@@ -5,6 +5,10 @@ class TestSlabAPIImportTask extends SlabAPIImportTask {
public resolve(nodes: ZipTreeNode[]): ZipTreeNode[] {
return this.resolveCollectionRootNodes(nodes);
}
public extractsTitleFromHeading(): boolean {
return this.shouldExtractTitleFromHeading();
}
}
const dir = (
@@ -69,4 +73,8 @@ describe("SlabAPIImportTask#resolveCollectionRootNodes", () => {
expect(task.resolve([root])).toEqual([root]);
});
it("does not derive the title from a document's leading heading", () => {
expect(task.extractsTitleFromHeading()).toBe(false);
});
});
+10
View File
@@ -40,4 +40,14 @@ export default class SlabAPIImportTask extends MarkdownAPIImportTask {
}
return nodes;
}
/**
* Slab does not treat a document's first heading as its title — the
* filename is authoritative. Keep the leading heading as body content.
*
* @returns false so the leading heading is preserved in the document body.
*/
protected shouldExtractTitleFromHeading(): boolean {
return false;
}
}
+14
View File
@@ -149,6 +149,20 @@ Jane,24,`;
expect(result.text).toContain("Subtitle");
});
it("should keep the leading H1 in the body when extractTitle is false", async () => {
const md = "# My Title\n\nContent here";
const result = await DocumentConverter.convert(
md,
"test.md",
"text/markdown",
{ extractTitle: false }
);
expect(result.title).toEqual("");
expect(result.text).toContain("My Title");
expect(result.text).toContain("Content here");
});
it("should convert frontmatter to yaml codeblock", async () => {
const md = `---
title: Test Document
+14 -5
View File
@@ -26,13 +26,20 @@ export class DocumentConverter {
* @param content The content of the file.
* @param fileName The name of the file, including extension.
* @param mimeType The mime type of the file.
* @param options Conversion options.
* @param options.extractTitle Whether a leading H1 heading should be lifted
* out as the document title and removed from the body. Defaults to true;
* set false for sources where the filename is authoritative and the first
* heading must remain part of the content (e.g. Slab).
* @returns The converted document with text, data, title, and icon.
*/
public static async convert(
content: Buffer | string,
fileName: string,
mimeType: string
mimeType: string,
options: { extractTitle?: boolean } = {}
): Promise<ConvertResult> {
const { extractTitle = true } = options;
let doc: Node;
// Route to appropriate conversion method
@@ -50,10 +57,12 @@ export class DocumentConverter {
// Extract title from first H1 heading
let title = "";
const headings = ProsemirrorHelper.getHeadings(doc);
if (headings.length > 0 && headings[0].level === 1) {
title = headings[0].title;
doc = ProsemirrorHelper.removeFirstHeading(doc);
if (extractTitle) {
const headings = ProsemirrorHelper.getHeadings(doc);
if (headings.length > 0 && headings[0].level === 1) {
title = headings[0].title;
doc = ProsemirrorHelper.removeFirstHeading(doc);
}
}
// Extract emoji from start of document