Prepare V2 corpus, editorial journal and reproducible publication boundary
This commit is contained in:
1 parent
7b3f49788d
commit
69607c52fd
498 files changed
+97671
-468
No files matched your search
@@ -0,0 +1,43 @@
|
||||
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><img src="${figure.src}" alt="${figure.alt}" width="${figure.width||640}" height="${figure.height||880}" loading="lazy" decoding="async"><figcaption>${figure.caption}</figcaption></figure>`;
|
||||
if(figure.afterHeading){const index=html.indexOf(figure.afterHeading);const paragraph=html.indexOf('</p>',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('</p>');html=first>=0?html.slice(0,first+4)+markup+html.slice(first+4):markup+html;}
|
||||
|
||||
}
|
||||
const toc=[];let number=0;
|
||||
html=html.replace(/<h([23])(?:\s[^>]*)?>([\s\S]*?)<\/h\1>/g,(_,level,title)=>{const id='section-'+(++number);if(level==='2')toc.push({id,title:decode(title.replace(/<[^>]*>/g,''))});return `<h${level} id="${id}">${title}</h${level}>`;});
|
||||
const clean=cleanPublication(html).replace(/(<table[\s\S]*?<\/table>)/g,'<div class="table-scroll" role="region" tabindex="0" aria-label="Сравнительная таблица; прокрутка клавишами со стрелками">$1</div>');
|
||||
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(/<pre/g)||[]).length,figures:(clean.match(/<figure/g)||[]).length,toc:toc.length});
|
||||
output.push({...article,contentHtml:clean,toc,readingMinutes:Math.max(1,Math.ceil(text.split(/\s+/).length/180))});
|
||||
}
|
||||
assert.equal(new Set(output.map(a=>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='<script>alert(1)</script><p onclick="evil()">ok<a href="javascript:alert(1)">bad</a><a href="//tracker.test">bad</a><img src="https://tracker.test/pixel" onerror="evil()"><iframe src="https://evil.test"></iframe><code><script></code></p>';
|
||||
const safe=cleanPublication(hostile);assert(!/onclick|onerror|javascript:|tracker.test|<script|<iframe/.test(safe));assert(safe.includes('<script>'));
|
||||
await writeFile(path.join(root,'data/publication-report.json'),JSON.stringify({articles:report,hostileFixturePassed:true},null,2)+'\n');
|
||||
// Stable source identity excludes runtime build identity/output and internal review documents.
|
||||
async function files(dir){const out=[];for(const entry of await readdir(path.join(root,dir),{withFileTypes:true})){const name=path.join(dir,entry.name);if(entry.isDirectory())out.push(...await files(name));else out.push(name);}return out;}
|
||||
const names=[...await files('app'),...await files('lib'),...await files('scripts'),...await files('data'),...await files('public/visuals'),'package.json','package-lock.json','next.config.mjs'].sort();
|
||||
const hash=createHash('sha256');for(const name of names){hash.update(name);hash.update(await readFile(path.join(root,name)));}const sourceHash=hash.digest('hex');
|
||||
await writeFile(path.join(root,'public/build-identity.json'),JSON.stringify({sourceHash,articles:output.length},null,2)+'\n');
|
||||
console.log(JSON.stringify({sourceHash,articles:report.length,hostileFixturePassed:true}));
|
||||
Reference in new issue
Block a user