Path: blob/main/src/project/project-crossrefs.ts
12924 views
/*1* project-crossrefs.ts2*3* Copyright (C) 2020-2022 Posit Software, PBC4*/56import { ensureDirSync, existsSync } from "../deno_ral/fs.ts";78import { basename, isAbsolute, join, relative } from "../deno_ral/path.ts";9import {10kCrossref,11kCrossrefChapterId,12kCrossrefChaptersAlpha,13kCrossrefChaptersAppendix,14} from "../config/constants.ts";15import { Metadata } from "../config/types.ts";16import { pathWithForwardSlashes } from "../core/path.ts";17import { shortUuid } from "../core/uuid.ts";1819import { projectScratchPath } from "./project-scratch.ts";2021export const kCrossrefIndexFile = "crossref-index-file";22export const kCrossrefInputType = "crossref-input-type";23export const kCrossrefResolveRefs = "crossref-resolve-refs";2425const kCrossrefIndexDir = "xref";2627type CrossrefIndex = Record<string, Record<string, string>>;2829export function crossrefIndexForOutputFile(30projectDir: string,31input: string,32output: string,33) {34// ensure we are dealing with a project relative path to the input35// that uses forward slashes36if (isAbsolute(input)) {37input = relative(projectDir, input);38}39input = pathWithForwardSlashes(input);4041// read (or create) main index42const crossrefDir = projectScratchPath(projectDir, kCrossrefIndexDir);43ensureDirSync(crossrefDir);44const mainIndexFile = join(crossrefDir, "INDEX");45const mainIndex: CrossrefIndex = existsSync(mainIndexFile)46? JSON.parse(Deno.readTextFileSync(mainIndexFile))47: {};4849// ensure this input/output has an index entry50// (generate and rewrite index file if not)51const outputBaseFile = basename(output);52if (mainIndex[input]?.[outputBaseFile] === undefined) {53if (mainIndex[input] === undefined) {54mainIndex[input] = {};55}56mainIndex[input][outputBaseFile] = shortUuid();57Deno.writeTextFileSync(58mainIndexFile,59JSON.stringify(mainIndex, undefined, 2),60);61}6263// return the file path64return join(crossrefDir, mainIndex[input]?.[outputBaseFile]);65}6667export function deleteCrossrefMetadata(metadata: Metadata) {68const crossref = metadata[kCrossref] as Metadata;69if (crossref) {70delete crossref[kCrossrefChaptersAppendix];71delete crossref[kCrossrefChaptersAlpha];72delete crossref[kCrossrefChapterId];73if (Object.keys(crossref).length === 0) {74delete metadata[kCrossref];75}76}77}787980