Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

feat(search): handle tags with spaces in search queries (#783)

Corentin Thomassetad575bfc6209
diff --git a/.changeset/lovely-cooks-decide.md b/.changeset/lovely-cooks-decide.md
new file mode 100644
index 00000000..269d4351
--- /dev/null
+++ b/.changeset/lovely-cooks-decide.md
@@ -0,0 +1,5 @@
+---
+"@papra/docker": patch
+---
+
+Fix tag automatic search link issue when tags have spaces, before `tag:my tag` would not work, now generate `tag:"my tag"` to fix it.
diff --git a/apps/papra-client/src/modules/documents/document.models.test.ts b/apps/papra-client/src/modules/documents/document.models.test.ts
index 6c7894e9..29917607 100644
--- a/apps/papra-client/src/modules/documents/document.models.test.ts
+++ b/apps/papra-client/src/modules/documents/document.models.test.ts
@@ -279,6 +279,26 @@ describe('files models', () => {
).to.eql('tag:invoices tag:receipts tag:tag_2222');
});
+ test('tags with spaces in their name are quoted in the search query', () => {
+ expect(
+ makeDocumentSearchQuery({ tags: [{ name: 'my tag' }] }),
+ ).to.eql(
+ 'tag:"my tag"',
+ );
+
+ expect(
+ makeDocumentSearchQuery({ tags: [{ name: 'tag with "quotes"' }] }),
+ ).to.eql(
+ 'tag:"tag with \\"quotes\\""',
+ );
+
+ expect(
+ makeDocumentSearchQuery({ tags: [{ id: 'tag_1234', name: 'escaped \\"quote' }] }),
+ ).to.eql(
+ `tag:"escaped \\\\\\"quote"`,
+ );
+ });
+
test('the tags are sorted alphabetically in the search query for deterministic results', () => {
expect(
makeDocumentSearchQuery({
diff --git a/apps/papra-client/src/modules/documents/document.models.ts b/apps/papra-client/src/modules/documents/document.models.ts
index ab204ca4..c8a5a1c4 100644
--- a/apps/papra-client/src/modules/documents/document.models.ts
+++ b/apps/papra-client/src/modules/documents/document.models.ts
@@ -184,8 +184,10 @@ type DocumentSearchCriteria = {
};
export function makeDocumentSearchQuery({ tags = [], query }: DocumentSearchCriteria = {}) {
+ const quoteIfSpaces = (str: string) => (str.includes(' ') ? `"${str.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"` : str);
+
return [
- ...tags.map(t => `tag:${t.name ?? t.id}`).toSorted(),
+ ...tags.map(t => `tag:${quoteIfSpaces(t.name ?? t.id ?? '')}`).toSorted(),
query,
].filter(Boolean).join(' ');
}