Skip to content

Commit e2ea4ca

Browse files
committed
fix: Decode HTML entities in llms.txt titles and descriptions
Asciidoctor escapes the doctitle and header attributes for HTML, so llms.txt showed "Configuration & Environment Overrides" and "Contributor’s guide" instead of the plain characters. Decode numeric entities and the basic named ones (amp, lt, gt, quot, apos) in a single pass, so "<" correctly stays the text "<".
1 parent 36c1713 commit e2ea4ca

1 file changed

Lines changed: 17 additions & 3 deletions

File tree

‎lib/llms-txt.js‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,30 @@ module.exports.register = function () {
6868
}
6969

7070
function entryFor (page, siteUrl) {
71-
const title = (page.asciidoc && page.asciidoc.doctitle ? page.asciidoc.doctitle : page.src.stem)
72-
.replace(/<[^>]+>/g, '')
71+
const title = decodeEntities(
72+
(page.asciidoc && page.asciidoc.doctitle ? page.asciidoc.doctitle : page.src.stem).replace(/<[^>]+>/g, '')
73+
)
7374
const description = page.asciidoc && page.asciidoc.attributes['description']
7475
const source = sourceUrl(page.src)
7576
let entry = `- [${title}](${siteUrl}${page.pub.url})`
76-
if (description) entry += `: ${description.replace(/\s+/g, ' ').trim()}`
77+
if (description) entry += `: ${decodeEntities(description.replace(/\s+/g, ' ').trim())}`
7778
if (source) entry += ` (source: ${source})`
7879
return entry
7980
}
8081

82+
// Asciidoctor escapes the doctitle and header attributes such as description for
83+
// HTML, so "&" arrives as "&amp;" and "'" as "&#8217;".
84+
// Decode in a single pass so "&amp;lt;" stays the literal text "&lt;".
85+
const NAMED_ENTITIES = { amp: '&', lt: '<', gt: '>', quot: '"', apos: "'" }
86+
87+
function decodeEntities (text) {
88+
return text.replace(/&(?:#(\d+)|#x([0-9a-f]+)|(amp|lt|gt|quot|apos));/gi, (match, dec, hex, name) => {
89+
if (dec) return String.fromCodePoint(Number(dec))
90+
if (hex) return String.fromCodePoint(parseInt(hex, 16))
91+
return NAMED_ENTITIES[name.toLowerCase()]
92+
})
93+
}
94+
8195
function sourceUrl (src) {
8296
const origin = src.origin
8397
if (!origin || !origin.url || !origin.url.startsWith('https://github.com/')) return undefined

0 commit comments

Comments
 (0)