Skip to content

Commit

Permalink
fix: export BibTeX using citation-js
Browse files Browse the repository at this point in the history
  • Loading branch information
agoose77 committed Apr 10, 2024
1 parent ea4b9ed commit 9fc0dad
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 29 deletions.
4 changes: 4 additions & 0 deletions packages/citation-js-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export type CitationRenderer = Record<
getDOI: () => string | undefined;
getURL: () => string | undefined;
cite: CSL;
exportBibTeX: () => string;
}
>;

Expand Down Expand Up @@ -246,6 +247,9 @@ export async function getCitationRenderers(data: CSL[]): Promise<CitationRendere
);
},
cite: c,
exportBibTeX(): string {
return cite.set(c).format('bibtex', {format: 'text'}) as string;
}
},
];
}),
Expand Down
2 changes: 1 addition & 1 deletion packages/citation-js-utils/types/citation-js/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ declare module '@citation-js/core' {

set(data: any): this;

format: (format: string, options: Any) => string | object[];
format: (format: string, options: any) => string | object[];

data: CSL[];
}
Expand Down
29 changes: 1 addition & 28 deletions packages/myst-cli/src/build/utils/bibtex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,6 @@ import type { ISession } from '../../session/types.js';
import { addWarningForFile } from '../../utils/addWarningForFile.js';
import type { References } from 'myst-common';

/**
* Extract a single entry from the entire content of a bibtex file
*
* Look for the pattern '@article{key' then finds the closing bracket
* and returns that substring. The "article" prefix may be any
* alpha word.
*/
export function extractBibtex(key: string, bibtex: string) {
const match = bibtex.match(new RegExp(`@[a-zA-Z]*{${key}`, 'g'));
if (!match) return;
const start = bibtex.indexOf(match[0]);
let bracketCount = 0;
let ind = start + match[0].length;
while (bibtex[ind] && (bibtex[ind] !== '}' || bracketCount !== 0)) {
if (bibtex[ind - 1] && bibtex[ind - 1] !== '\\') {
if (bibtex[ind] === '{') bracketCount++;
if (bibtex[ind] === '}') bracketCount--;
}
ind++;
}
return bibtex[ind] ? bibtex.substring(start, ind + 1) : undefined;
}

/**
* Write new bibtex file from citation renderer data and reference order
Expand All @@ -48,12 +26,7 @@ export function writeBibtexFromCitationRenderers(
const citationLookup: Record<string, string> = {};
Object.values(cache.$citationRenderers).forEach((renderers) => {
Object.entries(renderers).forEach(([key, renderer]) => {
const bibtexContent = (renderer.cite._graph as any[]).find((item) => {
return item.type === '@biblatex/text';
});
if (bibtexContent?.data) {
citationLookup[key] = extractBibtex(key, bibtexContent.data) ?? bibtexContent.data;
}
citationLookup[key] = renderer.exportBibTeX();
});
});
const bibtexContent: string[] = [];
Expand Down

0 comments on commit 9fc0dad

Please sign in to comment.