78 lines
2.8 KiB
JavaScript
78 lines
2.8 KiB
JavaScript
import { JSDOM } from 'jsdom';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import { fileURLToPath, pathToFileURL } from 'url';
|
|
|
|
const dom = new JSDOM('<!DOCTYPE html><html><body></body></html>');
|
|
global.window = dom.window;
|
|
global.document = dom.window.document;
|
|
global.DOMParser = dom.window.DOMParser;
|
|
global.Node = dom.window.Node;
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
const root = path.join(__dirname, '..');
|
|
|
|
const { parseReferencesBulkContent, parseReferencesEditableHtml } = await import(
|
|
pathToFileURL(path.join(root, 'src/utils/manuscriptReferenceHtml.js')).href
|
|
);
|
|
const { buildReferenceRunsFromContentHtml, buildManuscriptWordDocument } = await import(
|
|
pathToFileURL(path.join(root, 'src/utils/exportManuscriptWord.js')).href
|
|
);
|
|
const { Packer } = await import('docx');
|
|
|
|
const sampleItem =
|
|
'Author A. Title one. <i>Journal One</i>. 2020;1:1-10. Available at:<br><span style="color:#0082AA">https://doi.org/10.1000/one</span>';
|
|
|
|
const bulkBr = '1. ' + sampleItem + '<br><br>2. ' + sampleItem.replace('Author A', 'Author B').replace('one', 'two');
|
|
|
|
const bulkDiv =
|
|
'<div>1. ' +
|
|
sampleItem +
|
|
'</div><div>2. ' +
|
|
sampleItem.replace('Author A', 'Author B').replace('one', 'two') +
|
|
'</div>';
|
|
|
|
const bulkEditableDiv =
|
|
'<div>1. ' +
|
|
sampleItem +
|
|
'</div><div><br></div><div>2. ' +
|
|
sampleItem.replace('Author A', 'Author B').replace('one', 'two') +
|
|
'</div>';
|
|
|
|
function assertCount(label, items, expected) {
|
|
const ok = items.length === expected;
|
|
console.log((ok ? 'OK' : 'FAIL') + ' ' + label + ': got ' + items.length + ', expected ' + expected);
|
|
if (!ok) {
|
|
items.forEach(function (item, i) {
|
|
console.log(' [' + i + ']', String(item.html || '').slice(0, 120));
|
|
});
|
|
}
|
|
return ok;
|
|
}
|
|
|
|
console.log('--- parseReferencesBulkContent br/br ---');
|
|
assertCount('br/br', parseReferencesBulkContent(bulkBr), 2);
|
|
|
|
console.log('--- parseReferencesBulkContent div/div ---');
|
|
assertCount('div/div', parseReferencesBulkContent(bulkDiv), 2);
|
|
|
|
console.log('--- parseReferencesBulkContent editable div ---');
|
|
assertCount('editable div', parseReferencesBulkContent(bulkEditableDiv), 2);
|
|
|
|
console.log('--- buildReferenceRunsFromContentHtml ---');
|
|
const runs = buildReferenceRunsFromContentHtml(sampleItem, 'journal');
|
|
console.log('runs:', runs.length, runs.map((r) => r.options?.text || r.text || r.options?.children).join('|'));
|
|
|
|
console.log('--- buildManuscriptWordDocument with html items ---');
|
|
const doc = buildManuscriptWordDocument(
|
|
[{ type: 0, content: '<p>Intro text.</p>' }],
|
|
'',
|
|
[],
|
|
null,
|
|
null,
|
|
parseReferencesBulkContent(bulkBr)
|
|
);
|
|
const buf = await Packer.toBuffer(doc);
|
|
fs.writeFileSync(path.join(__dirname, 'test-ref-html-export.docx'), buf);
|
|
console.log('wrote test-ref-html-export.docx', buf.length, 'bytes');
|