dsh-reading-inbox 0.2.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/search.js ADDED
@@ -0,0 +1,109 @@
1
+ const STATUS_VALUES = new Set(['unread', 'reading', 'read', 'archived'])
2
+ const DATE_RE = /^\d{4}-\d{2}-\d{2}$/
3
+
4
+ function cleanValue(value) {
5
+ return String(value ?? '').replace(/^["']|["']$/g, '').trim()
6
+ }
7
+
8
+ /**
9
+ * Split a search string into free text and field filters:
10
+ * tag:security is:unread site:example.com after:2026-01-01
11
+ * before:2026-12-31 has:highlight has:annotation
12
+ *
13
+ * Unknown `field:value` tokens stay part of the free-text query so a URL or
14
+ * colon in normal prose is not silently swallowed.
15
+ */
16
+ export function parseQuery(raw) {
17
+ const parsed = {
18
+ text: '',
19
+ statuses: [],
20
+ tags: [],
21
+ sites: [],
22
+ after: '',
23
+ before: '',
24
+ hasHighlight: false,
25
+ }
26
+ const tokens = String(raw ?? '').match(/(?:[^\s"]+|"[^"]*")+/g) ?? []
27
+ const text = []
28
+
29
+ for (const token of tokens) {
30
+ const tag = /^tag:(.+)$/i.exec(token)
31
+ if (tag !== null) {
32
+ const value = cleanValue(tag[1]).toLowerCase()
33
+ if (value !== '') parsed.tags.push(value)
34
+ continue
35
+ }
36
+ const is = /^is:(.+)$/i.exec(token)
37
+ if (is !== null) {
38
+ const value = cleanValue(is[1]).toLowerCase()
39
+ if (STATUS_VALUES.has(value)) {
40
+ parsed.statuses.push(value)
41
+ } else if (value === 'highlight' || value === 'annotated' || value === 'annotation') {
42
+ parsed.hasHighlight = true
43
+ } else {
44
+ text.push(token)
45
+ }
46
+ continue
47
+ }
48
+ const site = /^site:(.+)$/i.exec(token)
49
+ if (site !== null) {
50
+ const value = cleanValue(site[1]).toLowerCase()
51
+ if (value !== '') parsed.sites.push(value)
52
+ continue
53
+ }
54
+ const after = /^after:(.+)$/i.exec(token)
55
+ if (after !== null && DATE_RE.test(cleanValue(after[1]))) {
56
+ parsed.after = cleanValue(after[1])
57
+ continue
58
+ }
59
+ const before = /^before:(.+)$/i.exec(token)
60
+ if (before !== null && DATE_RE.test(cleanValue(before[1]))) {
61
+ parsed.before = cleanValue(before[1])
62
+ continue
63
+ }
64
+ const has = /^has:(highlight|annotation|annotations)$/i.exec(token)
65
+ if (has !== null) {
66
+ parsed.hasHighlight = true
67
+ continue
68
+ }
69
+ text.push(token)
70
+ }
71
+
72
+ parsed.text = text.join(' ').trim()
73
+ return parsed
74
+ }
75
+
76
+ export function queryTerms(parsed) {
77
+ const terms = []
78
+ for (const term of String(parsed?.text ?? '').split(/\s+/)) {
79
+ const value = term.replace(/^["']|["']$/g, '').trim()
80
+ if (value !== '' && !terms.includes(value)) terms.push(value)
81
+ }
82
+ return terms
83
+ }
84
+
85
+ /**
86
+ * Return a short plain-text window around the first matched term. The UI
87
+ * escapes this text; it is not HTML and must never be injected as markup.
88
+ */
89
+ export function makeSnippet(text, terms, maxLength = 180) {
90
+ const normalized = String(text ?? '').replace(/\s+/g, ' ').trim()
91
+ if (normalized === '') return ''
92
+ const haystack = normalized.toLowerCase()
93
+ let index = -1
94
+ for (const term of terms) {
95
+ const candidate = String(term ?? '').toLowerCase()
96
+ if (candidate === '') continue
97
+ const found = haystack.indexOf(candidate)
98
+ if (found !== -1 && (index === -1 || found < index)) index = found
99
+ }
100
+ if (index === -1) {
101
+ return normalized.length > maxLength ? `${normalized.slice(0, maxLength).trimEnd()}…` : normalized
102
+ }
103
+ const before = Math.floor(maxLength * 0.38)
104
+ const start = Math.max(0, index - before)
105
+ const end = Math.min(normalized.length, start + maxLength)
106
+ const prefix = start > 0 ? '…' : ''
107
+ const suffix = end < normalized.length ? '…' : ''
108
+ return `${prefix}${normalized.slice(start, end)}${suffix}`
109
+ }