diff --git a/server/queues/tasks/MarkdownAPIImportTask.ts b/server/queues/tasks/MarkdownAPIImportTask.ts index d7c7dbc227..5336b01edf 100644 --- a/server/queues/tasks/MarkdownAPIImportTask.ts +++ b/server/queues/tasks/MarkdownAPIImportTask.ts @@ -166,6 +166,19 @@ export default class MarkdownAPIImportTask extends APIImportTask { 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) { await new MarkdownAPIImportTask().schedule({ importTaskId: importTask.id }); } @@ -432,7 +445,8 @@ export default class MarkdownAPIImportTask extends APIImportTask { const { doc, title, icon } = await DocumentConverter.convert( transformedMarkdown, path.basename(item.path), - "text/markdown" + "text/markdown", + { extractTitle: this.shouldExtractTitleFromHeading() } ); taskOutput.push({ diff --git a/server/queues/tasks/SlabAPIImportTask.test.ts b/server/queues/tasks/SlabAPIImportTask.test.ts index d7bdf7cc32..24ad9f943c 100644 --- a/server/queues/tasks/SlabAPIImportTask.test.ts +++ b/server/queues/tasks/SlabAPIImportTask.test.ts @@ -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); + }); }); diff --git a/server/queues/tasks/SlabAPIImportTask.ts b/server/queues/tasks/SlabAPIImportTask.ts index 441b1067cd..aa4fa7f805 100644 --- a/server/queues/tasks/SlabAPIImportTask.ts +++ b/server/queues/tasks/SlabAPIImportTask.ts @@ -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; + } } diff --git a/server/utils/DocumentConverter.test.ts b/server/utils/DocumentConverter.test.ts index ed66816797..bd65f3ab90 100644 --- a/server/utils/DocumentConverter.test.ts +++ b/server/utils/DocumentConverter.test.ts @@ -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 diff --git a/server/utils/DocumentConverter.ts b/server/utils/DocumentConverter.ts index ab5d551e3a..e0daf1c9b3 100644 --- a/server/utils/DocumentConverter.ts +++ b/server/utils/DocumentConverter.ts @@ -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 { + 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