/**
* @typedef {import('./types').CiteItem} CiteItem
* @typedef {import('./types').Mode} Mode
* @typedef {import('./types').Options} Options
*/
import { getSortedRelevantRegistryItems, split, isSameAuthor } from './utils.js';
import { htmlToHast } from './html-transform-node.js';
/**
* Generate citation using citeproc
* This accounts for prev citations and additional properties
*
* @param {*} citeproc
* @param {Mode} mode
* @param {CiteItem[]} entries
* @param {string} citationIdRoot
* @param {number} citationId
* @param {any[]} citationPre
* @param {Options} options
* @param {boolean} isComposite
* @param {import('./types').CitationFormat} citationFormat
* @return {[string, string]}
*/
export const genCitation = (citeproc, mode, entries, citationIdRoot, citationId, citationPre, options, isComposite, citationFormat) => {
const { inlineClass, linkCitations } = options;
const key = `${citationIdRoot}-${citationId}`;
const c = citeproc.processCitationCluster({
citationID: key,
citationItems: entries,
properties: mode === 'in-text'
? { noteIndex: 0, mode: isComposite ? 'composite' : '' }
: { noteIndex: citationId, mode: isComposite ? 'composite' : '' },
}, citationPre.length > 0 ? citationPre : [], []);
console.log(citationFormat);
// c = [ { bibchange: true, citation_errors: [] }, [ [ 0, '(1)', 'CITATION-1' ] ]]
const citationText = c[1].find((x) => x[2] === key)[1];
const ids = `citation--${entries.map((x) => x.id.toLowerCase()).join('--')}--${citationId}`;
if (mode === 'note') {
// Use cite-fn-{id} to denote footnote from citation, will clean it up later to follow gfm "user-content" format
return [
citationText,
htmlToHast(`${citationId}`),
];
}
else if (linkCitations && citationFormat === 'numeric') {
// e.g. [1, 2]
let i = 0;
const refIds = entries.map((e) => e.id);
const output = citationText.replace(/\d+/g, function (d) {
const url = `${d}`;
i++;
return url;
});
return [
citationText,
htmlToHast(`${output}`),
];
}
else if (linkCitations && citationFormat === 'author-date') {
// E.g. (see Nash, 1950, pp. 12–13, 1951); (Nash, 1950; Xie, 2016)
if (entries.length === 1) {
// Do not link bracket
const output = isComposite
? `${citationText}`
: `${citationText.slice(0, 1)}${citationText.slice(1, -1)}${citationText.slice(-1)}`;
return [
citationText,
htmlToHast(`${output}`),
];
}
else {
// Retrieve the items in the correct order and attach link each of them
const refIds = entries.map((e) => e.id);
const results = getSortedRelevantRegistryItems(citeproc, refIds, citeproc.opt.sort_citations);
const output = [];
let str = citationText;
for (const [i, item] of results.entries()) {
// Need to compare author. If same just match on date.
const id = item.id;
let citeMatch = item.ambig;
// If author is the same as the previous, some styles like apa collapse the author
if (i > 0 && isSameAuthor(results[i - 1], item) && str.indexOf(citeMatch) === -1) {
// Just match on year
citeMatch = item.ref.issued.year.toString();
}
const startPos = str.indexOf(citeMatch);
const [start, rest] = split(str, startPos);
output.push(start); // Irrelevant parts
const url = `${rest.substring(0, citeMatch.length)}`;
output.push(url);
str = rest.substr(citeMatch.length);
}
output.push(str);
return [
citationText,
htmlToHast(`${output.join('')}`),
];
}
}
else {
return [
citationText,
htmlToHast(`${citationText}`),
];
}
};