Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots 📖

fix(ai): unwrap a description whose quoted value starts with a symbol

davbio17ea91b0d899
diff --git a/src/core/capture/ai/__tests__/text.test.ts b/src/core/capture/ai/__tests__/text.test.ts
index fde7198..1aaeb0f 100644
--- a/src/core/capture/ai/__tests__/text.test.ts
+++ b/src/core/capture/ai/__tests__/text.test.ts
@@ -40,6 +40,26 @@ describe('unwrapQuotes', () => {
expect(unwrapQuotes('"在Role下拉菜单中选择"Admin"选项"')).toBe('"在Role下拉菜单中选择"Admin"选项"');
});
+ it('leaves two quoted terms written without spaces as they are', () => {
+ expect(unwrapQuotes('"设置"页面中点击"保存"')).toBe('"设置"页面中点击"保存"');
+ });
+
+ it('unwraps a whole description quoting a value that starts or ends with a symbol', () => {
+ expect(unwrapQuotes('"Click "+ New""')).toBe('Click "+ New"');
+ expect(unwrapQuotes('"Type "$100" in Amount"')).toBe('Type "$100" in Amount');
+ expect(unwrapQuotes('"Select "(GMT-05:00) Eastern" from Time zone"')).toBe(
+ 'Select "(GMT-05:00) Eastern" from Time zone',
+ );
+ });
+
+ it('drops the stray opening quote of a reply cut off by the token limit', () => {
+ expect(unwrapQuotes('"Click Save')).toBe('Click Save');
+ });
+
+ it('leaves a lone quote standing between two spaces', () => {
+ expect(unwrapQuotes('"Type " in Email"')).toBe('"Type " in Email"');
+ });
+
it('leaves an unquoted sentence alone', () => {
expect(unwrapQuotes('Type "abc" in Email')).toBe('Type "abc" in Email');
});
diff --git a/src/core/capture/ai/text.ts b/src/core/capture/ai/text.ts
index a680d3a..5308332 100644
--- a/src/core/capture/ai/text.ts
+++ b/src/core/capture/ai/text.ts
@@ -1,21 +1,38 @@
+const SPACE = /\s/u;
const WORD = /[\p{L}\p{N}]/u;
+function opensQuote(inner: string, i: number): boolean | null {
+ const spaceLeft = i === 0 || SPACE.test(inner[i - 1]);
+ const spaceRight = i === inner.length - 1 || SPACE.test(inner[i + 1]);
+ if (spaceLeft !== spaceRight) return spaceLeft;
+ if (spaceLeft) return null;
+ const wordLeft = WORD.test(inner[i - 1]);
+ const wordRight = WORD.test(inner[i + 1]);
+ return wordLeft === wordRight ? null : wordRight;
+}
+
function quotesPairUp(inner: string): boolean {
let open = 0;
for (let i = 0; i < inner.length; i++) {
if (inner[i] !== '"') continue;
- const wordLeft = i > 0 && WORD.test(inner[i - 1]);
- const wordRight = i < inner.length - 1 && WORD.test(inner[i + 1]);
- if (wordLeft === wordRight) return false;
- if (wordRight) open++;
+ const opens = opensQuote(inner, i);
+ if (opens === null) return false;
+ if (opens) open++;
else if (open-- === 0) return false;
}
return open === 0;
}
+function withoutStrayQuote(trimmed: string): string {
+ if (trimmed.length < 2 || trimmed.indexOf('"') !== trimmed.lastIndexOf('"')) return trimmed;
+ if (trimmed.startsWith('"')) return trimmed.slice(1).trim();
+ if (trimmed.endsWith('"')) return trimmed.slice(0, -1).trim();
+ return trimmed;
+}
+
export function unwrapQuotes(text: string): string {
const trimmed = text.trim();
const wrapped =
trimmed.length >= 2 && trimmed.startsWith('"') && trimmed.endsWith('"') && quotesPairUp(trimmed.slice(1, -1));
- return wrapped ? trimmed.slice(1, -1).trim() : trimmed;
+ return wrapped ? trimmed.slice(1, -1).trim() : withoutStrayQuote(trimmed);
}