63 lines
2.0 KiB
TypeScript
63 lines
2.0 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 {
|
|
if (Array.isArray(value)) {
|
|
bibtexEntryString += ` ${key} = {${value.join(" ")}},
|
|
`;
|
|
}
|
|
}
|
|
}
|
|
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,
|
|
};
|
|
});
|
|
} |