llms.mjs
46 lines and 4 functions in DrDustinEdwards/prelum, at commit e389900 on 2026-10-24. This file on GitHub.
Functions
| Function | Lines | Complexity | Covered | CRAP | Mutation |
|---|---|---|---|---|---|
| llmsDocument | 32 to 42 | 6 | 100% | WatchCRAP 6 | No data: mutation testing has not run on this repo yet |
| llmsLink | 45 to 51 | 2 | 100% | OkCRAP 2 | No data: mutation testing has not run on this repo yet |
| oneLine | 47 to 47 | 1 | 100% | OkCRAP 1 | No data: mutation testing has not run on this repo yet |
| <anonymous:48> | 48 to 48 | 1 | 100% | OkCRAP 1 | No data: mutation testing has not run on this repo yet |
Imports
It imports no other module of this repo. Imported by index.mjs.
Source
/**
* @typedef {object} LlmsLink
* @property {string} title
* @property {string} url
* @property {string} [description]
*/
/**
* @typedef {object} LlmsLinkSection
* @property {string} heading
* @property {LlmsLink[]} links
*/
/**
* @typedef {object} LlmsProseSection
* @property {string} heading
* @property {string[]} body paragraphs, kept as written and separated by a blank line
*/
/** @typedef {LlmsLinkSection | LlmsProseSection} LlmsSection */
/**
* The llms.txt skeleton (llmstxt.org): title, a blockquote summary, optional intro paragraphs, then H2 sections of link lists.
* A link's title is Markdown link text, so `[` and `]` are escaped; everything else is kept as written, because the titles are
* prose and the sites' own files carry quotes and ampersands in them. A section with no links is dropped rather than left a
* bare heading. A prose section (`body` in place of `links`) is its heading, a blank line, then its paragraphs as written, for a
* file that mixes prose with its lists; one with no paragraphs is dropped the same way.
*
* @param {{ title: string, summary: string, intro?: string[], sections: LlmsSection[] }} doc
* @returns {string}
*/
export function llmsDocument(doc) {
const blocks = [`# ${doc.title}`, `> ${doc.summary}`, ...(doc.intro ?? [])];
for (const section of doc.sections) {
if ("body" in section) {
if (section.body.length > 0) blocks.push(`## ${section.heading}`, ...section.body);
} else if (section.links.length > 0) {
blocks.push(`## ${section.heading}\n${section.links.map(llmsLink).join("\n")}`);
}
}
return `${blocks.join("\n\n")}\n`;
}
/** @param {LlmsLink} link */
export function llmsLink(link) {
// One line per link: a newline in the title or description would end the list item, and a ")" in the URL would end the link.
const oneLine = (/** @type {string} */ text) => text.replace(/\s*[\r\n]+\s*/g, " ");
const title = oneLine(link.title).replace(/[[\]]/g, (bracket) => "\\" + bracket);
const url = link.url.replace(/\)/g, "%29");
return `- [${title}](${url})${link.description ? `: ${oneLine(link.description)}` : ""}`;
}