(text, { mappedNames = [], profileIds = [] } = {})
| 93 | * @param {Set<string>|string[]} [opts.profileIds] known profile ids (exact match) |
| 94 | */ |
| 95 | export function validateScript(text, { mappedNames = [], profileIds = [] } = {}) { |
| 96 | const warnings = []; |
| 97 | const norm = (text || '').replace(/\r\n?/g, '\n'); |
| 98 | const mapped = mappedNames instanceof Set ? mappedNames : new Set(mappedNames); |
| 99 | const ids = profileIds instanceof Set ? profileIds : new Set(profileIds); |
| 100 | |
| 101 | // 1. Unknown voices — a name with no cast mapping and no exact profile match. |
| 102 | for (const name of parseCastNames(norm)) { |
| 103 | if (!mapped.has(name) && !ids.has(name)) warnings.push({ type: 'unknown_voice', name }); |
| 104 | } |
| 105 | |
| 106 | // 2. Empty chapters — a `# heading` whose body has no spoken text. Split on |
| 107 | // headings (mirroring the parser) and check each body for any word. |
| 108 | const heads = []; |
| 109 | const hre = new RegExp(HEADING_RE.source, HEADING_RE.flags); |
| 110 | let hm; |
| 111 | while ((hm = hre.exec(norm)) !== null) { |
| 112 | heads.push({ index: hm.index, end: hm.index + hm[0].length, title: (hm[1] || '').trim() }); |
| 113 | if (hre.lastIndex === hm.index) hre.lastIndex++; |
| 114 | } |
| 115 | for (let i = 0; i < heads.length; i++) { |
| 116 | const bodyEnd = i + 1 < heads.length ? heads[i + 1].index : norm.length; |
| 117 | const body = norm.slice(heads[i].end, bodyEnd); |
| 118 | if (!stripMarkup(body).trim()) warnings.push({ type: 'empty_chapter', title: heads[i].title }); |
| 119 | } |
| 120 | |
| 121 | // 3. Unrecognized bracket tokens — anything outside the known grammar. |
| 122 | const seenTags = new Set(); |
| 123 | const bre = new RegExp(BRACKET_RE.source, BRACKET_RE.flags); |
| 124 | let bm; |
| 125 | while ((bm = bre.exec(norm)) !== null) { |
| 126 | const tok = bm[0]; |
| 127 | if (bre.lastIndex === bm.index) bre.lastIndex++; |
| 128 | const lower = tok.toLowerCase(); |
| 129 | const known = |
| 130 | VOICE_RE.test(tok) || |
| 131 | PAUSE_TOKEN_RE.test(tok) || |
| 132 | SSML_TOKEN_RE.test(tok) || |
| 133 | REACTION_TOKENS.has(lower); |
| 134 | VOICE_RE.lastIndex = 0; // test() advances a /g regex — reset it |
| 135 | if (!known && !seenTags.has(lower)) { |
| 136 | seenTags.add(lower); |
| 137 | warnings.push({ type: 'unknown_tag', tag: tok }); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | return warnings; |
| 142 | } |
no test coverage detected