import { ensureDirSync, existsSync } from "../deno_ral/fs.ts";
import { join } from "../deno_ral/path.ts";
import { PandocOptions } from "../command/render/types.ts";
import { FormatPandoc } from "../config/types.ts";
import { projectIsBook } from "./project-shared.ts";
import { projectScratchPath } from "./project-scratch.ts";
export const kCitesIndexFile = "cites-index-file";
const kCiteIndexDir = "cites";
type CiteIndex = Record<string, string[]>;
export function citeIndexFilterParams(
options: PandocOptions,
_defaults?: FormatPandoc,
) {
if (options.project && projectIsBook(options.project)) {
return {
[kCitesIndexFile]: citeIndexFile(options.project.dir),
};
} else {
return {};
}
}
export function citeIndex(projectDir: string) {
const mainIndexFile = citeIndexFile(projectDir);
const mainIndex: CiteIndex = existsSync(mainIndexFile)
? JSON.parse(Deno.readTextFileSync(mainIndexFile))
: {};
return mainIndex;
}
function citeIndexFile(projectDir: string) {
const citesDir = projectScratchPath(projectDir, kCiteIndexDir);
ensureDirSync(citesDir);
return join(citesDir, "index.json");
}