app/lib/search/ask-keys.mjs
76 lines and 7 functions in DrDustinEdwards/dustinedwards-info, at commit 1590ba4 on 2026-10-24. This file on GitHub.
Functions
| Function | Lines | Complexity | Covered | CRAP | Mutation |
|---|---|---|---|---|---|
| keyForUrl | 14 to 17 | 2 | 100% | OkCRAP 2 | No data: no mutants in this code |
| ownsAskKey | 26 to 30 | 1 | 25% | OkCRAP 1.4 | No data: no mutants in this code |
| <anonymous:29> | 29 to 29 | 2 | 0% | WatchCRAP 6 | No data: no mutants in this code |
| urlForKey | 38 to 49 | 9 | 100% | WatchCRAP 9 | No data: no mutants in this code |
| slugForKey | 55 to 63 | 5 | 100% | OkCRAP 5 | No data: no mutants in this code |
| labelForUrl | 69 to 82 | 7 | 100% | WatchCRAP 7 | No data: no mutants in this code |
| <anonymous:81> | 81 to 81 | 1 | 100% | OkCRAP 1 | No data: no mutants in this code |
Imports
It imports no other module of this repo. Imported by app/enhance/ask.ts, app/lib/publications/compile.mjs, app/lib/search/ask-prompt.mjs, app/lib/search/ask.server.ts.
Source
// AI Search carries nothing but item.key about a chunk's source, so the key IS the citation.
/**
* Double underscore: kebab slugs and anchors cannot contain it, but a single one can (posts_fts).
*/
export const KEY_SEPARATOR = "__";
/**
* The trailing slash is stripped, so a paper's key is exactly its markdown twin's URL.
*
* @param {string} url `/writing/<slug>`, `/writing/<slug>#<anchor>` or `/research/publications/<slug>/`
* @returns {string} `writing/<slug>.md`, `writing/<slug>__<anchor>.md` or `research/publications/<slug>.md`
*/
export function keyForUrl(url) {
const [path, anchor] = url.replace(/^\//, "").replace(/\/$/, "").split("#");
return anchor ? `${path}${KEY_SEPARATOR}${anchor}.md` : `${path}.md`;
}
/**
* Whether an item key is one of this post's: its document key or one of its section keys. Exact key
* or section prefix, never startsWith(slug), which would sweep up a longer slug.
*
* @param {string} slug
* @returns {(key: string) => boolean}
*/
export function ownsAskKey(slug) {
const documentKey = keyForUrl(`/writing/${slug}`);
const sectionPrefix = `${documentKey.replace(/\.md$/, "")}${KEY_SEPARATOR}`;
return (key) => key === documentKey || key.startsWith(sectionPrefix);
}
/**
* Null for a key this site did not write: an instance can hold items from another source.
*
* @param {string} key
* @returns {string | null}
*/
export function urlForKey(key) {
if (typeof key !== "string" || !key.endsWith(".md")) return null;
const withoutExtension = key.slice(0, -3);
const parts = withoutExtension.split(KEY_SEPARATOR);
if (parts.length > 2) return null;
const [path, anchor] = parts;
if (!path) return null;
// A paper cites its page, not its twin, with the canonical trailing slash (the slashless form redirects).
if (path.startsWith("research/publications/")) return anchor ? null : `/${path}/`;
if (!path.startsWith("writing/")) return null;
return anchor ? `/${path}#${anchor}` : `/${path}`;
}
/**
* @param {string} key
* @returns {string | null}
*/
export function slugForKey(key) {
const url = urlForKey(key);
if (!url) return null;
// Posts only: a paper has no posts row, and passing its URL as a slug would make every answer citing
// it unreplayable forever.
if (!url.startsWith("/writing/")) return null;
const slug = (url.split("#")[0] ?? "").replace(/^\/writing\//, "");
return slug.length > 0 ? slug : null;
}
/**
* @param {string} url
* @returns {string}
*/
export function labelForUrl(url) {
const [path, anchor] = url.split("#");
// The slug, not a DOI: the DOI-to-slug fold is lossy, and a plausible reconstruction is worse than none.
if ((path ?? "").startsWith("/research/publications/")) {
return `Paper ${(path ?? "").replace(/^\/research\/publications\//, "").replace(/\/$/, "")}`;
}
const target = anchor ?? (path ?? "").replace(/^\/writing\//, "");
const words = target
.replace(/^\d+-/, "")
.split("-")
.filter(Boolean);
if (words.length === 0) return "Untitled";
return words.join(" ").replace(/^./, (c) => c.toUpperCase());
}