Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions lib/llms-txt.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,30 @@ module.exports.register = function () {
}

function entryFor (page, siteUrl) {
const title = (page.asciidoc && page.asciidoc.doctitle ? page.asciidoc.doctitle : page.src.stem)
.replace(/<[^>]+>/g, '')
const title = decodeEntities(
(page.asciidoc && page.asciidoc.doctitle ? page.asciidoc.doctitle : page.src.stem).replace(/<[^>]+>/g, '')
)
const description = page.asciidoc && page.asciidoc.attributes['description']
const source = sourceUrl(page.src)
let entry = `- [${title}](${siteUrl}${page.pub.url})`
if (description) entry += `: ${description.replace(/\s+/g, ' ').trim()}`
if (description) entry += `: ${decodeEntities(description.replace(/\s+/g, ' ').trim())}`
if (source) entry += ` (source: ${source})`
return entry
}

// Asciidoctor escapes the doctitle and header attributes such as description for
// HTML, so "&" arrives as "&amp;" and "'" as "&#8217;".
// Decode in a single pass so "&amp;lt;" stays the literal text "&lt;".
const NAMED_ENTITIES = { amp: '&', lt: '<', gt: '>', quot: '"', apos: "'" }

function decodeEntities (text) {
return text.replace(/&(?:#(\d+)|#x([0-9a-f]+)|(amp|lt|gt|quot|apos));/gi, (match, dec, hex, name) => {
if (dec) return String.fromCodePoint(Number(dec))
if (hex) return String.fromCodePoint(parseInt(hex, 16))
return NAMED_ENTITIES[name.toLowerCase()]
})
}

function sourceUrl (src) {
const origin = src.origin
if (!origin || !origin.url || !origin.url.startsWith('https://github.com/')) return undefined
Expand Down
Loading