-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstory-text.js
More file actions
31 lines (28 loc) · 1.14 KB
/
story-text.js
File metadata and controls
31 lines (28 loc) · 1.14 KB
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
28
29
30
31
function sanitizeCanonicalStoryText(rawText, options = {}) {
const maxChars = Math.max(200, Number(options.maxChars || 12000));
const raw = String(rawText || '').replace(/\r/g, '\n').trim();
if (!raw) return '';
const lines = raw
.split('\n')
.map((line) => String(line || '').trim())
.filter(Boolean)
.map((line) => {
if (/^(story|article|source)\s*text\s*:/i.test(line)) {
return line.replace(/^(story|article|source)\s*text\s*:/i, '').trim();
}
if (/^(summary|description)\s*:/i.test(line)) {
return line.replace(/^(summary|description)\s*:/i, '').trim();
}
return line;
})
.filter((line) => !/^#{1,6}\s+/.test(line))
.filter((line) => !/^(error|warning|status|debug|trace|stack|exception|provider(?: used)?|panel errors?)\s*:/i.test(line))
.filter((line) => !/^no summary available\.?$/i.test(line))
.filter((line) => !/^\[[A-Z _-]{2,40}\]\s*/.test(line));
const out = lines.join('\n').replace(/\n{3,}/g, '\n\n').trim();
if (!out) return '';
return out.length > maxChars ? `${out.slice(0, maxChars)}...` : out;
}
module.exports = {
sanitizeCanonicalStoryText
};