From 95795a20c25bd9a1e6c5c74b52c953d1476807e7 Mon Sep 17 00:00:00 2001 From: 01zulfi <85733202+01zulfi@users.noreply.github.com> Date: Mon, 22 Sep 2025 15:56:21 +0500 Subject: [PATCH] core: fix leading spaces in txt export && remove unneeded space at start of txt Signed-off-by: 01zulfi <85733202+01zulfi@users.noreply.github.com> --- .../__snapshots__/tiptap.test.js.snap | 18 ++++++++++++++++++ .../src/content-types/__tests__/tiptap.test.js | 3 ++- packages/core/src/content-types/tiptap.ts | 16 ++++++++++++++++ packages/core/src/utils/templates/text.ts | 2 +- 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/packages/core/src/content-types/__tests__/__snapshots__/tiptap.test.js.snap b/packages/core/src/content-types/__tests__/__snapshots__/tiptap.test.js.snap index e14cd7c98..1868c856a 100644 --- a/packages/core/src/content-types/__tests__/__snapshots__/tiptap.test.js.snap +++ b/packages/core/src/content-types/__tests__/__snapshots__/tiptap.test.js.snap @@ -126,6 +126,16 @@ But this post was supposed to be about the "Secrets" of Typescript Generics, rig " `; +exports[`convert HTML to markdown with leadingWhitespaces > html-to-md-leadingWhitespaces.md 1`] = ` +"two spaces + +four spaces +two spaces +four spaces + +" +`; + exports[`convert HTML to markdown with outlinelists > html-to-md-outlinelists.md 1`] = ` "Testing outline list: @@ -389,6 +399,14 @@ right? Well, let's get into that. 4. TYPE INFERENCE USING INTERFACE PROPERTIES" `; +exports[`convert HTML to text with leadingWhitespaces > html-to-txt-leadingWhitespaces.txt 1`] = ` +"  two spaces + +    four spaces +  two spaces +    four spaces" +`; + exports[`convert HTML to text with outlinelists > html-to-txt-outlinelists.txt 1`] = ` "Testing outline list: * My outline list diff --git a/packages/core/src/content-types/__tests__/tiptap.test.js b/packages/core/src/content-types/__tests__/tiptap.test.js index d718e9001..dbad0d149 100644 --- a/packages/core/src/content-types/__tests__/tiptap.test.js +++ b/packages/core/src/content-types/__tests__/tiptap.test.js @@ -65,7 +65,8 @@ const HTMLS = { tasklists: `

Hello

Nene

`, outlinelists: `

Testing outline list:

`, codeblock2: `
hello
`, - singleSpacedParagraphs: `

hello world

hello world 2

` + singleSpacedParagraphs: `

hello world

hello world 2

`, + leadingWhitespaces: `

two spaces

four spaces

two spaces

four spaces

` }; for (const html in HTMLS) { diff --git a/packages/core/src/content-types/tiptap.ts b/packages/core/src/content-types/tiptap.ts index 76d1f8bff..7660e7d0e 100644 --- a/packages/core/src/content-types/tiptap.ts +++ b/packages/core/src/content-types/tiptap.ts @@ -413,6 +413,22 @@ function convertHtmlToTxt(html: string, wrap = true) { builder.openBlock({ leadingLineBreaks: dataSpacing == "single" ? 1 : 2 }); + + // convert leading whitespace to NO-BREAK SPACE + if (elem.children && elem.children.length > 0) { + const firstChild = elem.children[0]; + if (firstChild.type === "text" && firstChild.data) { + firstChild.data = firstChild.data.replace( + /^([ \t]+)/, + (match) => { + return match + .replace(/ /g, "\u00A0") + .replace(/\t/g, "\u00A0\u00A0\u00A0\u00A0"); + } + ); + } + } + walk(elem.children, builder); builder.closeBlock({ trailingLineBreaks: 1 diff --git a/packages/core/src/utils/templates/text.ts b/packages/core/src/utils/templates/text.ts index 0ba100bd1..183bf1068 100644 --- a/packages/core/src/utils/templates/text.ts +++ b/packages/core/src/utils/templates/text.ts @@ -22,5 +22,5 @@ import { TemplateData } from "./index.js"; export function buildText(data: TemplateData) { return `${data.title} - ${data.content}`; +${data.content}`; }