pomdtr-valtownsearch.web.val.run
Readme

Search for vals using the Github API.

Either use the provided UI, or the query param:

https://val-town-search.pomdtr.me/search?q=fetchJSON

How does it work ?

I've wrote about it!

Todos

  • Embed the results in the UI
  • Refresh the vals on a cron using a github action
  • Improve layout on small screens
  • Support json Accept header
  • Add pagination params
  • Allow to filter by authors
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/** @jsxImportSource npm:preact **/
import codeOnValTown from "https://esm.town/v/andreterron/codeOnValTown?v=50";
import { render } from "npm:preact-render-to-string";
const githubQuery = (query: string) => encodeURIComponent(`${query} repo:pomdtr/val-town-mirror path:vals/`);
async function handler(req: Request) {
const url = new URL(req.url);
if (url.pathname == "/opensearch.xml") {
return new Response(
`<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
<ShortName>Val Town Search</ShortName>
<Description>Search for vals using the Github API</Description>
<Url type="text/html" method="get" template="https://val-town-search.pomdtr.me/search?q={searchTerms}"/>
<Url type="application/opensearchdescription+xml" template="https://val-town-search.pomdtr.me/opensearch.xml"/>
<Image height="16" width="16" type="image/png">https://pomdtr-favicons.web.val.run/val-town</Image>
<moz:SearchForm>https://val-town-search.pomdtr.me/search</moz:SearchForm>
</OpenSearchDescription>`,
{
headers: {
"Content-Type": "application/opensearchdescription+xml",
},
},
);
}
if (url.pathname == "/search") {
const query = url.searchParams.get("q");
const resp = await fetch(`https://api.github.com/search/code?q=${githubQuery(query)}`, {
headers: {
"Accept": "application/vnd.github.text-match+json",
"Authorization": `Bearer ${Deno.env.get("GH_TOKEN")}`,
},
});
if (!resp.ok) {
return new Response(null, {
status: 500,
});
}
const body = await resp.json();
const items = body.items.map(item => {
const [_dir, author, filename] = item.path.split("/");
const [name, _extension] = filename.split(".");
return {
val: {
author,
name,
},
match: item.text_matches[0].fragment,
};
});
const accept = req.headers.get("Accept");
if (accept == "text/json" || accept == "application/json") {
return Response.json(items);
}
return new Response(
render(
<html>
<head>
<title>Val Town Search</title>
<link rel="icon" href="https://pomdtr-favicons.web.val.run/val-town" />
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css"
/>
<link href="https://cdn.jsdelivr.net/npm/prismjs@v1.29.0/themes/prism.css" rel="stylesheet" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<main class="container">
<h1>
<a href="/" class="contrast">Val Town Search</a>
</h1>
<section>
<form method="GET">
<fieldset role="search">
<input
type="search"
name="q"
value={query}
placeholder="Search for vals..."
autocomplete="off"
aria-label="Search"
/>
<input type="submit" value="Search" />
</fieldset>
</form>
</section>
<hr />
<section>
{items.map(item => (
<article>
<header>
<a href={`https://www.val.town/v/${item.val.author}/${item.val.name}`}>
{item.val.author}/{item.val.name}
</a>
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
v77
May 14, 2024