Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (claude): fix quote-reattach regex edge cases (opening quotes, idempotency)

[._.]/ Adam Eivyc43ee9567e8c
diff --git a/client/src/lib/manuscriptFormat.js b/client/src/lib/manuscriptFormat.js
index a1f1068a88..9be0f992c1 100644
--- a/client/src/lib/manuscriptFormat.js
+++ b/client/src/lib/manuscriptFormat.js
@@ -58,16 +58,27 @@ const rejoinDropCaps = (text) => text.replace(/^([A-Z])\n(?=[a-z])/gm, '$1');
// a closing quote is one preceded by text / whitespace, never one immediately
// followed by a word, which is how an opening quote reads.)
const reattachQuotes = (text) => text
- // A `"` alone on its own line collapses up onto the previous line:
+ // A `"` alone on its own line collapses up onto the previous NON-BLANK line.
+ // The `(\S)` anchor both keeps the quote off a blank line and makes the pass
+ // idempotent — without it a quote sitting after a blank line would hop one
+ // line closer per run instead of stabilizing.
// …diagnostics.\n"\n— Maggie → …diagnostics."\n— Maggie
- .replace(/\n[ \t]*"[ \t]*(?=\n|$)/g, '"')
+ .replace(/(\S)\n[ \t]*"[ \t]*(?=\n|$)/g, '$1"')
// A `"` that wrapped to the START of the next line, followed by more text,
// hugs the previous line instead (no inserted space — it's a closing quote):
// Panel Seven,\n" I say → Panel Seven," I say
+ // Accepted miss: a non-standard opening quote written with a space after it
+ // at line start (`\n" Wait`) is pulled up too; standard opening quotes hug
+ // their word (`"Wait`) and are untouched. Not worth tightening — the obvious
+ // guard (skip when the previous line ends in terminal punctuation) would
+ // regress the common closing-quote-after-a-sentence case (`gone.\n" she`).
.replace(/\n[ \t]*"(?=[ \t]+\S)/g, '"')
- // A stray space before a closing `"` at end of line:
+ // A stray space before a closing `"` at end of line — but only after
+ // sentence-terminating punctuation, the real "stray space" shape. Gating on
+ // [.!?] leaves an OPENING quote whose dialogue wrapped (`said, "\nGood`)
+ // alone, since it sits after a comma, not a sentence-ender.
// relationships. " → relationships."
- .replace(/[ \t]+"(?=[ \t]*$)/gm, '"');
+ .replace(/([.!?])[ \t]+"(?=[ \t]*$)/gm, '$1"');
// Collapse 3+ consecutive newlines to a single blank line, then trim the ends.
const tidyBlankLines = (text) => text.replace(/\n{3,}/g, '\n\n').trim();
diff --git a/client/src/lib/manuscriptFormat.test.js b/client/src/lib/manuscriptFormat.test.js
index 5e2c405fcc..9fe2ce9c27 100644
--- a/client/src/lib/manuscriptFormat.test.js
+++ b/client/src/lib/manuscriptFormat.test.js
@@ -107,6 +107,27 @@ describe('formatManuscript — orphaned closing quotes (prose)', () => {
expect(formatManuscript(clean, 'prose')).toBe(clean);
});
+ it('leaves an opening quote whose dialogue wrapped to the next line intact', () => {
+ // `said, "` ends a line because the OPENING quote wrapped — the space sits
+ // after a comma, not a sentence-ender, so it must not be eaten like a stray
+ // space before a closing quote.
+ const input = 'Maggie said, "\nGood morning.';
+ expect(formatManuscript(input, 'prose')).toBe('Maggie said, "\nGood morning.');
+ });
+
+ it('is idempotent over dirty quote inputs (re-running format = running once)', () => {
+ const cases = [
+ 'A line of prose.\n"\n— Author', // lone closing quote
+ 'Some text.\n\n"\n— Author', // lone closing quote after a blank line
+ '"Good morning,\n" I say, and continue.', // wrapped closing quote
+ '"You look stable. "', // stray space before closing quote
+ ];
+ for (const dirty of cases) {
+ const once = formatManuscript(dirty, 'prose');
+ expect(formatManuscript(once, 'prose')).toBe(once);
+ }
+ });
+
it('cleans the full pasted epigraph + dialogue block end to end', () => {
const input = [
'Chapter 1: Anomalous Readings',