Skip to content

Commit 01ac3fa

Browse files
authored
don't do jupyter front matter fixups in book renders. Closes #4866 (#4871)
1 parent b6ceaf3 commit 01ac3fa

File tree

4 files changed

+42
-27
lines changed

4 files changed

+42
-27
lines changed

src/command/render/render-files.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
/*
2-
* render-files.ts
3-
*
4-
* Copyright (C) 2020-2022 Posit Software, PBC
5-
*
6-
*/
2+
* render-files.ts
3+
*
4+
* Copyright (C) 2020-2022 Posit Software, PBC
5+
*/
76

87
// ensures cell handlers are installed
98
import "../../core/handlers/handlers.ts";
@@ -40,7 +39,11 @@ import {
4039
import { annotateOjsLineNumbers } from "../../execute/ojs/annotate-source.ts";
4140
import { ojsExecuteResult } from "../../execute/ojs/compile.ts";
4241
import { ExecuteResult, MappedExecuteResult } from "../../execute/types.ts";
43-
import { kProjectLibDir, ProjectContext } from "../../project/types.ts";
42+
import {
43+
kProjectLibDir,
44+
kProjectType,
45+
ProjectContext,
46+
} from "../../project/types.ts";
4447
import { outputRecipe } from "./output.ts";
4548
import { PandocRenderCompletion, renderPandoc } from "./render.ts";
4649
import { renderContexts } from "./render-contexts.ts";
@@ -204,6 +207,7 @@ export async function renderExecute(
204207
quiet: flags.quiet,
205208
previewServer: context.options.previewServer,
206209
handledLanguages: languages(),
210+
projectType: context.project?.config?.project?.[kProjectType],
207211
});
208212
popTiming();
209213

src/core/jupyter/jupyter-fixups.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
/*
2-
* jupyter-shared.ts
3-
*
4-
* Copyright (C) 2020-2023 Posit Software, PBC
5-
*
6-
*/
2+
* jupyter-shared.ts
3+
*
4+
* Copyright (C) 2020-2023 Posit Software, PBC
5+
*/
76

87
import { stringify } from "encoding/yaml.ts";
98
import { warning } from "log/mod.ts";
@@ -15,7 +14,7 @@ import { markdownWithExtractedHeading } from "../pandoc/pandoc-partition.ts";
1514
import { partitionYamlFrontMatter, readYamlFromMarkdown } from "../yaml.ts";
1615
import { JupyterNotebook, JupyterOutput } from "./types.ts";
1716

18-
function fixupBokehCells(nb: JupyterNotebook): JupyterNotebook {
17+
export function fixupBokehCells(nb: JupyterNotebook): JupyterNotebook {
1918
for (const cell of nb.cells) {
2019
if (cell.cell_type === "code") {
2120
let needsFixup = false;
@@ -183,16 +182,25 @@ export function fixupFrontMatter(nb: JupyterNotebook): JupyterNotebook {
183182
return nb;
184183
}
185184

186-
const fixups: ((
185+
type JupyterFixup = (nb: JupyterNotebook) => JupyterNotebook;
186+
187+
const defaultFixups: ((
187188
nb: JupyterNotebook,
188189
) => JupyterNotebook)[] = [
189190
fixupBokehCells,
190191
fixupFrontMatter,
191192
];
192193

194+
// books can't have the front matter fixup
195+
export const bookFixups: JupyterFixup[] = [
196+
fixupBokehCells,
197+
];
198+
193199
export function fixupJupyterNotebook(
194200
nb: JupyterNotebook,
201+
explicitFixups?: JupyterFixup[],
195202
): JupyterNotebook {
203+
const fixups = explicitFixups || defaultFixups;
196204
for (const fixup of fixups) {
197205
nb = fixup(nb);
198206
}

src/core/jupyter/jupyter.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
/*
2-
* jupyter.ts
3-
*
4-
* Copyright (C) 2020-2022 Posit Software, PBC
5-
*
6-
*/
2+
* jupyter.ts
3+
*
4+
* Copyright (C) 2020-2022 Posit Software, PBC
5+
*/
76

87
// deno-lint-ignore-file camelcase
98

@@ -155,7 +154,7 @@ import { ProjectContext } from "../../project/types.ts";
155154
import { mergeConfigs } from "../config.ts";
156155
import { encode as encodeBase64 } from "encoding/base64.ts";
157156
import { isIpynbOutput } from "../../config/format.ts";
158-
import { fixupJupyterNotebook } from "./jupyter-fixups.ts";
157+
import { bookFixups, fixupJupyterNotebook } from "./jupyter-fixups.ts";
159158

160159
export const kQuartoMimeType = "quarto_mimetype";
161160
export const kQuartoOutputOrder = "quarto_order";
@@ -656,7 +655,11 @@ export async function jupyterToMarkdown(
656655
options: JupyterToMarkdownOptions,
657656
): Promise<JupyterToMarkdownResult> {
658657
// perform fixups
659-
nb = fixupJupyterNotebook(nb);
658+
const fixups = options.executeOptions.projectType === "book"
659+
? bookFixups
660+
: undefined;
661+
662+
nb = fixupJupyterNotebook(nb, fixups);
660663

661664
// optional content injection / html preservation for html output
662665
// that isn't an ipynb
@@ -1637,9 +1640,9 @@ which does not appear to be plain text: ${JSON.stringify(data)}`);
16371640
} else {
16381641
if (options.toHtml) {
16391642
if (lines.some(hasAnsiEscapeCodes)) {
1640-
const html = (await Promise.all(
1643+
const html = await Promise.all(
16411644
lines.map(convertToHtmlSpans),
1642-
));
1645+
);
16431646
return mdMarkdownOutput(
16441647
[
16451648
"\n::: {.ansi-escaped-output}\n```{=html}\n<pre>",

src/execute/types.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
/*
2-
* types.ts
3-
*
4-
* Copyright (C) 2020-2022 Posit Software, PBC
5-
*
6-
*/
2+
* types.ts
3+
*
4+
* Copyright (C) 2020-2022 Posit Software, PBC
5+
*/
76
import {
87
kIncludeAfterBody,
98
kIncludeBeforeBody,
@@ -82,6 +81,7 @@ export interface ExecuteOptions {
8281
quiet?: boolean;
8382
previewServer?: boolean;
8483
handledLanguages: string[]; // list of languages handled by cell language handlers, after the execution engine
84+
projectType?: string;
8585
}
8686

8787
// result of execution

0 commit comments

Comments
 (0)