Skip to content

Commit 7f9dc0b

Browse files
TMHSDigitalclaude
andcommitted
site: request links on an empty search; an OpenSearch description
When a search finds nothing, the empty state now links the "Request an asset" form with the failed search (and category) in the title, and the "New source" form. The homepage links a built opensearch.xml so a browser can add the catalog as a search engine; a search opens /?q=<terms>. Checked in a browser. Closes #226 Closes #241 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LpkeXQPaUhxjgqdqnk6BrJ
1 parent 6b4e73b commit 7f9dc0b

6 files changed

Lines changed: 81 additions & 2 deletions

File tree

‎site/build.mjs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { execFileSync } from "node:child_process";
1414
import fs from "node:fs";
1515
import path from "node:path";
1616
import { fileURLToPath } from "node:url";
17-
import { baseHeadHtml, commercialLabel, headAssetsHtml, esc, latestAllowedDate, OG_CARD_NAME, PAGE_MODULES, PREVIEW_CREDIT_LINE, sponsorButtonHtml } from "./lib/shared.mjs";
17+
import { baseHeadHtml, commercialLabel, headAssetsHtml, esc, latestAllowedDate, OG_CARD_NAME, opensearchLinkHtml, opensearchXml, PAGE_MODULES, PREVIEW_CREDIT_LINE, sponsorButtonHtml } from "./lib/shared.mjs";
1818
import { filmstripHtml, groupedEntriesHtml } from "./lib/cards.mjs";
1919
import { guidePageHtml } from "./lib/guide-page.mjs";
2020
import { renderGuide } from "./lib/guide-render.mjs";
@@ -212,6 +212,7 @@ function guideRowsHtml(guides, repo) {
212212
function headMetaHtml(site, stats, generatedAt, hasCard) {
213213
return [
214214
baseHeadHtml({ site, title: site.title, description: site.tagline, url: site.siteUrl, hasCard, root: "./", modules: PAGE_MODULES.home }),
215+
opensearchLinkHtml(site),
215216
`<meta name="generator" content="site/build.mjs ${esc(generatedAt)}" />`,
216217
`<meta name="catalog:entries" content="${stats.total}" />`,
217218
`<script type="application/ld+json">${scriptJson(homeJsonLd({ site, generatedAt, total: stats.total - stats.deprecated }))}</script>`,
@@ -715,6 +716,7 @@ function main() {
715716
const modified = lastCommitDates();
716717
writeOut("sitemap.xml", sitemapXml({ site: config.site, entries: visible, stacks, stamp, modified, categories: categoriesInSitemap, guides: config.guides }));
717718
writeOut("robots.txt", robotsTxt(config.site));
719+
writeOut("opensearch.xml", opensearchXml(config.site));
718720
writeOut("404.html", notFoundHtml(config.site));
719721
writeOut(path.join("freshness", "index.html"), freshnessPageHtml({ stats: fresh, site: config.site, stamp, total: stats.total, hasCard }));
720722

‎site/lib/lib.test.mjs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,6 +1907,27 @@ eq("search: q from the URL is capped", readState(`?q=${"a".repeat(5000)}`, { q:
19071907
eq("convert failure: no stderr", convertFailure(new Error("spawn ffmpeg ENOENT")), "convert failed: spawn ffmpeg ENOENT");
19081908
}
19091909

1910+
/* empty search: request links (#226); OpenSearch (#241) -------------------- */
1911+
{
1912+
const { gapIssueUrl } = await import("./search.mjs");
1913+
const r = "https://github.com/o/r";
1914+
eq("gap link: query in the title, encoded", gapIssueUrl(r, "pixel cars & trucks", ""), `${r}/issues/new?template=request-asset.yml&title=${encodeURIComponent("[gap] pixel cars & trucks")}`);
1915+
has("gap link: category named", decodeURIComponent(gapIssueUrl(r, "cars", "2D")), "title=[gap] cars (in 2D)");
1916+
has("gap link: no query, category only", decodeURIComponent(gapIssueUrl(r, " ", "Audio")), "title=[gap] (in Audio)");
1917+
has("gap link: whitespace collapsed and capped", decodeURIComponent(gapIssueUrl(r, `a\n\nb ${"x".repeat(300)}`, "")), `title=[gap] a b ${"x".repeat(96)}`);
1918+
lacks("gap link: no raw spaces or ampersands in the query string", gapIssueUrl(r, "a & b c", "").split("title=")[1], " ");
1919+
const indexHtml = nodeFs.readFileSync(new URL("../public/index.html", import.meta.url), "utf8");
1920+
has("empty state links the request form", indexHtml, 'id="empty-request"');
1921+
has("empty state links the new-source form", indexHtml, "template=new-source.yml");
1922+
1923+
const { opensearchXml, opensearchLinkHtml } = await import("./shared.mjs");
1924+
const os = opensearchXml({ siteUrl: "https://x.test/s/", title: "Free Game Dev Assets", tagline: "Free <assets>" });
1925+
has("opensearch: query template uses q", os, 'template="https://x.test/s/?q={searchTerms}#catalog"');
1926+
has("opensearch: short name cut at a word, 16 at most", os, "<ShortName>Free Game Dev</ShortName>");
1927+
has("opensearch: text escaped", os, "Free &lt;assets&gt;");
1928+
has("opensearch: homepage link", opensearchLinkHtml({ siteUrl: "https://x.test/s", title: "T" }), 'href="https://x.test/s/opensearch.xml"');
1929+
}
1930+
19101931
/* lint: unused imports (#230) --------------------------------------------- */
19111932
{
19121933
const { unusedImports, scriptFiles } = await import("../lint.mjs");

‎site/lib/search.mjs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,17 @@ export function placementKey(state, { ranked, group, list }) {
237237
return `${group ? "group" : "flat"}:${state.sort}:${state.engine || "any"}`;
238238
}
239239

240+
/**
241+
* The "Request an asset" issue form, its title pre-filled with the search
242+
* that found nothing: `[gap] pixel cars (in 2D)`. Only the title is filled;
243+
* the form's own fields are left for the reporter.
244+
*/
245+
export function gapIssueUrl(repo, query, categoryLabel = "") {
246+
const q = String(query || "").replace(/\s+/g, " ").trim().slice(0, 100);
247+
const title = `[gap] ${q}${categoryLabel ? `${q ? " " : ""}(in ${categoryLabel})` : ""}`.trim();
248+
return `${repo}/issues/new?template=request-asset.yml&title=${encodeURIComponent(title)}`;
249+
}
250+
240251
const CHOICES = [
241252
["cat", "category"],
242253
["sort", "sort"],

‎site/lib/shared.mjs‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,42 @@ export const PAGE_MODULES = {
285285
notFound: ["not-found.js", "search.js", "shared.mjs"],
286286
};
287287

288+
/**
289+
* The OpenSearch description (opensearch.xml): lets a browser add the catalog
290+
* as a search engine. A search opens the homepage with `?q=`, which the page
291+
* already reads (search.mjs readState).
292+
*/
293+
/** A title cut at a word to OpenSearch's 16-character ShortName limit. */
294+
function shortName(title) {
295+
let out = "";
296+
for (const word of String(title).split(/\s+/)) {
297+
const next = out ? `${out} ${word}` : word;
298+
if (next.length > 16) break;
299+
out = next;
300+
}
301+
return out || String(title).slice(0, 16);
302+
}
303+
304+
export function opensearchXml(site) {
305+
const base = String(site.siteUrl).replace(/\/+$/, "");
306+
return `<?xml version="1.0" encoding="UTF-8"?>
307+
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" xmlns:moz="http://www.mozilla.org/2006/browser/search/">
308+
<ShortName>${esc(shortName(site.title))}</ShortName>
309+
<Description>${esc(`Search ${site.title}: ${site.tagline}`.slice(0, 1024))}</Description>
310+
<InputEncoding>UTF-8</InputEncoding>
311+
<Image width="16" height="16" type="image/svg+xml">${esc(base)}/favicon.svg</Image>
312+
<Url type="text/html" method="get" template="${esc(base)}/?q={searchTerms}#catalog" />
313+
<Url type="application/opensearchdescription+xml" rel="self" template="${esc(base)}/opensearch.xml" />
314+
<moz:SearchForm>${esc(base)}/</moz:SearchForm>
315+
</OpenSearchDescription>
316+
`;
317+
}
318+
319+
/** The homepage's <link> to opensearch.xml. */
320+
export function opensearchLinkHtml(site) {
321+
return `<link rel="search" type="application/opensearchdescription+xml" title="${esc(site.title)}" href="${esc(String(site.siteUrl).replace(/\/+$/, ""))}/opensearch.xml" />`;
322+
}
323+
288324
/**
289325
* The Content-Security-Policy every page carries. Pages cannot set response
290326
* headers, so it is a <meta> tag, first in <head>. Scripts, fonts, images and

‎site/public/app.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { closestEntries, computeResults as computeResultsFor, passesFilters as passes, placementKey, queryTests as compileQuery, readState, scoreFields, searchFields, shouldGroup as shouldGroupFor, slugOf, sorterFor, SORTS, stateQuery, ENGINES } from "./search.js";
1+
import { gapIssueUrl, closestEntries, computeResults as computeResultsFor, passesFilters as passes, placementKey, queryTests as compileQuery, readState, scoreFields, searchFields, shouldGroup as shouldGroupFor, slugOf, sorterFor, SORTS, stateQuery, ENGINES } from "./search.js";
22
import { CARD_LIMIT, LIST_LIMIT, commercialLabel, entryCardHtml, escapeHtml, groupHeadingHtml, groupMoreHtml, PERSPECTIVE_LABELS } from "./cards.mjs";
33
import { installViewToggles } from "./views.js";
44
import { PERSPECTIVES } from "./shared.mjs";
@@ -408,6 +408,11 @@ import { PERSPECTIVES } from "./shared.mjs";
408408
$("#result-count").textContent = `${list.length} of ${data.entries.length} entries shown`;
409409
empty.hidden = list.length > 0;
410410
renderSuggestions(list.length ? "" : state.q);
411+
if (!list.length) {
412+
// The search that came up empty goes into the gap report's title.
413+
$("#empty-request").href = gapIssueUrl(repo, state.q, state.category === "all" ? "" : categoryLabels[state.category]?.label || state.category);
414+
$("#empty-new-source").href = `${repo}/issues/new?template=new-source.yml`;
415+
}
411416

412417
// The default page (no search, no filter) previews each category and
413418
// links to its page, as the prerender does. Any filter or search shows

‎site/public/index.html‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ <h3 id="shortlist-title">Your shortlist (<span id="shortlist-count">0</span>)</h
201201
<button type="button" class="link-button" id="empty-reset">Clear all filters</button>
202202
to see the whole catalog.
203203
<span class="empty-suggest" id="empty-suggest" hidden></span>
204+
<span class="empty-next">
205+
Can't find it? <a id="empty-request" href="https://github.com/TMHSDigital/Free-Game-Dev-Assets/issues/new?template=request-asset.yml" rel="noopener noreferrer">Request this asset</a>,
206+
or if you know a free source, <a id="empty-new-source" href="https://github.com/TMHSDigital/Free-Game-Dev-Assets/issues/new?template=new-source.yml" rel="noopener noreferrer">suggest it</a>.
207+
</span>
204208
</p>
205209
</section>
206210

0 commit comments

Comments
 (0)