site/node_modules/@citation-js/plugin-csl/lib-mjs/citation.js

42 lines
1.1 KiB
JavaScript
Raw Permalink Normal View History

2024-10-14 06:09:33 +00:00
import { util } from '@citation-js/core';
import prepareEngine from './engines.js';
function prepareCiteItem(citeItem) {
return typeof citeItem === 'object' ? citeItem : {
id: citeItem
};
}
function prepareCitation(citation) {
if (citation.citationItems) {
return citation;
}
return {
citationItems: [].concat(citation).map(prepareCiteItem),
properties: {
noteIndex: 0
}
};
}
function prepareCitations(context) {
if (!context) {
return [];
}
return context.map(prepareCitation);
}
export default function citation(data, options = {}) {
const {
template = 'apa',
lang,
format = 'text'
} = options;
const ids = data.map(({
id
}) => id);
const entries = options.entry ? options.entry : ids;
data = util.downgradeCsl(data);
const citeproc = prepareEngine(data, template, lang, format);
const before = prepareCitations(options.citationsPre);
const citation = prepareCitation(entries);
const after = prepareCitations(options.citationsPost);
const output = citeproc.rebuildProcessorState([...before, citation, ...after], format, []);
return output[before.length][2];
}