Skip to content

Commit 07c9edb

Browse files
authored
Merge pull request #12694 from quarto-dev/bugfix/12693
NotebookContext - don't write all notebooks into environment
2 parents 41de2d1 + 632b527 commit 07c9edb

File tree

5 files changed

+30
-5
lines changed

5 files changed

+30
-5
lines changed

news/changelog-1.8.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ All changes included in 1.8:
2626

2727
- ([#12551](https://github.com/quarto-dev/quarto-cli/pull/12551)): Improve warning issued when `aliases` would overwrite an existing document.
2828
- ([#12616](https://github.com/quarto-dev/quarto-cli/issues/12616)): find SVG images in image discovery for listings.
29+
- ([#12693](https://github.com/quarto-dev/quarto-cli/issues/12693)): Prevent resource exhaustion on large websites by serializing `NotebookContext` information to file instead of the environment.
2930

3031
## Crossrefs
3132

src/command/render/filters.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ function jatsFilterParams(options: PandocOptions) {
549549

550550
function notebookContextFilterParams(options: PandocOptions) {
551551
const nbContext = options.services.notebook;
552-
const notebooks = nbContext.all();
552+
const notebooks = nbContext.all(options.project);
553553
if (notebooks.length > 0) {
554554
return {
555555
"notebook-context": notebooks,

src/render/notebook/notebook-context.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export function notebookContext(): NotebookContext {
7575
const nb: Notebook = notebooks[nbAbsPath] || emptyNotebook(nbAbsPath);
7676
nb[renderType] = output;
7777
notebooks[nbAbsPath] = nb;
78+
needRewrite = true;
7879

7980
if (context) {
8081
const contrib = contributor(renderType);
@@ -191,9 +192,28 @@ export function notebookContext(): NotebookContext {
191192
}
192193
}
193194

195+
let allNotebooksTempFilename: string | undefined;
196+
let needRewrite = true;
197+
194198
return {
195-
all: () => {
196-
return Object.values(notebooks);
199+
all: (context: ProjectContext) => {
200+
if (!allNotebooksTempFilename) {
201+
allNotebooksTempFilename = context.temp.createFile({
202+
suffix: ".json",
203+
});
204+
}
205+
if (needRewrite) {
206+
debug(
207+
`[NotebookContext]: Writing all notebooks to ${allNotebooksTempFilename}`,
208+
);
209+
const objs = Object.values(notebooks);
210+
Deno.writeTextFileSync(
211+
allNotebooksTempFilename,
212+
JSON.stringify(objs),
213+
);
214+
needRewrite = false;
215+
}
216+
return allNotebooksTempFilename;
197217
},
198218
get: (nbAbsPath: string, context?: ProjectContext) => {
199219
debug(`[NotebookContext]: Get Notebook:${nbAbsPath}`);

src/render/notebook/notebook-types.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ export interface NotebookTemplateMetadata extends NotebookMetadata {
8181
export interface NotebookContext {
8282
// Retrieves the notebook from the context.
8383
get: (nbPath: string, context: ProjectContext) => Notebook | undefined;
84-
all: () => Notebook[];
84+
85+
// returns a file name with the JSON serialization of all notebooks, Notebook[]
86+
all: (context: ProjectContext) => string;
87+
8588
// Resolves the data on an executedFile into data that will
8689
// create a `renderType` output when rendered.
8790
addMetadata: (nbPath: string, notebookMetadata: NotebookMetadata) => void;

src/resources/filters/layout/manuscript.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ function manuscript()
8282

8383
-- Use the notebook cotnext to try to determine the name
8484
-- of the output file
85-
local notebooks = param("notebook-context", {})
85+
local notebooks_filename = param("notebook-context", {})
86+
local notebooks = quarto.json.decode(io.open(notebooks_filename, "r"):read("*a"))
8687
local nbFileName = pandoc.path.filename(nbRelPath)
8788
local previewFile = nbFileName .. ".html"
8889
for _i, notebook in ipairs(notebooks) do

0 commit comments

Comments
 (0)