Skip to content

Commit

Permalink
fix: docx export image width
Browse files Browse the repository at this point in the history
  • Loading branch information
windingwind committed Dec 2, 2023
1 parent 6e090b3 commit 9569d5e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
41 changes: 27 additions & 14 deletions src/modules/export/docx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,20 +175,20 @@ function parseDocxCitationFields(html: string) {

// Replace all <span data-bn-citation-index="T21wEH05"></span> with ADDIN ZOTERO_ITEM CSL_CITATION {...}
const re = /<span data-bn-citation-index="([^"]+)"><\/span>/g;
const parsed = str
.replace(re, (match, p1) => {
return `<!--[if supportFields]>
<span style='mso-element:field-begin'></span>
<span style='mso-spacerun:yes'> </span>
ADDIN ZOTERO_ITEM CSL_CITATION ${htmlEscape(doc, citationCache[p1].field)}
<span style='mso-element:field-separator'></span>
<span style='mso-no-proof:yes'>
${htmlEscape(doc, citationCache[p1].text)}
</span>
<span style='mso-element:field-end'></span>
<![endif]-->`;
})
.replaceAll("\x3C!--[if supportFields]>", "<!--[if supportFields]>");
let parsed = str.replace(re, (match, p1) => {
return generateDocxField(
`ADDIN ZOTERO_ITEM CSL_CITATION ${htmlEscape(
doc,
citationCache[p1].field,
)}`,
htmlEscape(doc, citationCache[p1].text),
);
});

parsed += generateDocxField(
`ADDIN ZOTERO_BIBL {"uncited":[],"omitted":[],"custom":[]} CSL_BIBLIOGRAPHY`,
"[BIBLIOGRAPHY] Please click Zotero - Refresh in Word/LibreOffice to update all fields",
);

return parsed;
}
Expand Down Expand Up @@ -220,6 +220,19 @@ function htmlEscape(doc: Document, str: string) {
return div.innerHTML.replace(/"/g, "&quot;").replace(/'/g, "&#39;");
}

function generateDocxField(fieldCode: string, text: string) {
return `<!--[if supportFields]>
<span style='mso-element:field-begin'></span>
<span style='mso-spacerun:yes'> </span>
${fieldCode}
<span style='mso-element:field-separator'></span>
<span style='mso-no-proof:yes'>
${text}
</span>
<span style='mso-element:field-end'></span>
<![endif]-->`;
}

async function getWorker(): Promise<HTMLIFrameElement> {
const worker = ztoolkit.UI.createElement(document, "iframe", {
properties: {
Expand Down
17 changes: 16 additions & 1 deletion src/utils/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,22 @@ async function renderNoteHTML(
if (imageNodes.length) {
try {
const b64 = await getItemDataURL(attachment);
imageNodes.forEach((node) => node.setAttribute("src", b64));
imageNodes.forEach((node) => {
node.setAttribute("src", b64);
const width = Number(node.getAttribute("width"));
const height = Number(node.getAttribute("height"));
// 650/470 is the default width of images in Word
const maxWidth = Zotero.isMac ? 470 : 650;
if (width > maxWidth) {
node.setAttribute("width", maxWidth.toString());
if (height) {
node.setAttribute(
"height",
Math.round((height * maxWidth) / width).toString(),
);
}
}
});
} catch (e) {
ztoolkit.log(e);
}
Expand Down

0 comments on commit 9569d5e

Please sign in to comment.