Skip to content

Commit

Permalink
add patch
Browse files Browse the repository at this point in the history
  • Loading branch information
cjtantay committed Nov 6, 2024
1 parent 46910b1 commit 187ac0e
Showing 1 changed file with 48 additions and 42 deletions.
90 changes: 48 additions & 42 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import {
import { createInjectBlock } from './blocks/inject.js';
import { createScriptBlock } from './blocks/script.js';
import { createStaticBlock } from './blocks/static.js';
import path from 'node:path';
import glob from 'fast-glob';

// filters
import { dateFilter } from './filters/date.js';
Expand Down Expand Up @@ -374,13 +376,13 @@ export class Baker extends EventEmitter {
*/

async buildEmbedFallbacks(baseUrl) {
const distDir = this.output.split('/').slice(0, -1).join('/');
const embedFilePattern = path.join(distDir, 'embeds', '**/*.html');
/**
* An array of file paths representing embed files.
* @type {string[]}
*/
const embedFiles = Object.values(this.nunjucks.manifest).filter((path) =>
path.includes('embeds')
);
const embedFiles = glob.sync(embedFilePattern);

if (!embedFiles.length) return;

Expand All @@ -389,47 +391,51 @@ export class Baker extends EventEmitter {
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});

for (const embedFilePath of embedFiles) {
try {
const embedName = path.basename(embedFilePath, '.html');
const embedPath = `embeds/${embedName}/index.html`;
const screenshotLocalUrl = `${baseUrl}/${embedPath}`;
console.log(`Taking screenshot of: ${screenshotLocalUrl}`);

const page = await browser.newPage();
await page.goto(screenshotLocalUrl, {
waitUntil: 'networkidle0',
});

// set the viewport to the content height
const contentHeight = await page.evaluate(() => {
return Math.max(
document.body.scrollHeight,
document.documentElement.scrollHeight,
document.body.offsetHeight,
document.documentElement.offsetHeight,
document.body.clientHeight,
document.documentElement.clientHeight
);
});
await page.setViewport({
width: FIXED_FALLBACK_SCREENSHOT_WIDTH,
height: contentHeight,
deviceScaleFactor: 2,
});

await page.waitForNetworkIdle();

// store the fallback image in the _dist directory
const screenshotEmbedDir = join(this.output, embedPath)
.split('/')
.slice(0, -1)
.join('/')
.replace('_screenshot', '_dist');
const screenshotStoragePath = join(screenshotEmbedDir, 'fallback.png');
console.log(`Storing the fallback image at: ${screenshotStoragePath}.`);

for (const embedFilePath of embedFiles) {
const screenshotLocalUrl = `${baseUrl}/${embedFilePath}`;
console.log(`Taking screenshot of: ${screenshotLocalUrl}`);

const page = await browser.newPage();
await page.goto(screenshotLocalUrl, {
waitUntil: 'networkidle0',
});

// set the viewport to the content height
const contentHeight = await page.evaluate(() => {
return Math.max(
document.body.scrollHeight,
document.documentElement.scrollHeight,
document.body.offsetHeight,
document.documentElement.offsetHeight,
document.body.clientHeight,
document.documentElement.clientHeight
);
});
await page.setViewport({
width: FIXED_FALLBACK_SCREENSHOT_WIDTH,
height: contentHeight,
deviceScaleFactor: 2,
});

await page.waitForNetworkIdle();

// store the fallback image in the _dist directory
const screenshotEmbedDir = join(this.output, embedFilePath)
.split('/')
.slice(0, -1)
.join('/')
.replace('_screenshot', '_dist');
const screenshotStoragePath = join(screenshotEmbedDir, 'fallback.png');
console.log(`Storing the fallback image at: ${screenshotStoragePath}.`);

await page.screenshot({ path: screenshotStoragePath, fullPage: true });
await page.close();
await page.screenshot({ path: screenshotStoragePath, fullPage: true });
await page.close();
} catch (err) {
console.error(`Failed to process ${embedFilePath}: ${err.message}`);
}
}

await browser.close();
Expand Down

0 comments on commit 187ac0e

Please sign in to comment.