Spaces:
Sleeping
Sleeping
File size: 886 Bytes
097eaf0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import { Document } from "../../controllers/v1/types";
/** Flatten newlines and strip control characters from a metadata value. */
function sanitizeMetadataValue(value: string, maxLen: number): string {
return value
.replace(/[\r\n]+/g, " ")
.replace(/[\x00-\x1f\x7f]/g, "")
.trim()
.slice(0, maxLen);
}
export function buildDocument(document: Document): string {
const metadata = document.metadata;
const markdown = document.markdown;
// For each key in the metadata, sanitize and cap length
const metadataString = Object.entries(metadata)
.map(([key, value]) => {
return `${key}: ${sanitizeMetadataValue(value?.toString() ?? "", 250)}`;
})
.join("\n");
const documentMetadataString = `\n- - - - - Page metadata - - - - -\n${metadataString}`;
const documentString = `${markdown}${documentMetadataString}`;
return documentString;
}
|