)'
/** Merge citation-only blocks into the preceding paragraph (marked splits on newlines). */
export function collapseStandaloneCitationParagraphs(html: string): string {
if (!html || !html.includes('citation')) return html
const mergePattern = new RegExp(
`(<\\/(?:p|li)>)\\s*(?:
\\s*<\\/p>\\s*)*
\\s*(${CITATION_HTML_FRAGMENT})\\s*<\\/p>`,
'g',
)
let result = html
let prev = ''
while (result !== prev) {
prev = result
result = result.replace(mergePattern, (_match, closeTag: string, citation: string) => {
return ` ${citation}${closeTag}`
})
}
return result
}
/** Preserve raw / tags before sanitizers that would strip them. */
export function preserveCitationTags(contentStr: string): { text: string; tags: string[] } {
const tags: string[] = []
const text = contentStr.replace(KB_WEB_TAG_RE, (match) => {
const idx = tags.length
tags.push(match)
return `\x00TAG${idx}\x00`
})
return { text, tags }
}
export function restoreCitationTags(text: string, tags: string[]): string {
return text.replace(/\x00TAG(\d+)\x00/g, (_, idx) => tags[Number(idx)] || '')
}
/** A line that holds nothing but `` tags, including its line break. */
const KB_ONLY_LINE_RE = /^[ \t]*(?:]*?\/?>[ \t]*)+\n?/gm
/** An inline `` tag together with the space that separated it from the text. */
const KB_INLINE_TAG_RE = /[ \t]*]*?\s*\/?>/g
/**
* Strip chat-only citation markup so an answer can live on as standalone
* Markdown (e.g. saved into a knowledge base by "add to knowledge").
*
* `` becomes a plain Markdown link — the URL still means something
* outside the chat. `` points at chunk UUIDs that do not, so it is dropped
* and its document name is returned instead, for the caller to render as a
* source list. Removal stays local to the tags: no whitespace elsewhere in the
* answer is touched, so code blocks survive unchanged.
*/
export function stripCitationTagsForMarkdown(
content: string,
): { text: string; sources: string[] } {
if (!content) return { text: '', sources: [] }
const sources: string[] = []
const seen = new Set()
KB_TAG_ATTR_RE.lastIndex = 0
for (const match of content.matchAll(KB_TAG_ATTR_RE)) {
const doc = (parseTagAttributes(match[1]).doc || '').trim()
if (doc && !seen.has(doc)) {
seen.add(doc)
sources.push(doc)
}
}
const text = content
.replace(WEB_TAG_ATTR_RE, (_match, attrString: string) => {
const attrs = parseTagAttributes(attrString)
const url = (attrs.url || '').trim()
if (!url) return ''
// Brackets inside the label would break the link syntax we emit.
const label = (attrs.title || '').trim().replace(/[[\]]/g, ' ').trim() || url
return `[${label}](${url})`
})
.replace(KB_ONLY_LINE_RE, '')
.replace(KB_INLINE_TAG_RE, '')
.trim()
return { text, sources }
}