import {readFile,writeFile,readdir,access} from 'node:fs/promises'; import {createHash} from 'node:crypto'; import {fileURLToPath} from 'node:url'; import path from 'node:path'; import assert from 'node:assert/strict'; import {cleanPublication} from './publication-sanitizer.mjs'; const root=path.resolve(path.dirname(fileURLToPath(import.meta.url)),'..'); // Raw captures never enter this pipeline. Only individually edited publication input is eligible. const input=JSON.parse(await readFile(path.join(root,'data/articles-v2.json'),'utf8')); const corpus=JSON.parse(await readFile(path.join(root,'data/corpus-manifest.json'),'utf8')); assert.equal(input.length,corpus.articlePaths,'Corpus must match independently reconciled inventory'); const placements=JSON.parse(await readFile(path.join(root,'data/figure-placements.json'),'utf8')); const decode=s=>s.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"').replace(/'/g,"'"); const report=[];const output=[]; for(const article of input){ let html=article.contentHtml; for(const figure of placements[article.slug]||[]){ await access(path.join(root,'public',figure.src)); const markup=`
${figure.alt}
${figure.caption}
`; if(figure.afterHeading){const index=html.indexOf(figure.afterHeading);const paragraph=html.indexOf('

',index);if(index>=0&¶graph>=0)html=html.slice(0,paragraph+4)+markup+html.slice(paragraph+4);else html+=markup;} else {const first=html.indexOf('

');html=first>=0?html.slice(0,first+4)+markup+html.slice(first+4):markup+html;} } const toc=[];let number=0; html=html.replace(/]*)?>([\s\S]*?)<\/h\1>/g,(_,level,title)=>{const id='section-'+(++number);if(level==='2')toc.push({id,title:decode(title.replace(/<[^>]*>/g,''))});return `${title}`;}); const clean=cleanPublication(html).replace(/()/g,'
$1
'); assert(!/Не удалось восстановить|Источник изображения|Иллюстрация из архивного|Данный вирус практически никакой угрозы/.test(clean),'Unedited process or unsafe copy: '+article.slug); const text=decode(clean.replace(/<[^>]*>/g,' ')).replace(/\s+/g,' ').trim();assert(text.length>80,'Empty or damaged body '+article.slug); const bodyHash=createHash('sha256').update(clean).digest('hex');report.push({slug:article.slug,inputBytes:html.length,outputBytes:clean.length,textCharacters:text.length,bodyHash,codeBlocks:(clean.match(/
createHash('sha256').update(a.contentHtml).digest('hex'))).size,output.length,'Duplicate article bodies');
await writeFile(path.join(root,'data/articles.json'),JSON.stringify(output,null,2)+'\n');
// Exercise actual parser boundary with hostile imports and malformed URL schemes.
const hostile='

okbadbad<script>

'; const safe=cleanPublication(hostile);assert(!/onclick|onerror|javascript:|tracker.test|