-
Notifications
You must be signed in to change notification settings - Fork 432
Expand file tree
/
Copy pathnotebook-types.ts
More file actions
146 lines (131 loc) · 4.2 KB
/
notebook-types.ts
File metadata and controls
146 lines (131 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
* notebook-context.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*/
import {
ExecutedFile,
RenderResourceFiles,
RenderServices,
} from "../../command/render/types.ts";
import { Format } from "../../config/types.ts";
import { ProjectContext } from "../../project/types.ts";
export const kRemoteHref = "remote-href";
export const kHtmlPreview = "html-preview";
export const kJatsSubarticle = "jats-subarticle";
export const kRenderedIPynb = "rendered-ipynb";
export const kQmdIPynb = "qmd-ipynb";
export type RenderType =
| "html-preview"
| "jats-subarticle"
| "rendered-ipynb"
| "qmd-ipynb";
// Preview Options for HTML Previews
export interface NotebookPreviewOptions {
back?: boolean;
}
// The core notebook interface
export interface Notebook {
source: string;
metadata?: NotebookMetadata;
[kHtmlPreview]?: NotebookOutput;
[kJatsSubarticle]?: NotebookOutput;
[kRenderedIPynb]?: NotebookOutput;
[kQmdIPynb]?: NotebookOutput;
}
export interface NotebookOutput {
// The literal path to the notebook output
// This could be a file in the output directory, in a cache, or from anywhere really.
path: string;
// The original input like path to the notebook output
// which should be used when forming input or project relative paths to be used
// for things like making links, or computing resources/supporting files
hrefPath: string;
// The list of supporting files and resources
supporting: string[];
resourceFiles: RenderResourceFiles;
// A flag indicating whether this output was provided by a cache
// (in which case, it will not be cleaned up, for example)
cached?: boolean;
}
export interface NotebookRenderResult {
file: string;
supporting?: string[];
resourceFiles: RenderResourceFiles;
}
// Metadata that can be passed when rendering/resolving a notebook
export interface NotebookMetadata {
title: string;
filename: string;
downloadHref?: string;
backHref?: string;
downloadFile?: string;
}
// The template for notebook views is expecting this schema
export interface NotebookTemplateMetadata extends NotebookMetadata {
downloadLabel: string;
backLabel: string;
}
export interface NotebookContext {
// Retrieves the notebook from the context.
get: (nbPath: string, context: ProjectContext) => Notebook | undefined;
// returns a file name with the JSON serialization of all notebooks, Notebook[]
all: (context: ProjectContext) => string;
// Resolves the data on an executedFile into data that will
// create a `renderType` output when rendered.
addMetadata: (nbPath: string, notebookMetadata: NotebookMetadata) => void;
resolve: (
nbPath: string,
renderType: RenderType,
executedFile: ExecutedFile,
notebookMetadata?: NotebookMetadata,
) => ExecutedFile;
// Provide a preview to the notebook context (for example, if you rendered it yourself)
addRendering: (
nbPath: string,
renderType: RenderType,
result: NotebookRenderResult,
project: ProjectContext,
) => void;
removeRendering: (
nbAbsPath: string,
renderType: RenderType,
preserveFiles: string[],
) => void;
// Render a preview from scratch
render: (
nbPath: string,
format: Format,
renderType: RenderType,
renderServices: RenderServices,
notebookMetadata: NotebookMetadata | undefined,
project: ProjectContext,
) => Promise<NotebookOutput>;
// Previews are cleaned up when the notebook context is disposed, but
// you can use this to mark specific notebook > rendertypes to not be cleaned up.
preserve: (nbAbsPath: string, renderType: RenderType) => void;
// called to cleanup the context
cleanup: () => void;
}
export interface NotebookContributor {
outputFile(nbAbsPath: string): string;
resolve(
nbAbsPath: string,
token: string,
executedFile: ExecutedFile,
notebookMetadata?: NotebookMetadata,
): ExecutedFile;
render(
nbAbsPath: string,
format: Format,
token: string,
services: RenderServices,
notebookMetadata: NotebookMetadata | undefined,
project: ProjectContext,
): Promise<NotebookRenderResult>;
cache?: (output: NotebookOutput, project: ProjectContext) => void;
cachedPath?: (
nbAbsPath: string,
project: ProjectContext,
) => string | undefined;
}