Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion clients/gallery/astro/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import Gallery from "visualci-astro-gallery";

The props are `visualci-gallery`'s options (`title`, `headingLevel`, `id`, `screens`, `order`, `groups`,
`groupOrder`, `credit`, `layout`, `themeSwitch`, `seeAll`, `preload`, `deepLinks`, `text`) and `load()`'s (`manifest`, `timeout`, `attempts`,
`fetch`). Give the gallery your site's look with the `--vg-*` CSS variables, set on `.vg` or any element around it.
`fetch`). `<Screenshot manifest="…" screen="overview" link />` (`import { Screenshot } from "visualci-astro-gallery"`)
shows one screenshot in the reader's light or dark mode ([One screenshot](https://visualci.com/docs/gallery/screenshot/)).
Give the gallery your site's look with the `--vg-*` CSS variables, set on `.vg` or any element around it.
They win over the component's defaults in any stylesheet order:

```css
Expand Down
15 changes: 15 additions & 0 deletions clients/gallery/astro/examples/site/src/pages/overview.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
import { Screenshot } from "visualci-astro-gallery";
---

<html lang="en">
<head>
<meta charset="utf-8" />
<title>Overview</title>
</head>
<body>
<h1>Overview</h1>
<!-- One screenshot, in the reader's light or dark mode, linked to the full-size original. -->
<Screenshot manifest="https://app.visualci.com/g/acme/web.json" screen="overview" link />
</body>
</html>
5 changes: 3 additions & 2 deletions clients/gallery/astro/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "visualci-astro-gallery",
"version": "0.4.0",
"version": "0.4.1",
"description": "An Astro component for a screenshots page from VisualCI's public gallery: rendered at build time, hydrated by visualci-gallery.",
"keywords": [
"astro-component",
Expand All @@ -23,7 +23,8 @@
"type": "module",
"exports": {
".": "./src/index.ts",
"./Gallery.astro": "./src/Gallery.astro"
"./Gallery.astro": "./src/Gallery.astro",
"./Screenshot.astro": "./src/Screenshot.astro"
},
"scripts": {
"test": "vitest run"
Expand Down
24 changes: 24 additions & 0 deletions clients/gallery/astro/src/Screenshot.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
// One screenshot from a VisualCI public gallery manifest, rendered when the site builds: the screen's <img>, or a
// <picture> that shows the dark shot to readers whose setting is dark. It needs no script or stylesheet. A manifest
// that can't be read, or a screen with no shot for what's asked, fails the build.
//
// <Screenshot manifest="https://app.visualci.com/g/acme/web.json" screen="overview" link />
//
// The props are the library's screenshot options (screen, group, theme, size, alt, link, thumb, loading, dimensions,
// groups) and how to read the manifest (manifest, timeout, attempts, fetch).
import { loadManifest, screenshot, type ManifestSource, type ScreenshotOptions } from "visualci-gallery/render";

export type Props = ScreenshotOptions & ManifestSource;

const { manifest, timeout, attempts, fetch, ...options } = Astro.props;
const parsed = await loadManifest({ manifest, timeout, attempts, fetch });
let html: string;
try {
html = screenshot(parsed, options);
} catch (e) {
throw new Error(`The screenshot can't show ${manifest}: ${e instanceof Error ? e.message : String(e)}`);
}
---

<Fragment set:html={html} />
3 changes: 2 additions & 1 deletion clients/gallery/astro/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// visualci-astro-gallery: `import Gallery from "visualci-astro-gallery"`.
// visualci-astro-gallery: `import Gallery, { Screenshot } from "visualci-astro-gallery"`.
export { default } from "./Gallery.astro";
export type { Props } from "./Gallery.astro";
export { default as Screenshot } from "./Screenshot.astro";
8 changes: 7 additions & 1 deletion clients/gallery/astro/test/examples.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { execFileSync } from "node:child_process";
import { existsSync, readdirSync, readFileSync, rmSync } from "node:fs";
import { join } from "node:path";
import { afterAll, afterEach, beforeAll, describe, expect, it, vi } from "vitest";
import { buildModel, render, type Manifest } from "visualci-gallery/render";
import { buildModel, render, screenshot, type Manifest } from "visualci-gallery/render";

