diff --git a/src/generator.js b/src/generator.js index e3bf382..e46ffdc 100644 --- a/src/generator.js +++ b/src/generator.js @@ -24,6 +24,7 @@ import { loadCSL, loadLocale, getCitationFormat, + getFrontmatterField, } from './utils.js' const defaultCiteFormat = 'apa' @@ -46,11 +47,11 @@ const rehypeCitationGenerator = (Cite) => { /** @type {string[]} */ let bibtexFile = [] const inputCiteformat = - /** @type {string} */ // @ts-ignore - options.csl || file?.data?.matter?.csl || file?.data?.frontmatter?.csl || defaultCiteFormat + /** @type {string} */ + options.csl || getFrontmatterField(file, 'csl') || defaultCiteFormat const noCite = - /** @type {string[] | false} */ // @ts-ignore - options.noCite || file?.data?.matter?.noCite || file?.data?.frontmatter?.noCite || false + /** @type {string[] | false} */ + options.noCite || getFrontmatterField(file, 'noCite') || false const inputLang = options.lang || 'en-US' const config = Cite.plugins.config.get('@csl') const citeFormat = await loadCSL(Cite, inputCiteformat, options.path) @@ -73,7 +74,7 @@ const rehypeCitationGenerator = (Cite) => { } } } - const citations = new Cite(bibtexFile, {generateGraph: false}) + const citations = new Cite(bibtexFile, { generateGraph: false }) const citationIds = citations.data.map((x) => x.id) const citationPre = [] const citationDict = {} @@ -147,7 +148,6 @@ const rehypeCitationGenerator = (Cite) => { ] }) - if (noCite) { if (noCite.length === 1 && noCite[0] === '@*') { citeproc.updateItems(citationIds) diff --git a/src/utils.js b/src/utils.js index adc8f5b..df80dbc 100644 --- a/src/utils.js +++ b/src/utils.js @@ -44,17 +44,16 @@ export const isValidHttpUrl = (str) => { export const getBibliography = async (options, file) => { /** @type {string[]} */ let bibliography = [] + const frontmatterBibliography = getFrontmatterField(file, 'bibliography') if (options.bibliography) { bibliography = typeof options.bibliography === 'string' ? [options.bibliography] : options.bibliography - // @ts-ignore - } else if (file?.data?.frontmatter?.bibliography) { + } else if (frontmatterBibliography) { bibliography = - // @ts-ignore - typeof file.data.frontmatter.bibliography === 'string' - ? [file.data.frontmatter.bibliography] - : file.data.frontmatter.bibliography - } + typeof frontmatterBibliography === 'string' + ? [frontmatterBibliography] + : frontmatterBibliography + } // If local path, get absolute path for (let i = 0; i < bibliography.length; i++) { if (!isValidHttpUrl(bibliography[i])) { @@ -67,7 +66,6 @@ export const getBibliography = async (options, file) => { } } } - return bibliography } @@ -197,3 +195,31 @@ export const isSameAuthor = (item, item2) => { } return true } + +/** + * @typedef {Object} FrontmatterSource + * @property {Record} [matter] + * @property {Record} [frontmatter] + * @property {{ frontmatter?: Record }} [astro] + */ + +/** + * @param {{ data?: FrontmatterSource }} file + * @param {string} fieldName + * @returns {any} + */ +export const getFrontmatterField = (file, fieldName) => { + if (!file || !file.data) { + return undefined + } + + const sources = [file.data.matter, file.data.frontmatter, file.data.astro?.frontmatter] + + for (const source of sources) { + if (source && fieldName in source) { + return source[fieldName] + } + } + + return undefined +}