Files
tougao_web/scripts/inspect-word-fig-pairing.mjs
2026-07-31 15:07:12 +08:00

46 lines
1.3 KiB
JavaScript

import FS from 'fs';
import path from 'path';
import JSZip from 'jszip';
const file = path.join(
'C:/Users/Administrator/Desktop/IN2026A0418001_final_20260713 1.docx'
);
const xml = await (await JSZip.loadAsync(FS.readFileSync(file))).file('word/document.xml').async('string');
const paras = xml.split(/<\/w:p>/);
function plainOf(p) {
return [...p.matchAll(/<w:t(?:\s[^>]*)?>([\s\S]*?)<\/w:t>/g)]
.map((x) => x[1])
.join('')
.replace(/&amp;/g, '&')
.replace(/\s+/g, ' ')
.trim();
}
function blipCount(p) {
return (p.match(/a:blip/g) || []).length;
}
function drawingCount(p) {
return (p.match(/<w:drawing/g) || []).length;
}
for (let i = 0; i < paras.length; i++) {
const plain = plainOf(paras[i]);
if (!/^Figure\s+\d+/i.test(plain)) continue;
// look back up to 8 paras for images / A B labels
const back = [];
for (let j = Math.max(0, i - 8); j < i; j++) {
const t = plainOf(paras[j]);
const b = blipCount(paras[j]);
const d = drawingCount(paras[j]);
if (b || d || t) {
back.push({ j, blips: b, drawings: d, text: t.slice(0, 80) });
}
}
const next = plainOf(paras[i + 1] || '').slice(0, 180);
console.log('\nCAPTION', plain.slice(0, 100));
console.log('BACK', JSON.stringify(back, null, 0));
console.log('NOTE?', next);
}