Files
website/lib/publications.ts
Steffen Illium 3e6b27b293
All checks were successful
Next.js App CI / docker (push) Successful in 8m25s
corrected bibtex interaction
2025-09-25 09:51:36 +02:00

61 lines
1.9 KiB
TypeScript

import { parse } from "@retorquere/bibtex-parser";
import fs from "fs";
import path from "path";
const bibliographyPath = path.join(process.cwd(), "content", "_bibliography.bib");
export interface Publication {
key: string;
title: string;
authors: string[];
journal: string;
year: string;
bibtex: string;
pdfUrl: string;
url?: string;
pdfAvailable?: boolean;
}
export function getPublicationsData(): Publication[] {
const bibtexString = fs.readFileSync(bibliographyPath, "utf8");
const bibtexJson = parse(bibtexString);
return bibtexJson.entries.map((entry) => {
const authorNames = entry.fields.author
? entry.fields.author.map(
(author) => `${author.firstName} ${author.lastName}`
)
: [];
let bibtexEntryString = `@${entry.type}{${entry.key},\n`;
for (const [key, value] of Object.entries(entry.fields)) {
if (key === 'author') {
bibtexEntryString += ` author = {${entry.fields.author
? entry.fields.author.map(a => `${a.lastName}, ${a.firstName}`).join(' and ')
: ''}},
`;
} else {
bibtexEntryString += ` ${key} = {${value}},
`;
}
}
bibtexEntryString += `}`
const journalField = entry.fields.booktitle || entry.fields.journal;
const pdfPath = path.join(process.cwd(), "public", "publications", `${entry.key}.pdf`);
const pdfExists = fs.existsSync(pdfPath);
return {
key: entry.key,
title: Array.isArray(entry.fields.title) ? entry.fields.title.join(" ") : entry.fields.title,
authors: authorNames,
journal: Array.isArray(journalField) ? journalField.join(" ") : journalField,
year: Array.isArray(entry.fields.year) ? entry.fields.year.join(" ") : entry.fields.year,
url: Array.isArray(entry.fields.url) ? entry.fields.url.join(" ") : entry.fields.url,
bibtex: bibtexEntryString,
pdfUrl: `/publications/${entry.key}.pdf`,
pdfAvailable: pdfExists,
};
});
}