almost done
This commit is contained in:
65
lib/publications.ts
Normal file
65
lib/publications.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { parse } from "@retorquere/bibtex-parser";
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
|
||||
// Your existing path is correct.
|
||||
const bibliographyPath = path.join(process.cwd(), "content", "_bibliography.bib");
|
||||
|
||||
// --- FIX: Add `pdfAvailable` to the Publication interface ---
|
||||
export interface Publication {
|
||||
key: string;
|
||||
title: string;
|
||||
authors: string[];
|
||||
journal: string;
|
||||
year: string;
|
||||
bibtex: string;
|
||||
pdfUrl: string;
|
||||
url?: string; // Also make url optional to be safe
|
||||
pdfAvailable?: boolean; // This will hold the result of our check
|
||||
}
|
||||
|
||||
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, // <-- Add the result here
|
||||
};
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user