Skip to content

Commit

Permalink
parse astro frontmatter
Browse files Browse the repository at this point in the history
  • Loading branch information
timlrx committed Oct 11, 2024
1 parent 6f7672a commit ae9b9f0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
12 changes: 6 additions & 6 deletions src/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
loadCSL,
loadLocale,
getCitationFormat,
getFrontmatterField,
} from './utils.js'

const defaultCiteFormat = 'apa'
Expand All @@ -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)
Expand All @@ -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 = {}
Expand Down Expand Up @@ -147,7 +148,6 @@ const rehypeCitationGenerator = (Cite) => {
]
})


if (noCite) {
if (noCite.length === 1 && noCite[0] === '@*') {
citeproc.updateItems(citationIds)
Expand Down
42 changes: 34 additions & 8 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand All @@ -67,7 +66,6 @@ export const getBibliography = async (options, file) => {
}
}
}


return bibliography
}
Expand Down Expand Up @@ -197,3 +195,31 @@ export const isSameAuthor = (item, item2) => {
}
return true
}

/**
* @typedef {Object} FrontmatterSource
* @property {Record<string, any>} [matter]
* @property {Record<string, any>} [frontmatter]
* @property {{ frontmatter?: Record<string, any> }} [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
}

0 comments on commit ae9b9f0

Please sign in to comment.