const site = join(import.meta.dirname, "../examples/site");
const fixture = join(import.meta.dirname, "../../contract/fixtures/fleetlayer/manifest.json");
Expand Down Expand Up @@ -114,6 +114,12 @@ describe("examples/site", () => {
expect(root.dataset.vgTheme).toBe("light");
});

it("overview.astro: one screenshot, a <picture> with the dark shot for dark readers", () => {
const shot = screenshot(JSON.parse(readFileSync(fixture, "utf8")) as Manifest, { screen: "overview", link: true });
expect(shot).toContain("<picture>");
expect(page("overview")).toContain(shot.trim());
});

it("app.astro: with themeSwitch={false} the gallery follows the layout's toggle", async () => {
await open("app");
const root = q("section.vg");
Expand Down
35 changes: 35 additions & 0 deletions clients/gallery/astro/test/screenshot.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// The Screenshot component's markup is the contract's: for each case in contract/screenshots, rendering it with the
// case's options gives exactly its HTML. A screen with no shot for what's asked fails the render.
import { experimental_AstroContainer as AstroContainer } from "astro/container";
import { readdirSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import Screenshot from "../src/Screenshot.astro";

const dir = join(import.meta.dirname, "../../contract/screenshots");
const manifest = readFileSync(join(dir, "manifest.json"), "utf8");
const fetch = async () => new Response(manifest);
const MANIFEST = "https://app.visualci.com/g/acme/web.json";

describe("Screenshot", () => {
for (const file of readdirSync(join(dir, "cases"))
.filter((f) => f.endsWith(".json"))
.sort()) {
it(`renders the ${file} case's markup`, async () => {
const container = await AstroContainer.create();
const html = await container.renderToString(Screenshot, {
props: { ...JSON.parse(readFileSync(join(dir, "cases", file), "utf8")), manifest: MANIFEST, fetch },
});
expect(html).toBe(readFileSync(join(dir, "cases", file.replace(/\.json$/, ".html")), "utf8"));
});
}

it("fails when the screen has no shot for what's asked", async () => {
const container = await AstroContainer.create();
await expect(
container.renderToString(Screenshot, { props: { screen: "billing", theme: "dark", manifest: MANIFEST, fetch } }),
).rejects.toThrow(
`The screenshot can't show ${MANIFEST}: gallery screen billing in group Web has no dark desktop screenshot`,
);
});
});
4 changes: 4 additions & 0 deletions clients/gallery/contract/screenshots/cases/float-2x.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://app.visualci.com/g/acme/web/float-dark-2x.png" width="1441" height="901">
<img src="https://app.visualci.com/g/acme/web/float-light-2x.png" width="1441" height="901" alt="Float" loading="lazy" decoding="async">
</picture>
1 change: 1 addition & 0 deletions clients/gallery/contract/screenshots/cases/float-2x.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"screen": "float"}
4 changes: 4 additions & 0 deletions clients/gallery/contract/screenshots/cases/float-2x.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://app.visualci.com/g/acme/web/float-dark-2x.png" width="1441" height="901">
<img src="https://app.visualci.com/g/acme/web/float-light-2x.png" width="1441" height="901" alt="Float" loading="lazy" decoding="async">
</picture>
12 changes: 12 additions & 0 deletions clients/gallery/contract/screenshots/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@
"width": 1281,
"height": 801
},
{
"name": "float-light-2x",
"url": "https://app.visualci.com/g/acme/web/float-light-2x.png",
"width": 2881.0,
"height": 1801.0
},
{
"name": "float-dark-2x",
"url": "https://app.visualci.com/g/acme/web/float-dark-2x.png",
"width": 2881.0,
"height": 1801.0
},
{
"name": "wide-light-390-2x",
"url": "https://app.visualci.com/g/acme/web/wide-light-390-2x.png",
Expand Down
8 changes: 6 additions & 2 deletions clients/gallery/docusaurus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ import Gallery from "visualci-gallery-docusaurus/Gallery";
```

The component's props are the library's options, passed through as they are. The plugin takes `manifests`, `timeout`
and `attempts`, and adds the stylesheet to every page. A manifest that can't be read, or has nothing to show, fails
the build.
and `attempts`, and adds the stylesheet to every page. A manifest that can't be read fails
the build, and so does a page whose gallery has nothing to show.

`visualci-gallery-docusaurus/Screenshot` shows one screenshot from a manifest the plugin reads, in the reader's light
or dark mode: `<Screenshot manifest="https://app.visualci.com/g/acme/web.json" screen="overview" link />`. See
[One screenshot](https://visualci.com/docs/gallery/screenshot/).

## Development

Expand Down
9 changes: 9 additions & 0 deletions clients/gallery/docusaurus/examples/site/docs/overview.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Overview
---

import Screenshot from "visualci-gallery-docusaurus/Screenshot";

One screenshot, in the reader's light or dark mode, linked to the full-size original.

<Screenshot manifest="https://app.visualci.com/g/acme/web.json" screen="overview" link />
6 changes: 5 additions & 1 deletion clients/gallery/docusaurus/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "visualci-gallery-docusaurus",
"version": "0.4.0",
"version": "0.4.1",
"description": "A Docusaurus plugin and component for a screenshots page from VisualCI's public gallery: the manifest is read when the site builds, the gallery rendered into each page's HTML and hydrated by visualci-gallery.",
"keywords": [
"docusaurus",
Expand Down Expand Up @@ -30,6 +30,10 @@
"./Gallery": {
"types": "./dist/Gallery.d.ts",
"default": "./dist/Gallery.js"
},
"./Screenshot": {
"types": "./dist/Screenshot.d.ts",
"default": "./dist/Screenshot.js"
}
},
"scripts": {
Expand Down
37 changes: 37 additions & 0 deletions clients/gallery/docusaurus/src/Screenshot.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// visualci-gallery-docusaurus/Screenshot: one screenshot, in MDX or a React page: the screen's <img>, or a <picture>
// that shows the dark shot to readers whose setting is dark. Its manifest must be one of the plugin's `manifests`,
// read when the site built; the props are the library's screenshot options. It needs no script.
//
// import Screenshot from "visualci-gallery-docusaurus/Screenshot";
// <Screenshot manifest="https://app.visualci.com/g/acme/web.json" screen="overview" link />
import { usePluginData } from "@docusaurus/useGlobalData";
import { screenshot, type ScreenshotOptions } from "visualci-gallery/render";
import type { GlobalData } from "./index.js";

export interface ScreenshotProps extends ScreenshotOptions {
/** The manifest's URL, as it appears in the plugin's `manifests`. */
manifest: string;
className?: string;
}

function shown(manifest: string, run: () => string): string {
try {
return run();
} catch (e) {
throw new Error(`The screenshot can't show ${manifest}: ${e instanceof Error ? e.message : String(e)}`);
}
}

