provenance.ts
1,020 bytes
| 1 | const MARKER_PATTERN = /\[S(\d+)\]/g |
|---|---|
| 2 | |
| 3 | export interface ProvenanceResult { |
| 4 | content: string |
| 5 | invalidMarkerCount: number |
| 6 | } |
| 7 | |
| 8 | export function validateProvenance(content: string, validSegmentIds: ReadonlySet<string>): ProvenanceResult { |
| 9 | let invalidMarkerCount = 0 |
| 10 | |
| 11 | const lines = content.split('\n').map((line) => { |
| 12 | const markers = [...line.matchAll(MARKER_PATTERN)] |
| 13 | const invalidMarkers = markers.filter((match) => !validSegmentIds.has(`S${match[1]}`)) |
| 14 | if (invalidMarkers.length === 0) return line |
| 15 | |
| 16 | invalidMarkerCount += invalidMarkers.length |
| 17 | let updated = line |
| 18 | for (const match of invalidMarkers) { |
| 19 | updated = updated.replace(match[0], '') |
| 20 | } |
| 21 | |
| 22 | const leading = updated.match(/^\s*/)?.[0] ?? '' |
| 23 | const rest = updated.slice(leading.length).replace(/ {2,}/g, ' ').trimEnd() |
| 24 | updated = leading + rest |
| 25 | |
| 26 | if (!updated.includes('[unverified]')) { |
| 27 | updated = `${updated} [unverified]` |
| 28 | } |
| 29 | return updated |
| 30 | }) |
| 31 | |
| 32 | return { content: lines.join('\n'), invalidMarkerCount } |
| 33 | } |
| 34 | |