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): escape control characters in mirrored note frontmatter (#1646)

Henry Su6c72923fb90d
diff --git a/src/helpers/markdownMirror.js b/src/helpers/markdownMirror.js
index ee55c9ef4..1c8928c39 100644
--- a/src/helpers/markdownMirror.js
+++ b/src/helpers/markdownMirror.js
@@ -33,8 +33,22 @@ class MarkdownMirror {
_buildFrontmatter(note, folderName) {
const escYaml = (str) => {
if (!str) return '""';
- if (/[:#{}[\],&*?|>!%@`]/.test(str) || str.includes('"') || str.includes("'")) {
- return `"${str.replace(/\\/g, "\\\\").replace(/"/g, '\\"')}"`;
+ // A control character (newline, carriage return, tab) in a title splits the
+ // mapping onto a bare line and makes the whole frontmatter block unparseable,
+ // so force quoting and escape them like `"` and `\` — keeping the value on
+ // one line and round-tripping back to the original when parsed.
+ if (
+ /[:#{}[\],&*?|>!%@`]/.test(str) ||
+ /[\n\r\t]/.test(str) ||
+ str.includes('"') ||
+ str.includes("'")
+ ) {
+ return `"${str
+ .replace(/\\/g, "\\\\")
+ .replace(/"/g, '\\"')
+ .replace(/\n/g, "\\n")
+ .replace(/\r/g, "\\r")
+ .replace(/\t/g, "\\t")}"`;
}
return str;
};
diff --git a/test/helpers/markdownMirror.test.js b/test/helpers/markdownMirror.test.js
index 737f8b13a..16be1bcd3 100644
--- a/test/helpers/markdownMirror.test.js
+++ b/test/helpers/markdownMirror.test.js
@@ -81,6 +81,53 @@ test("renaming a mirrored note cleans up both stale files and reveals the note",
assert.match(fs.readFileSync(notePath, "utf-8"), /Updated notes$/);
});
+function readFrontmatter(fileContent) {
+ const match = fileContent.match(/^---\n([\s\S]*?)\n---\n/);
+ assert.ok(match, "file should start with a --- fenced frontmatter block");
+ const map = {};
+ for (const line of match[1].split("\n")) {
+ const kv = line.match(/^([A-Za-z_]+): (.*)$/);
+ assert.ok(kv, `frontmatter line is a "key: value" mapping entry: ${JSON.stringify(line)}`);
+ let value = kv[2];
+ if (value.startsWith('"') && value.endsWith('"')) {
+ value = value
+ .slice(1, -1)
+ .replace(/\\n/g, "\n")
+ .replace(/\\r/g, "\r")
+ .replace(/\\t/g, "\t")
+ .replace(/\\"/g, '"')
+ .replace(/\\\\/g, "\\");
+ }
+ map[kv[1]] = value;
+ }
+ return map;
+}
+
+test("a title with control characters keeps the frontmatter a valid single-line mapping", (t) => {
+ const basePath = fs.mkdtempSync(path.join(os.tmpdir(), "openwhispr-markdown-mirror-"));
+ t.after(() => fs.rmSync(basePath, { recursive: true, force: true }));
+
+ const note = {
+ id: 5,
+ title: "Q3 Review\nconfidential\ttag",
+ content: "Body text",
+ created_at: "2026-07-21T12:00:00Z",
+ };
+
+ markdownMirror.init(basePath);
+ markdownMirror.writeNote(note, "Personal");
+
+ const notePath = markdownMirror.getNotePath(note.id);
+ assert.ok(notePath, "the note file is discoverable, so its frontmatter parsed as a note");
+ const frontmatter = readFrontmatter(fs.readFileSync(notePath, "utf-8"));
+
+ // Every key survives and the title round-trips, control characters intact.
+ assert.equal(frontmatter.id, "5");
+ assert.equal(frontmatter.type, "personal");
+ assert.equal(frontmatter.folder, "Personal");
+ assert.equal(frontmatter.title, note.title);
+});
+
test("a note file re-saved with a BOM or CRLF is still recognised as its note", (t) => {
const basePath = fs.mkdtempSync(path.join(os.tmpdir(), "openwhispr-markdown-mirror-"));
t.after(() => fs.rmSync(basePath, { recursive: true, force: true }));