Skip to content

Commit ff4b58f

Browse files
committed
Place single file book outputs in subdirectories
- goes to: `_book/book-<format>` - Also ensure that download links for books use the format output directory
1 parent 38d8103 commit ff4b58f

File tree

7 files changed

+38
-9
lines changed

7 files changed

+38
-9
lines changed

src/format/asciidoc/format-asciidoc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export function asciidocFormat(): Format {
7272
);
7373
}
7474

75-
const kFormatOutputDir = "asciidoc";
75+
const kFormatOutputDir = "book-asciidoc";
7676
const kAsciidocDocType = "asciidoc-doctype";
7777

7878
// Ref target marks the refs div so the post process can inject the bibliography
@@ -85,7 +85,7 @@ const kUseAsciidocNativeCites = "use-asciidoc-native-cites";
8585
// This provide book specific behavior for producing asciidoc books
8686
const asciidocBookExtension = {
8787
multiFile: true,
88-
formatOutputDirectory(_format: Format) {
88+
formatOutputDirectory() {
8989
return kFormatOutputDir;
9090
},
9191
filterParams: (_options: PandocOptions) => {

src/format/docx/format-docx.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ export function docxFormat(): Format {
3333
};
3434
},
3535
extensions: {
36-
book: {},
36+
book: {
37+
formatOutputDirectory: () => {
38+
return "book-docx";
39+
},
40+
},
3741
},
3842
},
3943
);

src/format/epub/format-epub.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ export function epubFormat(): Format {
3030
}
3131

3232
const epubBookExtension: BookExtension = {
33+
formatOutputDirectory: () => {
34+
return "book-epub";
35+
},
3336
onSingleFilePreRender: (format: Format, config?: ProjectConfig): Format => {
3437
// derive epub-cover-image from cover-image if not explicitly specified
3538
if (!format.pandoc[kEPubCoverImage] && !format.metadata[kBookCoverImage]) {

src/format/pdf/format-pdf.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,10 @@ function createPdfFormat(
265265
}
266266

267267
const pdfBookExtension: BookExtension = {
268+
formatOutputDirectory: () => {
269+
return "book-pdf";
270+
},
271+
268272
onSingleFilePostRender: (
269273
project: ProjectContext,
270274
renderedFile: RenderedFile,

src/project/types/book/book-config.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import { basename, join } from "path/mod.ts";
1010

1111
import * as ld from "../../../core/lodash.ts";
1212

13-
import { ensureTrailingSlash, safeExistsSync } from "../../../core/path.ts";
13+
import {
14+
ensureTrailingSlash,
15+
pathWithForwardSlashes,
16+
safeExistsSync,
17+
} from "../../../core/path.ts";
1418
import { FormatLanguage, Metadata } from "../../../config/types.ts";
1519

1620
import {
@@ -90,6 +94,7 @@ import {
9094
NavigationItemObject,
9195
PageFooterRegion,
9296
} from "../../../resources/types/schema-types.ts";
97+
import { projectType } from "../project-types.ts";
9398

9499
export const kBookChapters = "chapters";
95100
export const kBookAppendix = "appendices";
@@ -455,7 +460,7 @@ function downloadTools(
455460
config: ProjectConfig,
456461
): SidebarTool[] | undefined {
457462
// Filter the user actions to the set that are single file books
458-
const downloadActions = websiteConfigActions("downloads", kBook, config);
463+
const downloadActions = websiteConfigActions(kBookDownloads, kBook, config);
459464
const filteredActions = downloadActions.filter((action) => {
460465
const format = defaultWriterFormat(action);
461466
if (format) {
@@ -467,19 +472,32 @@ function downloadTools(
467472

468473
// Map the action into sidebar items
469474
const outputStem = bookOutputStem(projectDir, config);
475+
const projType = projectType(config.project.type);
470476
const downloads = filteredActions.map((action) => {
471477
const format = defaultWriterFormat(action);
478+
const outputDir = projType.formatOutputDirectory
479+
? projType.formatOutputDirectory(format) || ""
480+
: "";
481+
472482
const downloadItem = kDownloadableItems[action];
473483
if (downloadItem) {
474484
return {
475485
icon: downloadItem.icon,
476486
text: `Download ${downloadItem.name}`,
477-
href: `/${outputStem}.${format.render[kOutputExt]}`,
487+
href: pathWithForwardSlashes(join(
488+
"/",
489+
outputDir,
490+
`/${outputStem}.${format.render[kOutputExt]}`,
491+
)),
478492
};
479493
} else {
480494
return {
481495
text: `Download action}`,
482-
href: `/${outputStem}.${format.render[kOutputExt]}`,
496+
href: pathWithForwardSlashes(join(
497+
"/",
498+
outputDir,
499+
`${outputStem}.${format.render[kOutputExt]}`,
500+
)),
483501
};
484502
}
485503
});

src/project/types/book/book-shared.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export interface BookExtension {
4444

4545
filterParams?: (options: PandocOptions) => Record<string, unknown>;
4646

47-
formatOutputDirectory?: (format: Format) => string;
47+
formatOutputDirectory?: () => string;
4848

4949
// book extensions can modify the format before render
5050
onSingleFilePreRender?: (format: Format, config?: ProjectConfig) => Format;

src/project/types/book/book.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export const bookProjectType: ProjectType = {
118118
if (format.extensions?.book) {
119119
const bookExt = format.extensions?.book as BookExtension;
120120
if (bookExt.formatOutputDirectory) {
121-
return bookExt.formatOutputDirectory(format);
121+
return bookExt.formatOutputDirectory();
122122
} else {
123123
return undefined;
124124
}

0 commit comments

Comments
 (0)