Skip to content

Commit

Permalink
Merge pull request #479 from beeminder/snap
Browse files Browse the repository at this point in the history
Add snap param to increase puppeteer diffing reliability
  • Loading branch information
narthur authored Mar 20, 2024
2 parents c42d8f4 + b900180 commit 41292a0
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 44 deletions.
8 changes: 6 additions & 2 deletions scripts/puppeteer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ async function handleUrl(browser: Browser, url: string) {
const pathname = new URL(url).pathname;
console.time(pathname);

const path1 = await snap(browser, url, "base");
const path2 = await snap(browser, url.replace(base, compare), "compare");
const path1 = await snap(browser, `${url}?snap`, "base");
const path2 = await snap(
browser,
`${url.replace(base, compare)}?snap`,
"compare",
);
const img1 = readScreenshot(path1);
const img2 = readScreenshot(path2);
const meta1 = await img1.metadata();
Expand Down
4 changes: 3 additions & 1 deletion src/components/Shadowbox.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
interface Props {
padding?: number;
class?: string | undefined;
id?: string | undefined;
}
const { padding = 1, class: className } = Astro.props;
const { padding = 1, class: className, id } = Astro.props;
---

<div
id={id}
class:list={["shadowbox", className]}
style={{
"--gap-multiplier": padding,
Expand Down
6 changes: 5 additions & 1 deletion src/components/Sidebar.astro
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ const sha: string | undefined = import.meta.env.RENDER_GIT_COMMIT;
</Shadowbox>
{
sha && (
<a class="sha" href={`https://github.com/beeminder/blog/commit/${sha}`}>
<a
id="sha"
class="sha"
href={`https://github.com/beeminder/blog/commit/${sha}`}
>
{sha.slice(0, 7)}
</a>
)
Expand Down
3 changes: 2 additions & 1 deletion src/layouts/Layout.astro
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const {
const canonicalUrl = new URL(Astro.url.pathname, Astro.site);
---

<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<link
Expand Down Expand Up @@ -83,6 +83,7 @@ const canonicalUrl = new URL(Astro.url.pathname, Astro.site);
async
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script>

<script src="../scripts/snap.ts"></script>
<script src="../scripts/slideshow.ts"></script>
<script src="../scripts/hotkeys.ts"></script>

Expand Down
72 changes: 33 additions & 39 deletions src/pages/[slug].astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
import type { InferGetStaticPropsType } from "astro";
import type { InferGetStaticPropsType } from "astro";
import Tags from "../components/Tags.astro";
import getPosts from "../lib/getPosts";
import Shadowbox from "../components/Shadowbox.astro";
Expand All @@ -12,10 +12,10 @@ import Paginated from "../layouts/Paginated.astro";
export async function getStaticPaths() {
const posts = await getPosts({
includeUnpublished: true,
sort: true
sort: true,
});
const published = (p: Post) => p.status === "publish"
const published = (p: Post) => p.status === "publish";
return posts.map((post: Post, i: number) => ({
params: {
Expand All @@ -31,22 +31,9 @@ export async function getStaticPaths() {
type Props = InferGetStaticPropsType<typeof getStaticPaths>;
const {
post,
newer,
older
} = Astro.props as Props;
const { post, newer, older } = Astro.props as Props;
const {
content,
title,
tags,
disqus_id,
excerpt,
image,
status,
slug,
} = post
const { content, title, tags, disqus_id, excerpt, image, status, slug } = post;
const isDraft = status === "draft";
const className = isDraft ? "draft" : undefined;
Expand All @@ -66,32 +53,39 @@ const { extracted, ...imageProps } = image || {};
url: `/${older.slug}`,
}}
>
<div class="post">
<Shadowbox padding={2} class={className}>
<PostMeta
post={{
...post,
title,
}}
includeExcerpt={false}
linkTitle={false}
Heading="h1"
/>
{extracted ? undefined : <img class="aligncenter" {...imageProps} />}
<Typography set:html={content} />
<Tags tags={tags} />
</Shadowbox>
<div class="post">
<Shadowbox padding={2} class={className}>
<PostMeta
post={{
...post,
title,
}}
includeExcerpt={false}
linkTitle={false}
Heading="h1"
/>
{extracted ? undefined : <img class="aligncenter" {...imageProps} />}
<Typography set:html={content} />
<Tags tags={tags} />
</Shadowbox>

<Shadowbox padding={2}>
{isDraft ? <p>Comments are disabled on DRAFT pages.</p> : <Comments id={disqus_id} url={`https://blog.beeminder.com/${slug}/`} />}
</Shadowbox>
</div>
<Shadowbox id="comments" padding={2}>
{
isDraft ? (
<p>Comments are disabled on DRAFT pages.</p>
) : (
<Comments
id={disqus_id}
url={`https://blog.beeminder.com/${slug}/`}
/>
)
}
</Shadowbox>
</div>
</Paginated>


<style>
.post :global(.draft) {
border: 1rem solid #ff700a;
}
</style>

8 changes: 8 additions & 0 deletions src/scripts/snap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Purpose: Improve reliability of puppeteer diffing

const urlParams = new URLSearchParams(window.location.search);

if (urlParams.has("snap")) {
const body = document.querySelector("body");
body?.classList.add("snap");
}
11 changes: 11 additions & 0 deletions src/styles/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,14 @@ ol {
margin-left: auto;
margin-right: auto;
}

/* .snap purpose: improve puppeteer diffing reliability */

body.snap #comments,
body.snap #sha {
display: none;
}

body.snap img {
filter: brightness(0);
}

0 comments on commit 41292a0

Please sign in to comment.