Skip to content

Commit

Permalink
Merge pull request #47 from halafy-ds/main
Browse files Browse the repository at this point in the history
Fix #42 + test case for mixing noCite citations with template ones
  • Loading branch information
timlrx authored Oct 11, 2024
2 parents 1fafdf6 + 5308900 commit 6f7672a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,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 All @@ -82,7 +82,7 @@ const rehypeCitationGenerator = (Cite) => {
/** @type {Mode} */
const mode = citeproc.opt.xclass
const citationFormat = getCitationFormat(citeproc)

let parsedEntries = []
visit(tree, 'text', (node, idx, parent) => {
const match = node.value.match(citationRE)
if (!match || ('tagName' in parent && !permittedTags.includes(parent.tagName))) return
Expand All @@ -105,6 +105,7 @@ const rehypeCitationGenerator = (Cite) => {
}

const [entries, isComposite] = parseCitation(match)
parsedEntries = entries

// If id is not in citation file (e.g. route alias or js package), abort process
for (const citeItem of entries) {
Expand Down Expand Up @@ -146,11 +147,17 @@ const rehypeCitationGenerator = (Cite) => {
]
})


if (noCite) {
if (noCite.length === 1 && noCite[0] === '@*') {
citeproc.updateItems(citationIds)
} else {
citeproc.updateItems(noCite.map((x) => x.replace('@', '')))
const mergedIds = citations.data
.filter((x) => noCite.map((x) => x.replace('@', '')).includes(x['citation-key']))
.map((x) => x.id)
.concat(parsedEntries.map((x) => x.id))

citeproc.updateItems(mergedIds)
}
}

Expand Down
12 changes: 12 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,18 @@ rehypeCitationTest('no-cite', async () => {
</div>`
assert.is(result, expected)
})
+
rehypeCitationTest('no-cite citations must be added to template citations', async () => {
const result = await processHtml('<div>[@Nash1950]</div>', {
noCite: ['@Nash1951'],
})
const expected = dedent`<div><span class="" id="citation--nash1950--1">(Nash, 1950)</span></div><div id="refs" class="references csl-bib-body">
<div class="csl-entry" id="bib-nash1950">Nash, J. (1950). Equilibrium points in n-person games. <i>Proceedings of the National Academy of Sciences</i>, <i>36</i>(1), 48–49.</div>
<div class="csl-entry" id="bib-nash1951">Nash, J. (1951). Non-cooperative games. <i>Annals of Mathematics</i>, 286–295.</div>
</div>`
assert.is(result, expected)
})


rehypeCitationTest('no-cite catch all', async () => {
const result = await processHtml('<div>text</div>', {
Expand Down

0 comments on commit 6f7672a

Please sign in to comment.