opencode-search 1.0.0-next.4 → 1.0.0-next.7
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/dist/package.json +4 -2
- package/dist/src/providers/gnews-search.d.ts +1 -1
- package/dist/src/providers/gnews-search.d.ts.map +1 -1
- package/dist/src/providers/gnews-search.js +5 -3
- package/dist/src/providers/gnews-search.js.map +1 -1
- package/dist/src/search.d.ts.map +1 -1
- package/dist/src/search.js +28 -18
- package/dist/src/search.js.map +1 -1
- package/package.json +4 -2
- package/search.ts +22 -0
- package/src/cache.test.ts +14 -0
- package/src/cache.ts +61 -0
- package/src/format.test.ts +175 -0
- package/src/format.ts +72 -0
- package/src/html.ts +39 -0
- package/src/normalize-blurb.test.ts +67 -0
- package/src/normalize-blurb.ts +22 -0
- package/src/opencode/notify.ts +16 -0
- package/src/parse-results.test.ts +75 -0
- package/src/parse-results.ts +22 -0
- package/src/providers/brave-search.ts +39 -0
- package/src/providers/bsky-search.test.ts +16 -0
- package/src/providers/bsky-search.ts +88 -0
- package/src/providers/ddg-search.test.ts +15 -0
- package/src/providers/ddg-search.ts +117 -0
- package/src/providers/gnews-search.ts +72 -0
- package/src/providers/mdn-search.test.ts +17 -0
- package/src/providers/mdn-search.ts +83 -0
- package/src/providers/standard-search.test.ts +29 -0
- package/src/providers/standard-search.ts +94 -0
- package/src/providers/wiki-search.test.ts +16 -0
- package/src/providers/wiki-search.ts +72 -0
- package/src/renderers.test.ts +327 -0
- package/src/renderers.ts +51 -0
- package/src/safe.test.ts +87 -0
- package/src/safe.ts +27 -0
- package/src/search.ts +310 -0
- package/src/strategies.test.ts +92 -0
- package/src/strategies.ts +62 -0
- package/src/user-agent.ts +3 -0
- package/src/v1-message.test.ts +17 -0
- package/src/v1-message.ts +10 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { z } from 'zod'
|
|
2
|
+
import * as cache from '../cache.ts'
|
|
3
|
+
import { USER_AGENT } from '../user-agent.ts'
|
|
4
|
+
|
|
5
|
+
const MDN_API = 'https://developer.mozilla.org/api/v1/search'
|
|
6
|
+
|
|
7
|
+
const documentSchema = z.object({
|
|
8
|
+
mdn_url: z.string().min(1),
|
|
9
|
+
title: z.string().min(1),
|
|
10
|
+
summary: z.string().default(''),
|
|
11
|
+
slug: z.string().default(''),
|
|
12
|
+
locale: z.string().default('en-US'),
|
|
13
|
+
popularity: z.number().default(0),
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
const responseSchema = z.object({
|
|
17
|
+
documents: z.array(z.unknown()),
|
|
18
|
+
metadata: z.object({
|
|
19
|
+
total: z.object({
|
|
20
|
+
value: z.number(),
|
|
21
|
+
}),
|
|
22
|
+
}).optional(),
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
export type MdnDocument = z.infer<typeof documentSchema>
|
|
26
|
+
|
|
27
|
+
export type MdnSearchResult = {
|
|
28
|
+
documents: Array<MdnDocument>
|
|
29
|
+
total: number
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
type MdnSearchOptions = {
|
|
33
|
+
query: string
|
|
34
|
+
limit?: number
|
|
35
|
+
page?: number
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const mdnSearch = async (options: MdnSearchOptions): Promise<MdnSearchResult> => {
|
|
39
|
+
const cacheKey = [
|
|
40
|
+
'mdn',
|
|
41
|
+
options.query,
|
|
42
|
+
options.limit ?? 10,
|
|
43
|
+
options.page ?? 1,
|
|
44
|
+
].join(':')
|
|
45
|
+
|
|
46
|
+
const cached = await cache.get<MdnSearchResult>(cacheKey)
|
|
47
|
+
if (cached && cached.documents && cached.documents.length > 0) return cached
|
|
48
|
+
|
|
49
|
+
const params = new URLSearchParams({
|
|
50
|
+
q: options.query,
|
|
51
|
+
locale: 'en-US',
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
if (options.limit) params.set('size', String(options.limit))
|
|
55
|
+
|
|
56
|
+
if (options.page) params.set('page', String(options.page))
|
|
57
|
+
|
|
58
|
+
const url = MDN_API + '?' + params.toString()
|
|
59
|
+
const res = await fetch(url, {
|
|
60
|
+
headers: { 'User-Agent': USER_AGENT },
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
if (!res.ok) {
|
|
64
|
+
const text = await res.text()
|
|
65
|
+
throw new Error('MDN search error (' + res.status + '): ' + text)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const json: unknown = await res.json()
|
|
69
|
+
const response = responseSchema.parse(json)
|
|
70
|
+
|
|
71
|
+
const documents = response.documents.flatMap((raw) => {
|
|
72
|
+
const parsed = documentSchema.safeParse(raw)
|
|
73
|
+
return parsed.success ? [parsed.data] : []
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
const total = response.metadata?.total.value ?? documents.length
|
|
77
|
+
|
|
78
|
+
const result: MdnSearchResult = { documents, total }
|
|
79
|
+
|
|
80
|
+
if (documents.length > 0) await cache.set(cacheKey, result)
|
|
81
|
+
|
|
82
|
+
return result
|
|
83
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test'
|
|
2
|
+
import { safeAsync } from '../safe.ts'
|
|
3
|
+
import { standardSearch } from './standard-search.ts'
|
|
4
|
+
|
|
5
|
+
const isServiceDown = (error: Error): boolean => (
|
|
6
|
+
error.message.includes('Unable to connect') || error.message.includes('ConnectionRefused')
|
|
7
|
+
)
|
|
8
|
+
|
|
9
|
+
describe('standardSearch', () => {
|
|
10
|
+
it('returns document results', async () => {
|
|
11
|
+
const { data: results, error } = await safeAsync(() => standardSearch({ query: 'atproto' }))
|
|
12
|
+
if (error) {
|
|
13
|
+
// An unreachable appview host is a service outage, not a plugin bug
|
|
14
|
+
if (isServiceDown(error)) {
|
|
15
|
+
console.warn('standard-search appview unreachable, skipping live assertion')
|
|
16
|
+
return
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
throw error
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
expect(results.documents.length > 0).toEqual(true)
|
|
23
|
+
|
|
24
|
+
const first = results.documents[0]
|
|
25
|
+
if (!first) throw new Error('expected at least one document')
|
|
26
|
+
expect(first.title).toBeDefined()
|
|
27
|
+
expect(first.url).toBeDefined()
|
|
28
|
+
})
|
|
29
|
+
})
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { JSDOM, VirtualConsole } from 'jsdom'
|
|
2
|
+
import * as cache from '../cache.ts'
|
|
3
|
+
|
|
4
|
+
const SEARCH_URL = 'https://standard-search.octet-stream.net/search'
|
|
5
|
+
|
|
6
|
+
export type StandardDocument = {
|
|
7
|
+
title: string
|
|
8
|
+
url: string
|
|
9
|
+
date: string
|
|
10
|
+
snippet: string
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type StandardSearchResult = {
|
|
14
|
+
documents: Array<StandardDocument>
|
|
15
|
+
totalResults: string
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const parseDocument = (el: Element): StandardDocument | null => {
|
|
19
|
+
const titleEl = el.querySelector('.result-title a')
|
|
20
|
+
if (!titleEl || !titleEl.textContent) return null
|
|
21
|
+
|
|
22
|
+
const title = titleEl.textContent.trim()
|
|
23
|
+
const href = titleEl.getAttribute('href')
|
|
24
|
+
|
|
25
|
+
const urlEl = el.querySelector('.result-url')
|
|
26
|
+
const urlText = urlEl && urlEl.textContent ? urlEl.textContent : ''
|
|
27
|
+
|
|
28
|
+
const snippetEl = el.querySelector('.result-snippet')
|
|
29
|
+
const snippet = snippetEl && snippetEl.textContent ? snippetEl.textContent : ''
|
|
30
|
+
|
|
31
|
+
const parts = urlText.split(' \u00B7 ')
|
|
32
|
+
const datePart = parts.length > 1 ? parts[1] : ''
|
|
33
|
+
|
|
34
|
+
return {
|
|
35
|
+
title,
|
|
36
|
+
url: href ? href.trim() : '',
|
|
37
|
+
date: datePart ? datePart.trim() : '',
|
|
38
|
+
snippet: snippet.trim(),
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
type StandardSearchOptions = {
|
|
43
|
+
query: string
|
|
44
|
+
limit?: number
|
|
45
|
+
offset?: number
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export const standardSearch = async (options: StandardSearchOptions): Promise<StandardSearchResult> => {
|
|
49
|
+
const cacheKey = ['standard', options.query, options.limit ?? 0, options.offset ?? 0].join(':')
|
|
50
|
+
|
|
51
|
+
const cached = await cache.get<StandardSearchResult>(cacheKey)
|
|
52
|
+
if (cached && cached.documents && cached.documents.length > 0) return cached
|
|
53
|
+
|
|
54
|
+
const url = SEARCH_URL + '?q=' + encodeURIComponent(options.query)
|
|
55
|
+
const res = await fetch(url)
|
|
56
|
+
|
|
57
|
+
if (!res.ok) throw new Error('Standard Search error (' + res.status + '): ' + res.statusText)
|
|
58
|
+
|
|
59
|
+
const html = await res.text()
|
|
60
|
+
|
|
61
|
+
const virtualConsole = new VirtualConsole()
|
|
62
|
+
virtualConsole.on('jsdomError', () => {})
|
|
63
|
+
|
|
64
|
+
const dom = new JSDOM(html, { url, virtualConsole })
|
|
65
|
+
const doc = dom.window.document
|
|
66
|
+
|
|
67
|
+
const countEl = doc.querySelector('.result-count')
|
|
68
|
+
const countText = countEl ? countEl.textContent : ''
|
|
69
|
+
const totalResults = countText ? countText.trim() : '0'
|
|
70
|
+
|
|
71
|
+
const elements = doc.querySelectorAll('.result')
|
|
72
|
+
const documents: Array<StandardDocument> = []
|
|
73
|
+
|
|
74
|
+
for (let i = 0; i < elements.length; i++) {
|
|
75
|
+
const el = elements[i]
|
|
76
|
+
if (!el) continue
|
|
77
|
+
|
|
78
|
+
const parsed = parseDocument(el)
|
|
79
|
+
if (parsed) documents.push(parsed)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const start = options.offset ?? 0
|
|
83
|
+
const end = options.limit ? start + options.limit : documents.length
|
|
84
|
+
const sliced = documents.slice(start, end)
|
|
85
|
+
|
|
86
|
+
const result: StandardSearchResult = {
|
|
87
|
+
documents: sliced,
|
|
88
|
+
totalResults,
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (sliced.length > 0) await cache.set(cacheKey, result)
|
|
92
|
+
|
|
93
|
+
return result
|
|
94
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test'
|
|
2
|
+
import { wikiSearch } from './wiki-search.ts'
|
|
3
|
+
|
|
4
|
+
describe('wikiSearch', () => {
|
|
5
|
+
it('returns page results', async () => {
|
|
6
|
+
const results = await wikiSearch({ query: 'typescript programming' })
|
|
7
|
+
|
|
8
|
+
expect(results.pages.length > 0).toEqual(true)
|
|
9
|
+
|
|
10
|
+
const first = results.pages[0]
|
|
11
|
+
if (!first) throw new Error('expected at least one page')
|
|
12
|
+
expect(first.title).toBeDefined()
|
|
13
|
+
expect(first.key).toBeDefined()
|
|
14
|
+
expect(first.excerpt).toBeDefined()
|
|
15
|
+
})
|
|
16
|
+
})
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { z } from 'zod'
|
|
2
|
+
import * as cache from '../cache.ts'
|
|
3
|
+
import { normalizeBlurb } from '../normalize-blurb.ts'
|
|
4
|
+
import { USER_AGENT } from '../user-agent.ts'
|
|
5
|
+
|
|
6
|
+
const WIKI_API = 'https://en.wikipedia.org/w/rest.php/v1/search/page'
|
|
7
|
+
|
|
8
|
+
const thumbnailSchema = z.object({
|
|
9
|
+
url: z.string().min(1),
|
|
10
|
+
width: z.number(),
|
|
11
|
+
height: z.number(),
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
const pageSchema = z.object({
|
|
15
|
+
id: z.number().default(0),
|
|
16
|
+
key: z.string().min(1),
|
|
17
|
+
title: z.string().min(1),
|
|
18
|
+
excerpt: z.string().default('').transform(normalizeBlurb),
|
|
19
|
+
description: z.string().nullable().default(null),
|
|
20
|
+
matched_title: z.string().nullable().default(null),
|
|
21
|
+
thumbnail: thumbnailSchema.nullable().default(null),
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
const responseSchema = z.object({
|
|
25
|
+
pages: z.array(z.unknown()),
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
export type WikiPage = z.infer<typeof pageSchema>
|
|
29
|
+
|
|
30
|
+
export type WikiSearchResult = {
|
|
31
|
+
pages: Array<WikiPage>
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
type WikiSearchOptions = {
|
|
35
|
+
query: string
|
|
36
|
+
limit?: number
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export const wikiSearch = async (options: WikiSearchOptions): Promise<WikiSearchResult> => {
|
|
40
|
+
const cacheKey = 'wiki:' + options.query + ':' + (options.limit ?? 10)
|
|
41
|
+
|
|
42
|
+
const cached = await cache.get<WikiSearchResult>(cacheKey)
|
|
43
|
+
if (cached && cached.pages && cached.pages.length > 0) return cached
|
|
44
|
+
|
|
45
|
+
const params = new URLSearchParams({ q: options.query })
|
|
46
|
+
|
|
47
|
+
if (options.limit) params.set('limit', String(options.limit))
|
|
48
|
+
|
|
49
|
+
const url = WIKI_API + '?' + params.toString()
|
|
50
|
+
const res = await fetch(url, {
|
|
51
|
+
headers: { 'User-Agent': USER_AGENT },
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
if (!res.ok) {
|
|
55
|
+
const text = await res.text()
|
|
56
|
+
throw new Error('Wikipedia search error (' + res.status + '): ' + text)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const json: unknown = await res.json()
|
|
60
|
+
const response = responseSchema.parse(json)
|
|
61
|
+
|
|
62
|
+
const pages = response.pages.flatMap((raw) => {
|
|
63
|
+
const parsed = pageSchema.safeParse(raw)
|
|
64
|
+
return parsed.success ? [parsed.data] : []
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
const result: WikiSearchResult = { pages }
|
|
68
|
+
|
|
69
|
+
if (pages.length > 0) await cache.set(cacheKey, result)
|
|
70
|
+
|
|
71
|
+
return result
|
|
72
|
+
}
|
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test'
|
|
2
|
+
import {
|
|
3
|
+
renderBskyPost,
|
|
4
|
+
renderDdgResult,
|
|
5
|
+
renderGnewsItem,
|
|
6
|
+
renderMdnDoc,
|
|
7
|
+
renderStandardDoc,
|
|
8
|
+
renderWebResult,
|
|
9
|
+
renderWikiPage,
|
|
10
|
+
} from './renderers.ts'
|
|
11
|
+
|
|
12
|
+
describe('renderDdgResult', () => {
|
|
13
|
+
it('returns three lines: title, url, abstract', () => {
|
|
14
|
+
const result = { title: 'TypeScript', url: 'https://typescriptlang.org', abstract: 'A typed superset of JavaScript' }
|
|
15
|
+
const lines = renderDdgResult(result, 0).split('\n')
|
|
16
|
+
expect(lines).toHaveLength(3)
|
|
17
|
+
expect(lines[0]).toContain('1. TypeScript')
|
|
18
|
+
expect(lines[1]).toContain('https://typescriptlang.org')
|
|
19
|
+
expect(lines[2]).toContain('A typed superset of JavaScript')
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it('uses 1-based index', () => {
|
|
23
|
+
const result = { title: 'Test', url: 'https://example.com', abstract: 'Abstract' }
|
|
24
|
+
const lines = renderDdgResult(result, 4).split('\n')
|
|
25
|
+
expect(lines[0]).toStartWith('5. ')
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
it('indents url and abstract lines', () => {
|
|
29
|
+
const result = { title: 'Test', url: 'https://example.com', abstract: 'Abstract' }
|
|
30
|
+
const lines = renderDdgResult(result, 0).split('\n')
|
|
31
|
+
expect(lines[1]).toStartWith(' ')
|
|
32
|
+
expect(lines[2]).toStartWith(' ')
|
|
33
|
+
})
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
describe('renderWebResult', () => {
|
|
37
|
+
it('returns three lines: title, url, abstract', () => {
|
|
38
|
+
const result = { title: 'TypeScript', url: 'https://typescriptlang.org', abstract: 'A typed superset of JavaScript' }
|
|
39
|
+
const lines = renderWebResult(result, 0).split('\n')
|
|
40
|
+
expect(lines).toHaveLength(3)
|
|
41
|
+
expect(lines[0]).toContain('1. TypeScript')
|
|
42
|
+
expect(lines[1]).toContain('https://typescriptlang.org')
|
|
43
|
+
expect(lines[2]).toContain('A typed superset of JavaScript')
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('uses 1-based index', () => {
|
|
47
|
+
const result = { title: 'Test', url: 'https://example.com', abstract: 'Abstract' }
|
|
48
|
+
const lines = renderWebResult(result, 4).split('\n')
|
|
49
|
+
expect(lines[0]).toStartWith('5. ')
|
|
50
|
+
})
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
describe('renderGnewsItem', () => {
|
|
54
|
+
const item = {
|
|
55
|
+
title: 'Rust 2.0 announced',
|
|
56
|
+
url: 'https://news.google.com/rss/articles/CBMiabc',
|
|
57
|
+
abstract: '',
|
|
58
|
+
source: 'The Register',
|
|
59
|
+
date: 'Fri, 18 Sep 2026 16:55:50 GMT',
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
it('returns three lines: title, source and date, url', () => {
|
|
63
|
+
const lines = renderGnewsItem(item, 0).split('\n')
|
|
64
|
+
|
|
65
|
+
expect(lines).toHaveLength(3)
|
|
66
|
+
expect(lines[0]).toContain('1. Rust 2.0 announced')
|
|
67
|
+
expect(lines[1]).toContain('The Register')
|
|
68
|
+
expect(lines[2]).toContain('news.google.com')
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
it('omits the separator when there is no date', () => {
|
|
72
|
+
const lines = renderGnewsItem({ ...item, date: '' }, 0).split('\n')
|
|
73
|
+
|
|
74
|
+
expect(lines[1]).not.toContain('—')
|
|
75
|
+
})
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
describe('renderBskyPost', () => {
|
|
79
|
+
const post = {
|
|
80
|
+
uri: 'at://did:plc:test/app.bsky.feed.post/abc',
|
|
81
|
+
cid: 'cid123',
|
|
82
|
+
author: {
|
|
83
|
+
did: 'did:plc:test',
|
|
84
|
+
handle: 'alice.bsky.social',
|
|
85
|
+
displayName: 'Alice',
|
|
86
|
+
},
|
|
87
|
+
record: {
|
|
88
|
+
text: 'Hello world from Bluesky!',
|
|
89
|
+
createdAt: '2025-06-15T14:30:00.000Z',
|
|
90
|
+
},
|
|
91
|
+
likeCount: 42,
|
|
92
|
+
repostCount: 7,
|
|
93
|
+
replyCount: 3,
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
it('returns three lines', () => {
|
|
97
|
+
const lines = renderBskyPost(post, 0).split('\n')
|
|
98
|
+
expect(lines).toHaveLength(3)
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it('includes index and author handle on first line', () => {
|
|
102
|
+
const lines = renderBskyPost(post, 0).split('\n')
|
|
103
|
+
expect(lines[0]).toContain('1. @alice.bsky.social')
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
it('uses 1-based index', () => {
|
|
107
|
+
const lines = renderBskyPost(post, 4).split('\n')
|
|
108
|
+
expect(lines[0]).toStartWith('5. @')
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
it('includes formatted date with time on first line', () => {
|
|
112
|
+
const lines = renderBskyPost(post, 0).split('\n')
|
|
113
|
+
expect(lines[0]).toContain('Jun')
|
|
114
|
+
expect(lines[0]).toContain('2025')
|
|
115
|
+
expect(lines[0]).toMatch(/AM|PM/)
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
it('includes post text on second line', () => {
|
|
119
|
+
const lines = renderBskyPost(post, 0).split('\n')
|
|
120
|
+
expect(lines[1]).toContain('Hello world from Bluesky!')
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
it('includes engagement counts on third line', () => {
|
|
124
|
+
const lines = renderBskyPost(post, 0).split('\n')
|
|
125
|
+
expect(lines[2]).toContain('Likes: 42')
|
|
126
|
+
expect(lines[2]).toContain('Reposts: 7')
|
|
127
|
+
expect(lines[2]).toContain('Replies: 3')
|
|
128
|
+
})
|
|
129
|
+
|
|
130
|
+
it('indents second and third lines', () => {
|
|
131
|
+
const lines = renderBskyPost(post, 0).split('\n')
|
|
132
|
+
expect(lines[1]).toStartWith(' ')
|
|
133
|
+
expect(lines[2]).toStartWith(' ')
|
|
134
|
+
})
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
describe('renderStandardDoc', () => {
|
|
138
|
+
it('includes date in title line when date is present', () => {
|
|
139
|
+
const doc = {
|
|
140
|
+
title: 'My Article',
|
|
141
|
+
url: 'https://example.com/article',
|
|
142
|
+
date: '2025-03-10',
|
|
143
|
+
snippet: 'Article snippet text',
|
|
144
|
+
}
|
|
145
|
+
const lines = renderStandardDoc(doc, 0).split('\n')
|
|
146
|
+
expect(lines[0]).toContain('1. My Article')
|
|
147
|
+
expect(lines[0]).toContain('Mar')
|
|
148
|
+
expect(lines[0]).toContain('2025')
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
it('omits date from title line when date is empty', () => {
|
|
152
|
+
const doc = {
|
|
153
|
+
title: 'No Date Article',
|
|
154
|
+
url: 'https://example.com/nodate',
|
|
155
|
+
date: '',
|
|
156
|
+
snippet: 'Snippet',
|
|
157
|
+
}
|
|
158
|
+
const lines = renderStandardDoc(doc, 0).split('\n')
|
|
159
|
+
expect(lines[0]).toBe('1. No Date Article')
|
|
160
|
+
expect(lines[0]).not.toContain('(')
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
it('returns three lines: title, url, snippet', () => {
|
|
164
|
+
const doc = {
|
|
165
|
+
title: 'Title',
|
|
166
|
+
url: 'https://example.com',
|
|
167
|
+
date: '',
|
|
168
|
+
snippet: 'Snippet text',
|
|
169
|
+
}
|
|
170
|
+
const lines = renderStandardDoc(doc, 0).split('\n')
|
|
171
|
+
expect(lines).toHaveLength(3)
|
|
172
|
+
expect(lines[1]).toContain('https://example.com')
|
|
173
|
+
expect(lines[2]).toContain('Snippet text')
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
it('uses 1-based index', () => {
|
|
177
|
+
const doc = {
|
|
178
|
+
title: 'Title',
|
|
179
|
+
url: 'https://example.com',
|
|
180
|
+
date: '',
|
|
181
|
+
snippet: 'Snippet',
|
|
182
|
+
}
|
|
183
|
+
const lines = renderStandardDoc(doc, 9).split('\n')
|
|
184
|
+
expect(lines[0]).toStartWith('10. ')
|
|
185
|
+
})
|
|
186
|
+
|
|
187
|
+
it('indents url and snippet lines', () => {
|
|
188
|
+
const doc = {
|
|
189
|
+
title: 'Title',
|
|
190
|
+
url: 'https://example.com',
|
|
191
|
+
date: '',
|
|
192
|
+
snippet: 'Snippet',
|
|
193
|
+
}
|
|
194
|
+
const lines = renderStandardDoc(doc, 0).split('\n')
|
|
195
|
+
expect(lines[1]).toStartWith(' ')
|
|
196
|
+
expect(lines[2]).toStartWith(' ')
|
|
197
|
+
})
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
describe('renderWikiPage', () => {
|
|
201
|
+
it('includes title and wikipedia url', () => {
|
|
202
|
+
const result = renderWikiPage({
|
|
203
|
+
id: 1,
|
|
204
|
+
key: 'TypeScript',
|
|
205
|
+
title: 'TypeScript',
|
|
206
|
+
excerpt: 'A programming language',
|
|
207
|
+
description: 'Open-source language',
|
|
208
|
+
matched_title: null,
|
|
209
|
+
thumbnail: null,
|
|
210
|
+
}, 0)
|
|
211
|
+
expect(result).toContain('1. TypeScript')
|
|
212
|
+
expect(result).toContain('https://en.wikipedia.org/wiki/TypeScript')
|
|
213
|
+
})
|
|
214
|
+
|
|
215
|
+
it('includes description when present', () => {
|
|
216
|
+
const result = renderWikiPage({
|
|
217
|
+
id: 1,
|
|
218
|
+
key: 'TypeScript',
|
|
219
|
+
title: 'TypeScript',
|
|
220
|
+
excerpt: 'A programming language',
|
|
221
|
+
description: 'Open-source language',
|
|
222
|
+
matched_title: null,
|
|
223
|
+
thumbnail: null,
|
|
224
|
+
}, 0)
|
|
225
|
+
expect(result).toContain('Open-source language')
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
it('omits description when null', () => {
|
|
229
|
+
const result = renderWikiPage({
|
|
230
|
+
id: 2,
|
|
231
|
+
key: 'JavaScript',
|
|
232
|
+
title: 'JavaScript',
|
|
233
|
+
excerpt: 'A scripting language',
|
|
234
|
+
description: null,
|
|
235
|
+
matched_title: null,
|
|
236
|
+
thumbnail: null,
|
|
237
|
+
}, 0)
|
|
238
|
+
const lines = result.split('\n')
|
|
239
|
+
expect(lines).toHaveLength(3)
|
|
240
|
+
expect(lines[0]).toContain('1. JavaScript')
|
|
241
|
+
expect(lines[1]).toContain('https://en.wikipedia.org/wiki/JavaScript')
|
|
242
|
+
expect(lines[2]).toContain('A scripting language')
|
|
243
|
+
})
|
|
244
|
+
|
|
245
|
+
it('returns four lines when description is present', () => {
|
|
246
|
+
const lines = renderWikiPage({
|
|
247
|
+
id: 1,
|
|
248
|
+
key: 'TypeScript',
|
|
249
|
+
title: 'TypeScript',
|
|
250
|
+
excerpt: 'A programming language',
|
|
251
|
+
description: 'Open-source language',
|
|
252
|
+
matched_title: null,
|
|
253
|
+
thumbnail: null,
|
|
254
|
+
}, 0).split('\n')
|
|
255
|
+
expect(lines).toHaveLength(4)
|
|
256
|
+
})
|
|
257
|
+
|
|
258
|
+
it('uses 1-based index', () => {
|
|
259
|
+
const lines = renderWikiPage({
|
|
260
|
+
id: 1,
|
|
261
|
+
key: 'Test',
|
|
262
|
+
title: 'Test',
|
|
263
|
+
excerpt: 'Excerpt',
|
|
264
|
+
description: null,
|
|
265
|
+
matched_title: null,
|
|
266
|
+
thumbnail: null,
|
|
267
|
+
}, 2).split('\n')
|
|
268
|
+
expect(lines[0]).toStartWith('3. ')
|
|
269
|
+
})
|
|
270
|
+
|
|
271
|
+
it('indents all lines after the title', () => {
|
|
272
|
+
const lines = renderWikiPage({
|
|
273
|
+
id: 1,
|
|
274
|
+
key: 'Test',
|
|
275
|
+
title: 'Test',
|
|
276
|
+
excerpt: 'Excerpt',
|
|
277
|
+
description: 'Desc',
|
|
278
|
+
matched_title: null,
|
|
279
|
+
thumbnail: null,
|
|
280
|
+
}, 0).split('\n')
|
|
281
|
+
for (let i = 1; i < lines.length; i++) {
|
|
282
|
+
expect(lines[i]).toStartWith(' ')
|
|
283
|
+
}
|
|
284
|
+
})
|
|
285
|
+
})
|
|
286
|
+
|
|
287
|
+
describe('renderMdnDoc', () => {
|
|
288
|
+
const doc = {
|
|
289
|
+
mdn_url: '/en-US/docs/Web/API/Fetch_API',
|
|
290
|
+
title: 'Fetch API',
|
|
291
|
+
summary: 'The Fetch API provides an interface for fetching resources.',
|
|
292
|
+
slug: 'Web/API/Fetch_API',
|
|
293
|
+
locale: 'en-US',
|
|
294
|
+
popularity: 0.5,
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
it('returns three lines: title, url, summary', () => {
|
|
298
|
+
const lines = renderMdnDoc(doc, 0).split('\n')
|
|
299
|
+
expect(lines).toHaveLength(3)
|
|
300
|
+
})
|
|
301
|
+
|
|
302
|
+
it('includes index and title on first line', () => {
|
|
303
|
+
const lines = renderMdnDoc(doc, 0).split('\n')
|
|
304
|
+
expect(lines[0]).toBe('1. Fetch API')
|
|
305
|
+
})
|
|
306
|
+
|
|
307
|
+
it('uses 1-based index', () => {
|
|
308
|
+
const lines = renderMdnDoc(doc, 4).split('\n')
|
|
309
|
+
expect(lines[0]).toStartWith('5. ')
|
|
310
|
+
})
|
|
311
|
+
|
|
312
|
+
it('includes full MDN url on second line', () => {
|
|
313
|
+
const lines = renderMdnDoc(doc, 0).split('\n')
|
|
314
|
+
expect(lines[1]).toContain('https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API')
|
|
315
|
+
})
|
|
316
|
+
|
|
317
|
+
it('includes summary on third line', () => {
|
|
318
|
+
const lines = renderMdnDoc(doc, 0).split('\n')
|
|
319
|
+
expect(lines[2]).toContain('The Fetch API provides an interface for fetching resources.')
|
|
320
|
+
})
|
|
321
|
+
|
|
322
|
+
it('indents url and summary lines', () => {
|
|
323
|
+
const lines = renderMdnDoc(doc, 0).split('\n')
|
|
324
|
+
expect(lines[1]).toStartWith(' ')
|
|
325
|
+
expect(lines[2]).toStartWith(' ')
|
|
326
|
+
})
|
|
327
|
+
})
|
package/src/renderers.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { formatDate, formatDateTime } from './format.ts'
|
|
2
|
+
import { normalizeBlurb } from './normalize-blurb.ts'
|
|
3
|
+
import type { BskyPost } from './providers/bsky-search.ts'
|
|
4
|
+
import type { DdgResult } from './providers/ddg-search.ts'
|
|
5
|
+
import type { GnewsResult } from './providers/gnews-search.ts'
|
|
6
|
+
import type { MdnDocument } from './providers/mdn-search.ts'
|
|
7
|
+
import type { StandardDocument } from './providers/standard-search.ts'
|
|
8
|
+
import type { WikiPage } from './providers/wiki-search.ts'
|
|
9
|
+
|
|
10
|
+
export const renderDdgResult = (result: DdgResult, index: number): string => {
|
|
11
|
+
return `${index + 1}. ${result.title}
|
|
12
|
+
${result.url}
|
|
13
|
+
${normalizeBlurb(result.abstract)}`
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Brave and DuckDuckGo return the same title/url/abstract shape.
|
|
17
|
+
export const renderWebResult = renderDdgResult
|
|
18
|
+
|
|
19
|
+
export const renderGnewsItem = (result: GnewsResult, index: number): string => {
|
|
20
|
+
return `${index + 1}. ${result.title}
|
|
21
|
+
${result.source}${result.date ? ' — ' + formatDate(result.date) : ''}
|
|
22
|
+
${result.url}`
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const renderBskyPost = (post: BskyPost, index: number): string => (
|
|
26
|
+
`${index + 1}. @${post.author.handle} (${formatDateTime(post.record.createdAt)})
|
|
27
|
+
${normalizeBlurb(post.record.text)}
|
|
28
|
+
Likes: ${post.likeCount} Reposts: ${post.repostCount} Replies: ${post.replyCount}`
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
export const renderStandardDoc = (doc: StandardDocument, index: number): string => {
|
|
32
|
+
const title = doc.date
|
|
33
|
+
? `${index + 1}. ${doc.title} (${formatDate(doc.date)})`
|
|
34
|
+
: `${index + 1}. ${doc.title}`
|
|
35
|
+
return `${title}
|
|
36
|
+
${doc.url}
|
|
37
|
+
${normalizeBlurb(doc.snippet)}`
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const renderWikiPage = (page: WikiPage, index: number): string => {
|
|
41
|
+
const desc = page.description ? `\n ${normalizeBlurb(page.description)}` : ''
|
|
42
|
+
return `${index + 1}. ${page.title}
|
|
43
|
+
https://en.wikipedia.org/wiki/${page.key}${desc}
|
|
44
|
+
${normalizeBlurb(page.excerpt)}`
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export const renderMdnDoc = (doc: MdnDocument, index: number): string => {
|
|
48
|
+
return `${index + 1}. ${doc.title}
|
|
49
|
+
https://developer.mozilla.org${doc.mdn_url}
|
|
50
|
+
${normalizeBlurb(doc.summary)}`
|
|
51
|
+
}
|