Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

core: fix leading spaces in txt export && remove unneeded space at start of txt Signed-off-by: 01zulfi <[email protected]>

01zulfi95795a20c25b
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: `<p>Hello</p><ul data-collapsed="false" class="checklist"><li class="checked checklist--item"><p>Task item 1</p></li><li class="checked checklist--item"><p>Task item 2</p></li><li class="checked checklist--item"><p>Task item 3</p></li><li class="checklist--item"><p>Task item 4</p><ul data-collapsed="false" class="checklist"><li class="checklist--item"><p>Sub task item 1</p></li><li class="checklist--item"><p>Sub task item 2</p></li></ul></li><li class="checklist--item"><p>Task Item 5</p></li></ul><p>Nene</p><ul><li><p>dasvsadv</p></li><li><p>adsva\`sd</p></li><li><p>vasd</p></li><li><p>vsadvdsa</p></li></ul>`,
outlinelists: `<p >Testing outline list:</p><ul data-collapsed="false" data-type="outlineList"><li data-type="outlineListItem"><p >My outline list</p></li><li data-type="outlineListItem"><p >works</p></li><li data-type="outlineListItem"><p >but sometimes</p><ul data-collapsed="false" data-type="outlineList"><li data-type="outlineListItem"><p >It doesn't</p></li><li data-type="outlineListItem"><p >what do I do?</p></li><li data-type="outlineListItem"><p >I need to do something!</p></li></ul></li><li data-type="outlineListItem"><p >Makes no sense!</p></li><li data-type="outlineListItem"><p >Yes it doesn't!</p></li></ul>`,
codeblock2: `<pre>hello<br></pre>`,
- singleSpacedParagraphs: `<p data-spacing="single">hello world</p><p data-spacing="single">hello world 2</p>`
+ singleSpacedParagraphs: `<p data-spacing="single">hello world</p><p data-spacing="single">hello world 2</p>`,
+ leadingWhitespaces: `<p data-spacing="double"> two spaces</p><p data-spacing="double"> four spaces</p><p data-spacing="single"> two spaces</p><p data-spacing="single"> four spaces</p>`
};
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}`;
}