Skip to content

Commit

Permalink
Update citeproc-js-based-replacer.js
Browse files Browse the repository at this point in the history
Fix #2
  • Loading branch information
kotobuki committed Apr 19, 2023
1 parent df6e40d commit eb38a98
Showing 1 changed file with 45 additions and 36 deletions.
81 changes: 45 additions & 36 deletions citeproc-js-based-replacer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,33 @@ function collectCitations(obj) {
return obj.map(collectCitations);
} else if (typeof obj === "object" && obj !== null) {
if (obj.t === "Cite") {
const citationId = obj.c[0][0].citationId;
const noteIndex = citationKeys.length;
citationKeys.push(citationId);

const citationSuffix = obj.c[0][0].citationSuffix;

let locator = "";
if (citationSuffix.length > 0) {
const suffixes = citationSuffix.map((suffix) => suffix.c);
locator = suffixes.join("|");
for (const item of obj.c[0]) {
const citationId = item.citationId;
const noteIndex = citationKeys.length;
citationKeys.push(citationId);

const citationSuffix = item.citationSuffix;

let locator = "";
if (citationSuffix.length > 0) {
const suffixes = citationSuffix
.filter((suffix) => suffix.t === "Str")
.map((suffix) => suffix.c.replace(/^\[\s*|\s*\]$/g, ""));
locator = suffixes.join("|");
}

const citationItem = { id: citationId };
if (locator) {
citationItem.locator = locator;
}

citationObjects.push({
citationID: `${citationId}_${noteIndex}`,
citationItems: [citationItem],
properties: { noteIndex: noteIndex },
});
}

const citationItem = { id: citationId };
if (locator) {
citationItem.locator = locator;
}

citationObjects.push({
citationID: `${citationId}_${noteIndex}`,
citationItems: [citationItem],
properties: { noteIndex: noteIndex },
});

return;
} else {
return Object.fromEntries(
Expand Down Expand Up @@ -80,23 +84,28 @@ function replaceCitations(obj) {
return obj.map(replaceCitations);
} else if (typeof obj === "object" && obj !== null) {
if (obj.t === "Cite") {
const formattedItems = obj.c[0].map(() => {
const formattedCitation = formattedCitations
.shift()
.trim()
.replace(/<i>/g, "*")
.replace(/<\/i>/g, "*")
.replace(/<div class="csl-entry">/g, "")
.replace(/<\/div>/g, "")
.replace(/&ndash;/g, "--")
.replace(/&mdash;/g, "---")
.replace(/&amp;/g, "&")
.replace(/&lt;/g, "<")
.replace(/&gt;/g, ">");

return formattedCitation;
});

const concatenatedFormattedItems = formattedItems.join("; ");

return {
t: "RawInline",
c: [
"markdown",
formattedCitations
.shift()
.trim()
.replace(/<i>/g, "*")
.replace(/<\/i>/g, "*")
.replace(/<div class="csl-entry">/g, "")
.replace(/<\/div>/g, "")
.replace(/&ndash;/g, "--")
.replace(/&mdash;/g, "---")
.replace(/&amp;/g, "&")
.replace(/&lt;/g, "<")
.replace(/&gt;/g, ">"),
],
c: ["markdown", concatenatedFormattedItems],
};
} else {
return Object.fromEntries(
Expand Down

0 comments on commit eb38a98

Please sign in to comment.