Skip to content
Open
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
38 changes: 38 additions & 0 deletions scripts/notion-fetch/contentSanitizer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,42 @@ describe("contentSanitizer", () => {
expect(result).toBe("[tag](#tag)");
});
});

describe("restoreSoftLineBreaks", () => {
it("should convert single newlines between text into <br /> elements", () => {
const input = "First line\nSecond line";
const result = scriptModule.restoreSoftLineBreaks(input);
expect(result).toBe("First line<br />\nSecond line");
});

it("should leave paragraph breaks (double newlines) untouched", () => {
const input = "First paragraph\n\nSecond paragraph";
const result = scriptModule.restoreSoftLineBreaks(input);
expect(result).toBe(input);
});

it("should ignore newlines that start markdown list items", () => {
const input = "Intro text\n- list item";
const result = scriptModule.restoreSoftLineBreaks(input);
expect(result).toBe(input);
});

it("should ignore newlines before numbered list items", () => {
const input = "Intro text\n1. First item";
const result = scriptModule.restoreSoftLineBreaks(input);
expect(result).toBe(input);
});

it("should not modify content inside fenced code blocks", () => {
const input = "```js\nconst x = 1;\nconst y = 2;\n```\nOutside";
const result = scriptModule.restoreSoftLineBreaks(input);
expect(result).toBe(input);
});

it("should normalize unicode line separators into <br /> line breaks", () => {
const input = "Line one\u2028Line two";
const result = scriptModule.restoreSoftLineBreaks(input);
expect(result).toBe("Line one<br />\nLine two");
});
});
});
66 changes: 66 additions & 0 deletions scripts/notion-fetch/contentSanitizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,69 @@ export function sanitizeMarkdownContent(content: string): string {

return content;
}

/**
* Restores intentional soft line breaks (Shift+Enter in Notion) by converting single
* newlines within paragraphs into `<br />` elements while avoiding structural markdown lines.
*/
export function restoreSoftLineBreaks(content: string): string {
if (!content) return content;

const codeBlocks: string[] = [];
const codeSpans: string[] = [];

const blockPlaceholder = (index: number) =>
`__SOFTBREAK_CODEBLOCK_${index}__`;
const spanPlaceholder = (index: number) => `__SOFTBREAK_CODESPAN_${index}__`;

// Protect fenced blocks and inline code so formatting is left untouched
let transformed = content.replace(/```[\s\S]*?```/g, (match) => {
codeBlocks.push(match);
return blockPlaceholder(codeBlocks.length - 1);
});

transformed = transformed.replace(/`[^`\n]*`/g, (match) => {
codeSpans.push(match);
return spanPlaceholder(codeSpans.length - 1);
});

// Normalize uncommon Unicode line separators that Notion may emit
transformed = transformed.replace(/[\u2028\u2029]/g, "\n");

transformed = transformed.replace(
/(?<=\S)\n(?=\S)/g,
(newline, offset, full) => {
const nextLine = full.slice(offset + newline.length);
const trimmedNextLine = nextLine.replace(/^[ \t]+/, "");

const before = full.slice(0, offset);
const prevLine = before.slice(before.lastIndexOf("\n") + 1);
const trimmedPrevLine = prevLine.trim();

// Skip markdown constructs that should remain as new lines
if (
/^([-*+>#|<])/.test(trimmedNextLine) ||
/^\d+[.)]/.test(trimmedNextLine) ||
/^```/.test(trimmedPrevLine) ||
/^---$/.test(trimmedPrevLine) ||
trimmedPrevLine.startsWith("__SOFTBREAK_CODEBLOCK_")
) {
return newline;
}

return "<br />\n";
}
);

// Restore masked code sections
transformed = transformed.replace(
/__SOFTBREAK_CODEBLOCK_(\d+)__/g,
(_m, i) => codeBlocks[Number(i)]
);
transformed = transformed.replace(
/__SOFTBREAK_CODESPAN_(\d+)__/g,
(_m, i) => codeSpans[Number(i)]
);

return transformed;
}
1 change: 1 addition & 0 deletions scripts/notion-fetch/generateBlocks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ vi.mock("./imageProcessor", () => ({

vi.mock("./utils", () => ({
sanitizeMarkdownContent: vi.fn((content) => content),
restoreSoftLineBreaks: vi.fn((content) => content),
compressImageToFileWithFallback: vi.fn(),
detectFormatFromBuffer: vi.fn(() => "jpeg"),
formatFromContentType: vi.fn(() => "jpeg"),
Expand Down
4 changes: 4 additions & 0 deletions scripts/notion-fetch/generateBlocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import chalk from "chalk";
import { processImage } from "./imageProcessor";
import {
sanitizeMarkdownContent,
restoreSoftLineBreaks,
compressImageToFileWithFallback,
detectFormatFromBuffer,
formatFromContentType,
Expand Down Expand Up @@ -1550,6 +1551,9 @@ export async function generateBlocks(pages, progressCallback) {
markdownString.parent = sanitizeMarkdownContent(
markdownString.parent
);
markdownString.parent = restoreSoftLineBreaks(
markdownString.parent
);
// Remove duplicate title heading if it exists
// The first H1 heading often duplicates the title in Notion exports
let contentBody = markdownString.parent;
Expand Down
7 changes: 5 additions & 2 deletions scripts/notion-fetch/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import os from "node:os";
import chalk from "chalk";
import { compressImage } from "./imageCompressor";

// Re-export sanitize so callers have a single utils entrypoint
export { sanitizeMarkdownContent } from "./contentSanitizer";
// Re-export sanitize helpers so callers have a single utils entrypoint
export {
sanitizeMarkdownContent,
restoreSoftLineBreaks,
} from "./contentSanitizer";

// Fail-open toggle: defaults to true unless explicitly set to 'false'
export const SOFT_FAIL: boolean =
Expand Down