Skip to the content

Capsid menu

app/lib/markdown-twin.ts

37 lines and 5 functions in DrDustinEdwards/dustinedwards-info, at commit 1590ba4 on 2026-10-24. This file on GitHub.

Functions

The functions of markdown-twin.ts
FunctionLinesComplexityCoveredCRAPMutation
linkToMarkdown4 to 61No data: no coverage report in this mapNo data: no coverage report in this mapNo data: no mutants in this code
alternateMarkdownLink9 to 111No data: no coverage report in this mapNo data: no coverage report in this mapNo data: no mutants in this code
canonicalLink19 to 211No data: no coverage report in this mapNo data: no coverage report in this mapNo data: no mutants in this code
prefersMarkdown23 to 251No data: no coverage report in this mapNo data: no coverage report in this mapNo data: no mutants in this code
markdownResponse32 to 422No data: no coverage report in this mapNo data: no coverage report in this mapNo data: no mutants in this code

Imports

It imports app/lib/negotiate.mjs, app/lib/seo.ts. Imported by app/routes/blog.$slug.tsx, app/routes/blog.$slug[.md].ts, app/routes/content-page[.md].ts, app/routes/cv[.md].ts, app/routes/kb-library[.csv].ts, app/routes/kb-library[.json].ts, app/routes/kb-library[.md].ts, app/routes/lab[.md].ts, app/routes/llms-full[.txt].ts, app/routes/llms.ts, app/routes/procedure.version[.md].ts, app/routes/procedure[.md].ts, app/routes/protocols[.csv].ts, app/routes/protocols[.json].ts, app/routes/protocols[.md].ts, app/routes/publications.$slug[.md].ts, app/routes/systems[.md].ts.

Source

import { prefersType } from "./negotiate.mjs";
import { NO_STORE_CACHE_CONTROL, SITE_ORIGIN } from "./seo";

export function linkToMarkdown(slug: string) {
  return alternateMarkdownLink(`/writing/${slug}.md`);
}

/** `path` includes the `.md`. Absolute, so it matches the blog twin and `canonicalLink`. */
function alternateMarkdownLink(path: string) {
  return `<${SITE_ORIGIN}${path}>; rel="alternate"; type="text/markdown"`;
}

/**
 * The `Link` header a machine-only document carries to name the HTML page it stands for: a post's
 * twin names the post, llms.txt and llms-full.txt name the home page. Canonical, not alternate, so a
 * crawler that reaches the twin first credits the page instead. The paper twins are assets and set the
 * same header from public/_headers, where code cannot reach.
 */
export function canonicalLink(path: string) {
  return `<${SITE_ORIGIN}${path}>; rel="canonical"`;
}

export function prefersMarkdown(request: Request) {
  return prefersType(request, "text/markdown");
}

/**
 * The negotiated `/writing/:slug` variant must never be stored: under `Vary: Accept, Cookie` a second
 * stored variant lets a cookie-bearing request HIT the cookieless one, skipping the Worker's
 * `private, no-store` downgrade. Do not "optimize" it to `SHARED_CACHE_CONTROL`.
 */
export function markdownResponse(slug: string, body: string, cacheControl: string) {
  return new Response(body, {
    headers: {
      "content-type": "text/markdown; charset=utf-8",
      "cache-control": cacheControl,
      link: canonicalLink(`/writing/${slug}`),
      // Only the never-stored response is the negotiated one, so only it varies on Accept.
      ...(cacheControl === NO_STORE_CACHE_CONTROL ? { vary: "Accept" } : {}),
    },
  });
}