Skip to content

Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.

fix(notes): preserve literal content in text exports (#2456)

Henry Suc5e1b0c4e0c1
diff --git a/src/helpers/ipcHandlers.js b/src/helpers/ipcHandlers.js
index d770661f3..284919aa5 100644
--- a/src/helpers/ipcHandlers.js
+++ b/src/helpers/ipcHandlers.js
@@ -2736,13 +2736,8 @@ class IPCHandlers {
let exportContent;
if (format === "txt") {
- exportContent = (note.content || "")
- .replace(/#{1,6}\s+/g, "")
- .replace(/[*_~`]+/g, "")
- .replace(/\[([^\]]+)\]\([^)]+\)/g, "$1")
- .replace(/!\[([^\]]*)\]\([^)]+\)/g, "$1")
- .replace(/^>\s+/gm, "")
- .trim();
+ const { markdownToPlainText } = await import("./markdownToPlainText.ts");
+ exportContent = markdownToPlainText(note.content || "");
} else {
exportContent = note.enhanced_content || note.content;
}
diff --git a/src/helpers/markdownToPlainText.ts b/src/helpers/markdownToPlainText.ts
index 18f9450dd..1b869ea29 100644
--- a/src/helpers/markdownToPlainText.ts
+++ b/src/helpers/markdownToPlainText.ts
@@ -8,10 +8,9 @@
// bullet, a `1.` number, `2 * 3`, snake_case, `#hashtag` and `x > y` are all
// left exactly as written.
//
-// The inline rules are applied per line, so emphasis that spans a line break
-// and setext (underlined) headings are out of scope: the prompt suffix that
-// asks the model for plain prose is the primary defence, and this helper is
-// the floor under a model that drifts back to markdown.
+// Inline rules span prose lines within a block, so emphasis around a hard
+// break is removed without matching across paragraphs, tables or code.
+// Setext (underlined) headings remain out of scope.
const FENCE_LINE = /^\s*(`{3,}|~{3,})(.*)$/;
const HORIZONTAL_RULE = /^\s*([-*_])(\s*\1){2,}\s*$/;
@@ -105,7 +104,7 @@ function stripInline(text: string): string {
return `${placeholderPrefix}${literals.length - 1}`;
};
const withPlaceholders = text.replace(
- /(?<![\\`])(`+)(?!`)(.*?)(?<!`)\1(?!`)/g,
+ /(?<![\\`])(`+)(?!`)(.*?)(?<!`)\1(?!`)/gs,
(_match, _delimiter: string, content: string): string =>
// Markdown permits padding a code span with one space at either end
// so literal backticks do not merge with the delimiters.
@@ -146,15 +145,15 @@ function stripInline(text: string): string {
// non-word close that is also not `(`, so dunder identifiers such as
// `__init__` (immediately followed by a call's `(`) are never mistaken
// for emphasis, while `__bold__ word` still strips.
- .replace(/(?<!\\)\*\*(\S(?:.*?\S)??)\*\*/g, "$1")
- .replace(/(?<![\w\\])__(\S(?:.*?\S)??)__(?![\w(])/g, "$1")
- .replace(/(?<!\\)~~(\S(?:.*?\S)??)~~/g, "$1")
+ .replace(/(?<!\\)\*\*(\S(?:.*?\S)??)\*\*/gs, "$1")
+ .replace(/(?<![\w\\])__(\S(?:.*?\S)??)__(?![\w(])/gs, "$1")
+ .replace(/(?<!\\)~~(\S(?:.*?\S)??)~~/gs, "$1")
// Markers must hug non-space on the inside, not sit inside a word on the
// outside, and not be escaped — so `2 * 3`, snake_case and `\*` survive.
// The single-underscore content also may not start or end with `_` itself,
// so a dunder like `__init__` is never absorbed as `_` + `_init_` + `_`.
- .replace(/(?<![\w*\\])\*(\S(?:.*?\S)??)\*(?![\w*])/g, "$1")
- .replace(/(?<![\w_\\])_(?!_)(\S(?:.*?[^\s_])??)_(?![\w_])/g, "$1");
+ .replace(/(?<![\w*\\])\*(\S(?:.*?\S)??)\*(?![\w*])/gs, "$1")
+ .replace(/(?<![\w_\\])_(?!_)(\S(?:.*?[^\s_])??)_(?![\w_])/gs, "$1");
// A literal can itself hold placeholders (a code span inside a link destination).
const placeholder = new RegExp(`${placeholderPrefix}(\\d+)`, "g");
@@ -185,9 +184,20 @@ function splitTableCells(row: string): string[] {
export function markdownToPlainText(markdown: string): string {
const lines: string[] = [];
const codeLines = new Set<number>();
+ const sourceLines = markdown.split(/\r?\n/);
+ const proseLines: string[] = [];
let openingFence: string | null = null;
- for (const line of markdown.split(/\r?\n/)) {
+ const flushProse = (): void => {
+ if (!proseLines.length) return;
+ for (const line of stripInline(proseLines.join("\n")).split("\n")) {
+ lines.push(line.replace(/[ \t]+$/, ""));
+ }
+ proseLines.length = 0;
+ };
+
+ for (let index = 0; index < sourceLines.length; index += 1) {
+ const line = sourceLines[index];
const fence = line.match(FENCE_LINE);
if (openingFence) {
if (
@@ -204,24 +214,53 @@ export function markdownToPlainText(markdown: string): string {
continue;
}
if (fence && (fence[1][0] === "~" || !fence[2].includes("`"))) {
+ flushProse();
openingFence = fence[1];
continue;
}
- if (HORIZONTAL_RULE.test(line)) continue;
+ if (HORIZONTAL_RULE.test(line)) {
+ flushProse();
+ continue;
+ }
- if (TABLE_ROW.test(line)) {
+ const nextLine = sourceLines[index + 1] ?? "";
+ if (TABLE_ROW.test(line) && TABLE_ROW.test(nextLine)) {
const cells = splitTableCells(line);
- if (cells.every((cell) => TABLE_ALIGNMENT_CELL.test(cell))) continue;
- lines.push(cells.map(stripInline).join("\t"));
+ const alignment = splitTableCells(nextLine);
+ if (
+ cells.length === alignment.length &&
+ alignment.every((cell) => TABLE_ALIGNMENT_CELL.test(cell))
+ ) {
+ flushProse();
+ lines.push(cells.map(stripInline).join("\t"));
+ index += 2;
+ // Only the separator immediately after the header is syntax; later
+ // hyphen-only rows are data, such as missing values.
+ while (index < sourceLines.length && TABLE_ROW.test(sourceLines[index])) {
+ lines.push(splitTableCells(sourceLines[index]).map(stripInline).join("\t"));
+ index += 1;
+ }
+ index -= 1;
+ continue;
+ }
+ }
+
+ if (/^\s*$/.test(line)) {
+ flushProse();
+ lines.push("");
continue;
}
+ const startsBlock = /^\s{0,3}(?:>|#{1,6}\s|[-+*]\s|\d+[.)]\s)/.test(line);
+ if (startsBlock) flushProse();
const block = line
.replace(/^(\s{0,3}>\s?)+/, "")
.replace(/^\s{0,3}#{1,6}\s+/, "")
.replace(/^(\s*)\*\s+/, "$1- ");
- lines.push(stripInline(block).replace(/[ \t]+$/, ""));
+ proseLines.push(block);
+ if (/^\s{0,3}(?:>|#{1,6}\s)/.test(line)) flushProse();
}
+ flushProse();
// Trim only prose padding: code whitespace and table edge cells are data.
let start = 0;
diff --git a/test/components/richTextEditor.test.js b/test/components/richTextEditor.test.js
index fda2d47cd..5c3316f1a 100644
--- a/test/components/richTextEditor.test.js
+++ b/test/components/richTextEditor.test.js
@@ -100,6 +100,21 @@ function markdownOf(editor) {
return markdown.trim();
}
+test("plain text conversion removes bold applied across an editor hard break", async (t) => {
+ const { markdownToPlainText } = await import("../../src/helpers/markdownToPlainText.ts");
+ const editor = createEditor("");
+ t.after(() => editor.destroy());
+ editor
+ .chain()
+ .insertContent("first")
+ .setHardBreak()
+ .insertContent("second")
+ .selectAll()
+ .toggleBold()
+ .run();
+ assert.equal(markdownToPlainText(markdownOf(editor)), "first\\\nsecond");
+});
+
/** Asserts the output and that loading it back gives the same Markdown. */
function assertSaves(editor, expected) {
const markdown = markdownOf(editor);
diff --git a/test/helpers/exportDialog.test.js b/test/helpers/exportDialog.test.js
index c2b8827d4..99af63d6d 100644
--- a/test/helpers/exportDialog.test.js
+++ b/test/helpers/exportDialog.test.js
@@ -1,11 +1,16 @@
const test = require("node:test");
const assert = require("node:assert/strict");
const Module = require("node:module");
+const fs = require("node:fs");
+const os = require("node:os");
+const path = require("node:path");
const handlersModulePath = require.resolve("../../src/helpers/ipcHandlers");
const originalLoad = Module._load;
const handlers = new Map();
let saveDialogOptions;
+let saveDialogResult;
+let note;
const electronStub = {
app: {
@@ -35,7 +40,7 @@ const electronStub = {
dialog: {
showSaveDialog: async (options) => {
saveDialogOptions = options;
- return { canceled: true, filePath: "" };
+ return saveDialogResult;
},
},
screen: { getPrimaryDisplay: () => ({ workAreaSize: { width: 0, height: 0 } }) },
@@ -75,11 +80,7 @@ test.before(() => {
const IPCHandlers = require(handlersModulePath);
const target = {
databaseManager: {
- getNote: () => ({
- id: 1,
- title: "Team sync",
- transcript: JSON.stringify([{ text: "Hello", start: 0, end: 1 }]),
- }),
+ getNote: () => note,
},
_buildSpeakerMappings: () => ({}),
};
@@ -92,6 +93,12 @@ test.before(() => {
test.beforeEach(() => {
saveDialogOptions = undefined;
+ saveDialogResult = { canceled: true, filePath: "" };
+ note = {
+ id: 1,
+ title: "Team sync",
+ transcript: JSON.stringify([{ text: "Hello", start: 0, end: 1 }]),
+ };
});
test.after(() => {
@@ -122,3 +129,88 @@ for (const [format, name] of [
assert.deepEqual(result, { success: false });
});
}
+
+async function exportNoteToFile(t, content, format = "txt") {
+ const directory = fs.mkdtempSync(path.join(os.tmpdir(), "openwhispr-note-export-"));
+ t.after(() => fs.rmSync(directory, { recursive: true, force: true }));
+ const filePath = path.join(directory, `note.${format}`);
+ note.content = content;
+ saveDialogResult = { canceled: false, filePath };
+ assert.deepEqual(await handlers.get("export-note")({}, 1, format), { success: true });
+ return fs.readFileSync(filePath, "utf8");
+}
+
+test("Text note export preserves literal identifiers, arithmetic and fenced code", async (t) => {
+ const content = "File: report_final.csv\nCompute 2 * 3.\n```sh\nls ~/project_*\n```";
+ assert.equal(
+ await exportNoteToFile(t, content),
+ "File: report_final.csv\nCompute 2 * 3.\nls ~/project_*"
+ );
+});
+
+test("Text note export strips formatting while retaining paragraphs and list lines", async (t) => {
+ const content = "# Plan\n\n**Ship** with _care_.\n> Keep this\n- First\n- Second";
+ assert.equal(
+ await exportNoteToFile(t, content),
+ "Plan\n\nShip with care.\nKeep this\n- First\n- Second"
+ );
+});
+
+test("Text note export keeps inline code and linked resource addresses", async (t) => {
+ const content = "Run `a_b * c`.\nSee [the report](https://example.test/report_final?q=a_b).";
+ assert.equal(
+ await exportNoteToFile(t, content),
+ "Run a_b * c.\nSee the report (https://example.test/report_final?q=a_b)."
+ );
+});
+
+test("Text note export preserves code whitespace and accepts empty content", async (t) => {
+ assert.equal(await exportNoteToFile(t, "```\n value_name * 2 \n```"), " value_name * 2 ");
+ for (const content of ["", null, undefined]) {
+ assert.equal(await exportNoteToFile(t, content), "");
+ }
+});
+
+test("Text note export preserves literal pipes and hyphen-only table data", async (t) => {
+ assert.equal(await exportNoteToFile(t, "|x|"), "|x|");
+ assert.equal(
+ await exportNoteToFile(t, "| Name | Value |\n| --- | --- |\n| - | - |\n| actual | 5 |"),
+ "Name\tValue\n-\t-\nactual\t5"
+ );
+});
+
+test("Text note export removes bold spanning an editor hard break", async (t) => {
+ assert.equal(await exportNoteToFile(t, "**first\\\nsecond**"), "first\\\nsecond");
+});
+
+test("Markdown note export retains the existing enhanced-content preference", async (t) => {
+ note.enhanced_content = "## Summary\n**Ready**";
+ assert.equal(await exportNoteToFile(t, "Original notes", "md"), note.enhanced_content);
+ note.enhanced_content = null;
+ assert.equal(await exportNoteToFile(t, "**Original notes**", "md"), "**Original notes**");
+});
+
+test("a repeated Text note export writes the latest content to the selected file", async (t) => {
+ await exportNoteToFile(t, "First_file");
+ note.content = "Second_file";
+ assert.deepEqual(await handlers.get("export-note")({}, 1, "txt"), { success: true });
+ assert.equal(fs.readFileSync(saveDialogResult.filePath, "utf8"), "Second_file");
+});
+
+test("canceling a Text note export does not write the selected path", async (t) => {
+ await exportNoteToFile(t, "Existing_file");
+ saveDialogResult.canceled = true;
+ note.content = "Replacement_file";
+ assert.deepEqual(await handlers.get("export-note")({}, 1, "txt"), { success: false });
+ assert.equal(fs.readFileSync(saveDialogResult.filePath, "utf8"), "Existing_file");
+});
+
+test("a failed Text note write returns an error and a later export still succeeds", async (t) => {
+ await exportNoteToFile(t, "Initial_file");
+ saveDialogResult.filePath = path.dirname(saveDialogResult.filePath);
+ const result = await handlers.get("export-note")({}, 1, "txt");
+ assert.equal(result.success, false);
+ assert.equal(typeof result.error, "string");
+ assert.ok(result.error.length > 0);
+ assert.equal(await exportNoteToFile(t, "Retry_file"), "Retry_file");
+});
diff --git a/test/helpers/mainProcessEsmEntries.test.js b/test/helpers/mainProcessEsmEntries.test.js
index 813ee914d..7a9a77fee 100644
--- a/test/helpers/mainProcessEsmEntries.test.js
+++ b/test/helpers/mainProcessEsmEntries.test.js
@@ -8,7 +8,10 @@ const { spawnSync } = require("node:child_process");
// rejects JSON imports without `with { type: "json" }`. The suite runs under
// tsx, which resolves them anyway, so importing these from a normal test proves
// nothing — each entry needs a child process with no loader hooks (#1908).
-const MAIN_PROCESS_ESM_ENTRIES = ["src/helpers/transcriptionRoute.ts"];
+const MAIN_PROCESS_ESM_ENTRIES = [
+ "src/helpers/transcriptionRoute.ts",
+ "src/helpers/markdownToPlainText.ts",
+];
const repoRoot = path.resolve(__dirname, "../..");
const bareEnv = { ...process.env };
diff --git a/test/helpers/markdownToPlainText.test.js b/test/helpers/markdownToPlainText.test.js
index d3bba4429..96abe6218 100644
--- a/test/helpers/markdownToPlainText.test.js
+++ b/test/helpers/markdownToPlainText.test.js
@@ -30,6 +30,39 @@ test("headings lose their marks", async () => {
assert.equal(markdownToPlainText("## Title\nBody"), "Title\nBody");
});
+test("emphasis spans consecutive prose lines but not separate blocks", async () => {
+ const { markdownToPlainText } = await helperModule;
+ for (const marker of ["**", "*", "__", "_", "~~"]) {
+ assert.equal(markdownToPlainText(`${marker}first\nsecond${marker}`), "first\nsecond");
+ assert.equal(markdownToPlainText(`${marker}first\\\nsecond${marker}`), "first\\\nsecond");
+ }
+ for (const [content, expected] of [
+ ["**first\n\nsecond**", "**first\n\nsecond**"],
+ ["**first\n# second**", "**first\nsecond**"],
+ ["**first\n- second**", "**first\n- second**"],
+ ["**first\n```\nsecond**\n```", "**first\nsecond**"],
+ ["**first\n---\nsecond**", "**first\nsecond**"],
+ ["**first\n| A |\n| - |\nsecond**", "**first\nA\nsecond**"],
+ ]) {
+ assert.equal(markdownToPlainText(content), expected);
+ }
+});
+
+test("pipe-delimited prose stays literal unless followed by a matching table separator", async () => {
+ const { markdownToPlainText } = await helperModule;
+ for (const content of ["|x|", "| a | b |", "| A | B |\n| --- |", "| - | - |", "| | | |"]) {
+ assert.equal(markdownToPlainText(content), content);
+ }
+});
+
+test("only the table separator is removed, not hyphen-only headers or data rows", async () => {
+ const { markdownToPlainText } = await helperModule;
+ assert.equal(
+ markdownToPlainText("| - | - |\n| --- | --- |\n| - | - |\n| a | b |\n\n|x|"),
+ "-\t-\n-\t-\na\tb\n\n|x|"
+ );
+});
+
test("inline code and fenced blocks keep their content verbatim", async () => {
const { markdownToPlainText } = await helperModule;
assert.equal(
@@ -108,6 +141,7 @@ test("inline code content is verbatim, never run through other inline rules", as
assert.equal(markdownToPlainText("use `__init__` here"), "use __init__ here");
assert.equal(markdownToPlainText("inline `**not bold**` code"), "inline **not bold** code");
assert.equal(markdownToPlainText("`C:\\Users\\me`"), "C:\\Users\\me");
+ assert.equal(markdownToPlainText("Run `**first\nsecond**`."), "Run **first\nsecond**.");
});
test("a literal placeholder sequence in the input survives the restore step", async () => {
@@ -200,9 +234,10 @@ test("escaped table pipes remain inside their cell and preserve empty cells", as
markdownToPlainText("| Choice | Meaning |\n| --- | --- |\n| A \\| B | either choice |"),
"Choice\tMeaning\nA | B\teither choice"
);
- assert.equal(markdownToPlainText(String.raw`| A\\| B |`), "A\\\tB");
- assert.equal(markdownToPlainText(String.raw`| A\\\|B | C |`), "A\\|B\tC");
- assert.equal(markdownToPlainText("| A | | C |"), "A\t\tC");
+ const header = "| Left | Right |\n| --- | --- |\n";
+ assert.equal(markdownToPlainText(header + String.raw`| A\\| B |`), "Left\tRight\nA\\\tB");
+ assert.equal(markdownToPlainText(header + String.raw`| A\\\|B | C |`), "Left\tRight\nA\\|B\tC");
+ assert.equal(markdownToPlainText("| A | | C |\n| --- | --- | --- |"), "A\t\tC");
});
test("POSIX paths retain literal underscores alongside surrounding emphasis", async () => {
@@ -275,7 +310,7 @@ test("table conversion preserves empty edge cells and column alignment", async (
markdownToPlainText("\n| A | B | |\n|---|---|---|\n| | value | |\n\n"),
"A\tB\t\n\tvalue\t"
);
- assert.equal(markdownToPlainText("| | | |"), "\t\t");
+ assert.equal(markdownToPlainText("| | | |\n| --- | --- | --- |"), "\t\t");
assert.equal(markdownToPlainText(" Prose. \n\nMore prose. \n"), "Prose.\n\nMore prose.");
});