export default function Screenshot({ manifest, className, ...options }: ScreenshotProps) {
const data = usePluginData("visualci-gallery-docusaurus") as GlobalData | undefined;
const parsed = data?.manifests[manifest];
if (parsed === undefined) {
throw new Error(`visualci-gallery-docusaurus: add ${manifest} to the plugin's manifests in docusaurus.config`);
}
return (
<span
className={className}
dangerouslySetInnerHTML={{ __html: shown(manifest, () => screenshot(parsed, options)) }}
/>
);
}
29 changes: 7 additions & 22 deletions clients/gallery/docusaurus/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// visualci-gallery-docusaurus: the plugin. It reads each manifest in `manifests` when the site builds (or the dev
// server starts) and hands them to the Gallery component as the plugin's global data; it adds the library's
// stylesheet to every page. A manifest that can't be read, or has nothing to show, fails the build.
// stylesheet to every page. A manifest that can't be read fails the build, as does a gallery or a screenshot it
// can't show.
//
// plugins: [["visualci-gallery-docusaurus", { manifests: ["https://app.visualci.com/g/acme/web.json"] }]]
import { createRequire } from "node:module";
import type { LoadContext, Plugin } from "@docusaurus/types";
import { load, type Manifest } from "visualci-gallery/render";
import { loadManifest, type Manifest } from "visualci-gallery/render";

