Skip to content

Commit

Permalink
prefer html outputs for sidebar links in websites/books
Browse files Browse the repository at this point in the history
  • Loading branch information
jjallaire committed Apr 14, 2022
1 parent d5ff7a8 commit 99ea0a0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/command/render/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
import { handlerForScript } from "../../core/run/run.ts";
import { execProcess } from "../../core/process.ts";
import { parseShellRunCommand } from "../../core/run/shell.ts";
import { clearProjectIndex } from "../../project/project-index.ts";

export async function renderProject(
context: ProjectContext,
Expand Down Expand Up @@ -160,6 +161,7 @@ export async function renderProject(
(options.flags?.clean == true)
) {
const realProjectDir = Deno.realPathSync(context.dir);
// ouptut dir
let realOutputDir = join(realProjectDir, projOutputDir);
if (existsSync(realOutputDir)) {
realOutputDir = Deno.realPathSync(realOutputDir);
Expand All @@ -170,6 +172,8 @@ export async function renderProject(
removeIfExists(realOutputDir);
}
}
// remove index
clearProjectIndex(realProjectDir);
}

// run pre-render step if we are rendering all files
Expand Down
23 changes: 18 additions & 5 deletions src/project/project-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import { Metadata } from "../config/types.ts";
import { Format } from "../config/types.ts";
import { PartitionedMarkdown } from "../core/pandoc/types.ts";

import { dirAndStem, pathWithForwardSlashes } from "../core/path.ts";
import {
dirAndStem,
pathWithForwardSlashes,
removeIfExists,
} from "../core/path.ts";
import { kOutputFile, kTitle } from "../config/constants.ts";
import { renderFormats } from "../command/render/render.ts";
import { fileExecutionEngine } from "../execute/engine.ts";
Expand All @@ -25,7 +29,10 @@ import { projectScratchPath } from "./project-scratch.ts";
import { parsePandocTitle } from "../core/pandoc/pandoc-partition.ts";
import { readYaml } from "../core/yaml.ts";
import { formatKeys } from "../config/metadata.ts";
import { normalizeWebsiteFormat } from "./types/website/website-config.ts";
import {
formatsPreferHtml,
normalizeWebsiteFormat,
} from "./types/website/website-config.ts";
import { isJupyterNotebook } from "../core/jupyter/jupyter.ts";

export interface InputTargetIndex extends Metadata {
Expand Down Expand Up @@ -155,9 +162,10 @@ export async function resolveInputTarget(
) {
const index = await inputTargetIndex(project, href);
if (index) {
const format = Object.values(index.formats)[0];
const formats = formatsPreferHtml(index.formats) as Record<string, Format>;
const format = Object.values(formats)[0];
const [hrefDir, hrefStem] = dirAndStem(href);
const outputFile = format?.pandoc[kOutputFile] || `${hrefStem}.html`;
const outputFile = format.pandoc[kOutputFile] || `${hrefStem}.html`;
const outputHref = pathWithForwardSlashes(
(absolute ? "/" : "") + join(hrefDir, outputFile),
);
Expand Down Expand Up @@ -211,10 +219,15 @@ export async function inputTargetIndexForOutputFile(
}
}

export function clearProjectIndex(projectDir: string) {
const indexPath = projectScratchPath(projectDir, "idx");
removeIfExists(indexPath);
}

function inputTargetIndexFile(projectDir: string, input: string): string {
return indexPath(projectDir, `${input}.json`);
}

function indexPath(projectDir: string, path = ""): string {
function indexPath(projectDir: string, path: string): string {
return projectScratchPath(projectDir, join("idx", path));
}
16 changes: 16 additions & 0 deletions src/project/types/website/website-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,22 @@ export function normalizeWebsiteFormat(
}
}

export function formatsPreferHtml(formats: Record<string, unknown>) {
const orderedFormats = {} as Record<string, unknown>;
const formatNames = Object.keys(formats);
const htmlFormatPos = formatNames.findIndex((format) =>
isHtmlOutput(format, true)
);
if (htmlFormatPos !== -1) {
const htmlFormatName = formatNames.splice(htmlFormatPos, 1)[0];
orderedFormats[htmlFormatName] = formats[htmlFormatName];
}
for (const formatName of formatNames) {
orderedFormats[formatName] = formats[formatName];
}
return orderedFormats;
}

// provide a project context that elevates html to the default
// format for documents (unless they explicitly declare another format)
export function websiteProjectConfig(
Expand Down

0 comments on commit 99ea0a0

Please sign in to comment.