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.caption}`;
if(figure.afterHeading){const index=html.indexOf(figure.afterHeading);const paragraph=html.indexOf('
');
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='