Skip to content

Commit 9c31fe4

Browse files
committed
Added notebook.$(cell).
1 parent d734d58 commit 9c31fe4

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ Returns a Promise that resolves when the cell named `cell` is `"fulfilled"` (see
192192

193193
Replace the [FileAttachments](https://observablehq.com/@observablehq/file-attachments) of the notebook with those defined in `files`. `files` is an object where the keys are the names of the FileAttachment, and the values are the absolute paths to the files that will replace the FileAttachments.
194194

195+
### notebook.**\$**(cell)
196+
197+
Returns the [`ElementHandle`](https://pptr.dev/#?product=Puppeteer&version=v7.1.0&show=api-class-elementhandle) of the container HTML element for the given observed cell. Can be used to call `.click()`, `.screenshot()`, `.evaluate()`, or any other method to have more control of a specfic rendered cell.
198+
195199
## CLI Reference
196200

197201
`observable-prerender` also comes bundled with 2 CLI programs, `observable-prerender` and `observable-prerender-animate`, that allow you to more quickly pre-render notebooks and integrate with local files and other CLI tools.

src/index.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ class Notebook {
3737
async close() {
3838
return this.launchedBrowser ? this.browser.close() : this.page.close();
3939
}
40+
async $(cellName) {
41+
return this.page.$(`#notebook-${serializeCellName(cellName)}`);
42+
}
4043
async _value(cell) {
4144
await this.page.waitForFunction(() => window.notebookModule);
4245
return await this.page.evaluate(async (cell) => {
@@ -76,19 +79,17 @@ class Notebook {
7679
}
7780
async html(cell, path) {
7881
await this.waitFor(cell);
79-
const html = await this.page.$eval(
80-
`#notebook-${serializeCellName(cell)}`,
81-
(e) => e.innerHTML
82+
const html = await this.$(cell).then((container) =>
83+
container.evaluate((e) => e.innerHTML)
8284
);
8385
if (path) return rw.writeFileSync(path, html);
8486
return html;
8587
}
8688
// inspired by https://observablehq.com/@mbostock/saving-svg
8789
async svg(cell, path) {
8890
await this.waitFor(cell);
89-
const html = await this.page.$eval(
90-
`#notebook-${serializeCellName(cell)} svg`,
91-
(e) => {
91+
const html = await this.$(cell).then((container) =>
92+
container.$eval(`svg`, (e) => {
9293
const xmlns = "http://www.w3.org/2000/xmlns/";
9394
const xlinkns = "http://www.w3.org/1999/xlink";
9495
const svgns = "http://www.w3.org/2000/svg";
@@ -113,7 +114,7 @@ class Notebook {
113114
const serializer = new window.XMLSerializer();
114115
const string = serializer.serializeToString(svg);
115116
return string;
116-
}
117+
})
117118
);
118119
if (path)
119120
return new Promise((resolve, reject) =>
@@ -125,7 +126,7 @@ class Notebook {
125126
}
126127
async screenshot(cell, path, options = {}) {
127128
await this.waitFor(cell);
128-
const container = await this.page.$(`#notebook-${serializeCellName(cell)}`);
129+
const container = await this.$(cell);
129130
return await container.screenshot({ path, ...options });
130131
}
131132

0 commit comments

Comments
 (0)