相关性代码调整

This commit is contained in:
2026-06-24 16:52:24 +08:00
parent 5a1bbb0894
commit 5260ca8ea5
29 changed files with 6330 additions and 568 deletions

1
scripts/h1-before.xml Normal file
View File

@@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:w16cex="http://schemas.microsoft.com/office/word/2018/wordml/cex" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16="http://schemas.microsoft.com/office/word/2018/wordml" xmlns:w16sdtdh="http://schemas.microsoft.com/office/word/2020/wordml/sdtdatahash" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" mc:Ignorable="w14 w15 wp14"><w:body><w:p><w:pPr><w:outlineLvl w:val="0"/></w:pPr><w:r><w:rPr><w:b/><w:bCs/><w:color w:val="D25A5A"/><w:sz w:val="18"/><w:szCs w:val="18"/></w:rPr><w:t xml:space="preserve">Introduction</w:t></w:r></w:p><w:sectPr><w:pgSz w:w="11906" w:h="16838" w:orient="portrait"/><w:pgMar w:top="1440" w:right="1440" w:bottom="1440" w:left="1440" w:header="708" w:footer="708" w:gutter="0"/><w:pgNumType/><w:docGrid w:linePitch="360"/></w:sectPr></w:body></w:document>

BIN
scripts/test-h1-inject.docx Normal file

Binary file not shown.

BIN
scripts/test-h1-out.docx Normal file

Binary file not shown.

0
scripts/test-h1-out.xml Normal file
View File

BIN
scripts/test-h1-output.docx Normal file

Binary file not shown.

View File

@@ -0,0 +1,77 @@
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');