export interface PluginOptions {
/** The manifests the site's galleries show: `https://app.visualci.com/g/<org>/<project>.json`. */
Expand All @@ -25,26 +26,10 @@ export interface GlobalData {

export const PLUGIN_NAME = "visualci-gallery-docusaurus";

/** Reads one manifest with load()'s retries and checks (it throws when the manifest can't be read or shows nothing),
* keeping the parsed JSON for the component to render with each gallery's own options. */
async function read(url: string, options: PluginOptions): Promise<Manifest> {
const get = options.fetch ?? fetch;
let manifest: Manifest | undefined;
await load({
manifest: url,
timeout: options.timeout,
attempts: options.attempts,
fetch: async (input, init) => {
const res = await get(input, init);
if (res.ok)
manifest = (await res
.clone()
.json()
.catch(() => undefined)) as Manifest | undefined;
return res;
},
});
return manifest!;
/** Reads one manifest with loadManifest()'s retries (it throws when the manifest can't be read), keeping the parsed
* JSON for the components to render with each one's own options: a gallery or a screenshot checks what it shows. */
function read(url: string, options: PluginOptions): Promise<Manifest> {
return loadManifest({ manifest: url, timeout: options.timeout, attempts: options.attempts, fetch: options.fetch });
}

export default function visualciGallery(_context: LoadContext, options: PluginOptions): Plugin<GlobalData> {
Expand Down
8 changes: 7 additions & 1 deletion clients/gallery/docusaurus/test/examples.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { execFileSync } from "node:child_process";
import { existsSync, readdirSync, readFileSync, rmSync } from "node:fs";
import { join } from "node:path";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { buildModel, render, type Manifest, type Options } from "visualci-gallery/render";
import { buildModel, render, screenshot, type Manifest, type Options } from "visualci-gallery/render";

const site = join(import.meta.dirname, "../examples/site");
const fixture = join(import.meta.dirname, "../../contract/fixtures/fleetlayer/manifest.json");
Expand Down Expand Up @@ -76,6 +76,12 @@ describe("examples/site", () => {
);
});

it("docs/overview.mdx: one screenshot, a <picture> with the dark shot for dark readers", () => {
const shot = screenshot(JSON.parse(readFileSync(fixture, "utf8")) as Manifest, { screen: "overview", link: true });
expect(shot).toContain("<picture>");
expect(page("docs/overview")).toContain(`<span>${shot}</span>`);
});

it("docs/app.mdx: with themeSwitch={false} the gallery follows Docusaurus's toggle", () => {
expect(page("docs/app")).toContain(markup({ title: null, themeSwitch: false }));
});
Expand Down
8 changes: 5 additions & 3 deletions clients/gallery/docusaurus/test/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// The plugin on its own: it reads each manifest into its global data, refuses a config without manifests, and fails
// (so the build does) when a manifest can't be read or has nothing to show.
// (so the build does) when a manifest can't be read.
import { readFileSync } from "node:fs";
import { join } from "node:path";
import type { LoadContext } from "@docusaurus/types";
Expand Down Expand Up @@ -45,7 +45,9 @@ describe("visualci-gallery-docusaurus", () => {
);
});

it("fails when a manifest has nothing to show", async () => {
await expect(content({ manifests: [MANIFEST], fetch: serve('{"groups":[]}') })).rejects.toThrow(/can't show/);
it("keeps a manifest as it was read: the components check what they show", async () => {
await expect(content({ manifests: [MANIFEST], fetch: serve('{"groups":[]}') })).resolves.toEqual({
manifests: { [MANIFEST]: { groups: [] } },
});
});
});
5 changes: 4 additions & 1 deletion clients/gallery/hugo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ script and stylesheet (this module's version, from jsDelivr when the site builds
fingerprinted) bring it to life. A manifest that can't be read, or has nothing to show, and invalid options fail the
build. Needs Hugo 0.146 or later.

For one screenshot, in the reader's light or dark mode, `{{< visualci-screenshot screen="overview" ... >}}` and the
`visualci-screenshot.html` partial ([One screenshot](https://visualci.com/docs/gallery/screenshot/)).

```sh
hugo mod get github.com/DocSpring/visualci/clients/gallery/hugo@v0.4.0
hugo mod get github.com/DocSpring/visualci/clients/gallery/hugo@v0.4.1
```

```toml
Expand Down
7 changes: 7 additions & 0 deletions clients/gallery/hugo/examples/site/content/overview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
title: Overview
---

One screenshot, in the reader's light or dark mode, linked to the full-size original.

{{< visualci-screenshot manifest="https://app.visualci.com/g/acme/web.json" screen="overview" link=true >}}
2 changes: 1 addition & 1 deletion clients/gallery/hugo/examples/site/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module acme.example/site

go 1.22

require github.com/DocSpring/visualci/clients/gallery/hugo v0.4.0
require github.com/DocSpring/visualci/clients/gallery/hugo v0.4.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{{- /* The screen's variant with exactly the values in .choice for every dimension in .order, or nil. */ -}}
{{- $found := index dict "none" -}}
{{- range $v := .variants -}}
{{- $match := true -}}
{{- range $k := $.order }}{{ if ne (index $v.values $k) (index $.choice $k) }}{{ $match = false }}{{ end }}{{ end -}}
{{- if $match }}{{ $found = $v }}{{ break }}{{ end -}}
{{- end -}}
{{- return $found -}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{{- /* A screenshot (. is screenshot.html's shown) as HTML: an <img>, or a <picture> with the dark shot. */ -}}
{{- $e := "visualci-gallery/escape.html" -}}
{{- $l := .light -}}
{{- $srcset := cond (ne $l.src2x nil) (printf ` srcset="%s"` (partial "visualci-gallery/screenshot-srcset.html" $l)) "" -}}
{{- $img := printf `<img src="%s"%s width="%v" height="%v" alt="%s" loading="%s" decoding="async">` (partial $e $l.src) $srcset $l.width $l.height (partial $e .alt) .loading -}}
{{- $open := cond .link (printf `<a href="%s">` (partial $e .href)) "" -}}
{{- $close := cond .link "</a>" "" -}}
{{- $out := printf "%s%s%s\n" $open $img $close -}}
{{- with .dark -}}
{{- $source := printf `<source media="(prefers-color-scheme: dark)" srcset="%s" width="%v" height="%v">` (partial "visualci-gallery/screenshot-srcset.html" .) .width .height -}}
{{- $out = printf "%s<picture>\n%s\n%s\n</picture>%s\n" $open $source $img $close -}}
{{- end -}}
{{- return $out -}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{{- /* A screenshot (. is screenshot.html's shown) for a README: Markdown's ![alt](url), linked when asked, or the HTML
<picture> when it has a dark shot. Markdown's backslash escapes every ASCII punctuation character in the alt, and the
characters that would end a URL, start an escape or an entity in it. */ -}}
{{- $out := "" -}}
{{- if .dark -}}
{{- $out = partial "visualci-gallery/screenshot-html.html" . -}}
{{- else -}}
{{- $alt := replaceRE "([!-/:-@\\[-`{-~])" `\$1` .alt -}}
{{- $url := `([\\()&])` -}}
{{- $out = printf "![%s](%s)" $alt (replaceRE $url `\$1` .light.src) -}}
{{- if .link }}{{ $out = printf "[%s](%s)" $out (replaceRE $url `\$1` .href) }}{{ end -}}
{{- $out = printf "%s\n" $out -}}
{{- end -}}
{{- return $out -}}
Loading
Loading