Skip to the content

Capsid menu

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

The functions of ask-keys.mjs
FunctionLinesComplexityCoveredCRAPMutation
keyForUrl14 to 172100%OkCRAP 2No data: no mutants in this code
ownsAskKey26 to 30125%OkCRAP 1.4No data: no mutants in this code
<anonymous:29>29 to 2920%WatchCRAP 6No data: no mutants in this code
urlForKey38 to 499100%WatchCRAP 9No data: no mutants in this code
slugForKey55 to 635100%OkCRAP 5No data: no mutants in this code
labelForUrl69 to 827100%WatchCRAP 7No data: no mutants in this code
<anonymous:81>81 to 811100%OkCRAP 1No 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());
}