Skip to content

Commit

Permalink
fix: docx character error
Browse files Browse the repository at this point in the history
  • Loading branch information
windingwind committed Dec 2, 2023
1 parent 35cf602 commit c4ed5a6
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/modules/export/docx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,11 @@ function parseDocxCitationFields(html: string) {
citationItems.push(item);
}
const properties = citation.properties;
const formattedCitation = elem.textContent || "";
const formattedCitation = `${
elem.textContent || "Zotero Citation"
} - Please click Zotero - Refresh in Word/LibreOffice to update all fields.`;
properties.formattedCitation = formattedCitation;
properties.plainCitation = formattedCitation + " ";
properties.noteIndex = 0;
const citationID = getCitationID(citationCache);

Expand Down Expand Up @@ -177,16 +180,15 @@ function parseDocxCitationFields(html: string) {
return `<!--[if supportFields]>
<span style='mso-element:field-begin'></span>
<span style='mso-spacerun:yes'> </span>
ADDIN ZOTERO_ITEM CSL_CITATION ${citationCache[p1].field}
ADDIN ZOTERO_ITEM CSL_CITATION ${htmlEscape(doc, citationCache[p1].field)}
<span style='mso-element:field-separator'></span>
<span style='mso-no-proof:yes'>
${citationCache[p1].text} - Please click Zotero - Refresh in Word/LibreOffice to update all fields.
${htmlEscape(doc, citationCache[p1].text)}
</span>
<span style='mso-element:field-end'></span>
<![endif]-->`;
})
.replaceAll("\x3C", "<")
.replaceAll("\x3E", ">");
.replaceAll("\x3C!--[if supportFields]>", "<!--[if supportFields]>");

return parsed;
}
Expand All @@ -211,6 +213,13 @@ function tryParse(s: string) {
}
}

function htmlEscape(doc: Document, str: string) {
const div = doc.createElement("div");
const text = doc.createTextNode(str);
div.appendChild(text);
return div.innerHTML.replace(/"/g, "&quot;").replace(/'/g, "&#39;");
}

async function getWorker(): Promise<HTMLIFrameElement> {
const worker = ztoolkit.UI.createElement(document, "iframe", {
properties: {
Expand Down

0 comments on commit c4ed5a6

Please sign in to comment.