From ca0fd50f85d77e7f1d221ee053f780ff7c70f3f9 Mon Sep 17 00:00:00 2001 From: Donghyun Lee Date: Fri, 23 Oct 2026 12:55:59 +0900 Subject: [PATCH 1/6] build: move the demo to site/ as a two-page Vite app Co-Authored-By: Claude Opus 5.5 --- .github/workflows/ci.yml | 1 + .github/workflows/pages.yml | 6 ++-- .gitignore | 5 +++ CONTRIBUTING.md | 18 +++++----- README.md | 2 +- examples/vanilla/tsconfig.json | 10 ------ examples/vanilla/vite.config.ts | 10 ------ pnpm-lock.yaml | 38 ++++++++++---------- pnpm-workspace.yaml | 2 +- scripts/render-doc-images.ts | 8 ++--- site/docs/index.html | 12 +++++++ site/e2e/pages.spec.ts | 47 +++++++++++++++++++++++++ {examples/vanilla => site}/index.html | 0 {examples/vanilla => site}/main.ts | 0 {examples/vanilla => site}/package.json | 6 ++-- {examples/vanilla => site}/passage.ts | 0 site/playwright.config.ts | 18 ++++++++++ {examples/vanilla => site}/snippets.ts | 0 {examples/vanilla => site}/style.css | 0 site/tsconfig.json | 7 ++++ site/vite.config.ts | 20 +++++++++++ site/vitest.config.ts | 14 ++++++++ vitest.workspace-aliases.ts | 2 +- 23 files changed, 166 insertions(+), 60 deletions(-) delete mode 100644 examples/vanilla/tsconfig.json delete mode 100644 examples/vanilla/vite.config.ts create mode 100644 site/docs/index.html create mode 100644 site/e2e/pages.spec.ts rename {examples/vanilla => site}/index.html (100%) rename {examples/vanilla => site}/main.ts (100%) rename {examples/vanilla => site}/package.json (74%) rename {examples/vanilla => site}/passage.ts (100%) create mode 100644 site/playwright.config.ts rename {examples/vanilla => site}/snippets.ts (100%) rename {examples/vanilla => site}/style.css (100%) create mode 100644 site/tsconfig.json create mode 100644 site/vite.config.ts create mode 100644 site/vitest.config.ts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6ede4a0..a6fa4b2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -26,3 +26,4 @@ jobs: - run: pnpm test - run: pnpm test:perf - run: pnpm build + - run: pnpm --filter overlapmark-site test:e2e diff --git a/.github/workflows/pages.yml b/.github/workflows/pages.yml index 94678aa..1437590 100644 --- a/.github/workflows/pages.yml +++ b/.github/workflows/pages.yml @@ -1,6 +1,6 @@ name: Pages -# Builds the demo (examples/vanilla) and deploys it to GitHub Pages at +# Builds the site (site/: the demo and the docs) and deploys it to GitHub Pages at # https://edonghyun.github.io/overlapmark/. Off until the repository # variable PAGES_ENABLED is 'true' and Pages is set to deploy from Actions. on: @@ -33,9 +33,9 @@ jobs: - run: corepack enable - run: pnpm install --frozen-lockfile # The site is served from the /overlapmark/ subpath. - - run: pnpm --filter vanilla-example build --base /overlapmark/ + - run: pnpm --filter overlapmark-site build --base /overlapmark/ - uses: actions/upload-pages-artifact@v5 with: - path: examples/vanilla/dist + path: site/dist - id: deployment uses: actions/deploy-pages@v5 diff --git a/.gitignore b/.gitignore index dda3455..8072463 100644 --- a/.gitignore +++ b/.gitignore @@ -32,7 +32,12 @@ __screenshots__ # local-only planning docs docs/ +!site/docs/ # acceptance-test working files acceptance/.work/ packages/*/LICENSE + +# Playwright output of the site e2e suite +site/test-results/ +site/playwright-report/ diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 3a5328e..c41a543 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -14,15 +14,15 @@ pnpm exec playwright install chromium # browser tests The repository is a pnpm workspace: -| Path | What it is | -| --------------------------------- | ---------------------------------------------------------------------------------------------- | -| `packages/core` | `@overlapmark/core`: segments, render contract, DOM helpers, `styles.css` | -| `packages/dom` | `@overlapmark/dom`, the vanilla JS adapter | -| `packages/vue`, `react`, `svelte` | The framework adapters | -| `packages/adapter-contract` | Private: the browser test suite every adapter runs | -| `acceptance` | End-to-end tests of apps installed from the packed tarballs | -| `examples/vanilla` | The [live demo](https://edonghyun.github.io/overlapmark/): `pnpm --filter vanilla-example dev` | -| `docs-assets` | README images, rendered from the demo by `pnpm docs:images` | +| Path | What it is | +| --------------------------------- | -------------------------------------------------------------------------------------------------------- | +| `packages/core` | `@overlapmark/core`: segments, render contract, DOM helpers, `styles.css` | +| `packages/dom` | `@overlapmark/dom`, the vanilla JS adapter | +| `packages/vue`, `react`, `svelte` | The framework adapters | +| `packages/adapter-contract` | Private: the browser test suite every adapter runs | +| `acceptance` | End-to-end tests of apps installed from the packed tarballs | +| `site` | The [live demo and docs](https://edonghyun.github.io/overlapmark/): `pnpm --filter overlapmark-site dev` | +| `docs-assets` | README images, rendered from the site by `pnpm docs:images` | ## Checks diff --git a/README.md b/README.md index d936a74..3978c36 100644 --- a/README.md +++ b/README.md @@ -101,7 +101,7 @@ highlights = findMatches(text, 'highlights').map((m, i) => ({ hl.update({ highlights }); ``` -The [live demo](https://edonghyun.github.io/overlapmark/) lives in [`examples/vanilla`](examples/vanilla): `pnpm --filter vanilla-example dev`. +The [live demo and docs](https://edonghyun.github.io/overlapmark/) live in [`site`](site): `pnpm --filter overlapmark-site dev`. ## Quick start (Vue) diff --git a/examples/vanilla/tsconfig.json b/examples/vanilla/tsconfig.json deleted file mode 100644 index de3c6fd..0000000 --- a/examples/vanilla/tsconfig.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "extends": [ - "../../tsconfig.base.json", - "../../tsconfig.workspace-paths.json" - ], - "compilerOptions": { - "types": ["vite/client"] - }, - "include": ["*.ts"] -} diff --git a/examples/vanilla/vite.config.ts b/examples/vanilla/vite.config.ts deleted file mode 100644 index c1aa6fd..0000000 --- a/examples/vanilla/vite.config.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { defineConfig } from 'vite'; -import { workspaceAliases } from '../../vitest.workspace-aliases.ts'; - -// The workspace packages resolve to their sources, so the demo runs without -// building them first. The Pages build serves it from a subpath with -// `vite build --base /overlapmark/` (see .github/workflows/pages.yml); every -// other build keeps the default base. -export default defineConfig({ - resolve: { alias: workspaceAliases }, -}); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b72d67f..45010e4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -78,25 +78,6 @@ importers: specifier: ^5.0.5 version: 5.0.5(@types/node@22.20.5)(@vitest/browser-playwright@5.0.5)(@vitest/ui@5.0.5)(vite@8.3.4(@types/node@22.20.5)(esbuild@0.28.2)(yaml@2.9.1)) - examples/vanilla: - dependencies: - '@overlapmark/core': - specifier: workspace:* - version: link:../../packages/core - '@overlapmark/dom': - specifier: workspace:* - version: link:../../packages/dom - devDependencies: - '@fontsource/instrument-sans': - specifier: ^5.3.0 - version: 5.3.0 - '@fontsource/literata': - specifier: ^5.3.0 - version: 5.3.0 - vite: - specifier: ^8 - version: 8.3.4(@types/node@22.20.5)(esbuild@0.28.2)(yaml@2.9.1) - packages/adapter-contract: dependencies: vitest: @@ -188,6 +169,25 @@ importers: specifier: ^3.3.13 version: 3.3.13(typescript@6.0.3) + site: + dependencies: + '@overlapmark/core': + specifier: workspace:* + version: link:../packages/core + '@overlapmark/dom': + specifier: workspace:* + version: link:../packages/dom + devDependencies: + '@fontsource/instrument-sans': + specifier: ^5.3.0 + version: 5.3.0 + '@fontsource/literata': + specifier: ^5.3.0 + version: 5.3.0 + vite: + specifier: ^8 + version: 8.3.4(@types/node@22.20.5)(esbuild@0.28.2)(yaml@2.9.1) + packages: '@andrewbranch/untar.js@1.0.4': diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index f65dc03..5287661 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -1,3 +1,3 @@ packages: - 'packages/*' - - 'examples/*' + - 'site' diff --git a/scripts/render-doc-images.ts b/scripts/render-doc-images.ts index b5869e8..7ef0564 100644 --- a/scripts/render-doc-images.ts +++ b/scripts/render-doc-images.ts @@ -1,5 +1,5 @@ -// Renders the README images into docs-assets/ from the built demo -// (examples/vanilla): one per overlap mode and a hero of the inspector. +// Renders the README images into docs-assets/ from the built site +// (site/): one per overlap mode and a hero of the inspector. // Run with `pnpm docs:images`. The pages are captured in the light theme at // twice the pixel density. The mode images are white cards on a transparent // page and the hero carries the page's own background, so both read on @@ -14,12 +14,12 @@ import { preview } from 'vite'; import { toPalettePng } from './palette-png.ts'; const root = resolve(dirname(fileURLToPath(import.meta.url)), '..'); -const demo = join(root, 'examples/vanilla'); +const demo = join(root, 'site'); const out = join(root, 'docs-assets'); const MODES = ['multiply', 'darken', 'none'] as const; -execFileSync('pnpm', ['--filter', 'vanilla-example', 'build'], { +execFileSync('pnpm', ['--filter', 'overlapmark-site', 'build'], { cwd: root, stdio: 'inherit', }); diff --git a/site/docs/index.html b/site/docs/index.html new file mode 100644 index 0000000..3df97a5 --- /dev/null +++ b/site/docs/index.html @@ -0,0 +1,12 @@ + + + + + + overlapmark docs + + +

overlapmark docs

+

Demo

+ + diff --git a/site/e2e/pages.spec.ts b/site/e2e/pages.spec.ts new file mode 100644 index 0000000..28539d4 --- /dev/null +++ b/site/e2e/pages.spec.ts @@ -0,0 +1,47 @@ +import { expect, test } from '@playwright/test'; + +const PAGES = ['./', './docs/']; + +for (const path of PAGES) { + test(`${path} loads under /overlapmark/ without console errors`, async ({ + page, + }) => { + const errors: string[] = []; + page.on('console', (m) => { + if (m.type() === 'error') errors.push(m.text()); + }); + page.on('pageerror', (e) => errors.push(e.message)); + const response = await page.goto(path); + expect(response?.status()).toBe(200); + await page.waitForLoadState('networkidle'); + expect(errors).toEqual([]); + }); + + test(`every same-origin link and asset on ${path} resolves`, async ({ + page, + request, + }) => { + await page.goto(path); + const urls = await page.evaluate(() => { + const out = new Set(); + const attr = (sel: string, name: string) => + document.querySelectorAll(sel).forEach((el) => { + const value = el.getAttribute(name); + if (!value) return; + const url = new URL(value, location.href); + url.hash = ''; + if (url.origin === location.origin) out.add(url.href); + }); + attr('a[href]', 'href'); + attr('link[href]', 'href'); + attr('script[src]', 'src'); + attr('img[src]', 'src'); + return [...out]; + }); + expect(urls.length).toBeGreaterThan(0); + const statuses = await Promise.all( + urls.map(async (url) => [url, (await request.get(url)).status()]), + ); + expect(statuses.filter(([, status]) => status !== 200)).toEqual([]); + }); +} diff --git a/examples/vanilla/index.html b/site/index.html similarity index 100% rename from examples/vanilla/index.html rename to site/index.html diff --git a/examples/vanilla/main.ts b/site/main.ts similarity index 100% rename from examples/vanilla/main.ts rename to site/main.ts diff --git a/examples/vanilla/package.json b/site/package.json similarity index 74% rename from examples/vanilla/package.json rename to site/package.json index db1037c..1205cb0 100644 --- a/examples/vanilla/package.json +++ b/site/package.json @@ -1,5 +1,5 @@ { - "name": "vanilla-example", + "name": "overlapmark-site", "private": true, "version": "0.0.0", "type": "module", @@ -7,7 +7,9 @@ "dev": "vite", "build": "vite build", "preview": "vite preview", - "typecheck": "tsc --noEmit" + "typecheck": "tsc --noEmit", + "test": "vitest run --passWithNoTests", + "test:e2e": "playwright test" }, "dependencies": { "@overlapmark/core": "workspace:*", diff --git a/examples/vanilla/passage.ts b/site/passage.ts similarity index 100% rename from examples/vanilla/passage.ts rename to site/passage.ts diff --git a/site/playwright.config.ts b/site/playwright.config.ts new file mode 100644 index 0000000..8252b9d --- /dev/null +++ b/site/playwright.config.ts @@ -0,0 +1,18 @@ +import { defineConfig, devices } from '@playwright/test'; + +// The site as Pages serves it: a production build under /overlapmark/. +export default defineConfig({ + testDir: 'e2e', + retries: 0, + forbidOnly: !!process.env.CI, + reporter: process.env.CI ? 'list' : 'line', + use: { baseURL: 'http://localhost:4179/overlapmark/' }, + projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }], + webServer: { + command: + 'pnpm exec vite build --base /overlapmark/ && pnpm exec vite preview --base /overlapmark/ --port 4179 --strictPort', + url: 'http://localhost:4179/overlapmark/', + reuseExistingServer: !process.env.CI, + timeout: 120_000, + }, +}); diff --git a/examples/vanilla/snippets.ts b/site/snippets.ts similarity index 100% rename from examples/vanilla/snippets.ts rename to site/snippets.ts diff --git a/examples/vanilla/style.css b/site/style.css similarity index 100% rename from examples/vanilla/style.css rename to site/style.css diff --git a/site/tsconfig.json b/site/tsconfig.json new file mode 100644 index 0000000..04f5447 --- /dev/null +++ b/site/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": ["../tsconfig.base.json", "../tsconfig.workspace-paths.json"], + "compilerOptions": { + "types": ["vite/client"] + }, + "include": ["*.ts", "src/**/*.ts", "e2e/**/*.ts"] +} diff --git a/site/vite.config.ts b/site/vite.config.ts new file mode 100644 index 0000000..6e5a891 --- /dev/null +++ b/site/vite.config.ts @@ -0,0 +1,20 @@ +import { resolve } from 'node:path'; +import { defineConfig } from 'vite'; +import { workspaceAliases } from '../vitest.workspace-aliases.ts'; + +// Two pages: the demo (index.html) and the docs (docs/index.html). The +// workspace packages resolve to their sources, so the site runs without +// building them first. The Pages build serves it from a subpath with +// `vite build --base /overlapmark/` (see .github/workflows/pages.yml); every +// other build keeps the default base. +export default defineConfig({ + resolve: { alias: workspaceAliases }, + build: { + rolldownOptions: { + input: { + main: resolve(import.meta.dirname, 'index.html'), + docs: resolve(import.meta.dirname, 'docs/index.html'), + }, + }, + }, +}); diff --git a/site/vitest.config.ts b/site/vitest.config.ts new file mode 100644 index 0000000..38cd1e4 --- /dev/null +++ b/site/vitest.config.ts @@ -0,0 +1,14 @@ +import { defineConfig } from 'vitest/config'; +import { workspaceAliases } from '../vitest.workspace-aliases.ts'; + +// Unit tests of the site's data modules. The pages themselves are tested by +// Playwright in e2e/ against the production build. +export default defineConfig({ + resolve: { alias: workspaceAliases }, + test: { + name: 'site', + root: import.meta.dirname, + include: ['src/**/*.test.ts'], + environment: 'node', + }, +}); diff --git a/vitest.workspace-aliases.ts b/vitest.workspace-aliases.ts index 3a62127..6b6a5bc 100644 --- a/vitest.workspace-aliases.ts +++ b/vitest.workspace-aliases.ts @@ -1,5 +1,5 @@ // Vite/Vitest aliases resolving every workspace package to its sources, the -// counterpart of tsconfig.workspace-paths.json. Tests and the examples run +// counterpart of tsconfig.workspace-paths.json. Tests and the site run // without building first, and every importer shares one module instance: // @overlapmark/adapter-contract imports @overlapmark/core without declaring // it (core's own tests run the suite, so a declared dependency would be a From b3de2c21d0b9c77a97781d33a00741fa7e5e9d47 Mon Sep 17 00:00:00 2001 From: Donghyun Lee Date: Fri, 23 Oct 2026 12:58:12 +0900 Subject: [PATCH 2/6] feat(site): shared samples, palettes and quick starts Co-Authored-By: Claude Opus 5.5 --- acceptance/scripts/check-readme.ts | 13 +++- site/src/palette.test.ts | 29 +++++++ site/src/palette.ts | 44 +++++++++++ site/src/quick-starts.ts | 84 ++++++++++++++++++++ site/src/samples.test.ts | 56 ++++++++++++++ site/src/samples.ts | 119 +++++++++++++++++++++++++++++ site/src/words.test.ts | 38 +++++++++ site/src/words.ts | 30 ++++++++ 8 files changed, 411 insertions(+), 2 deletions(-) create mode 100644 site/src/palette.test.ts create mode 100644 site/src/palette.ts create mode 100644 site/src/quick-starts.ts create mode 100644 site/src/samples.test.ts create mode 100644 site/src/samples.ts create mode 100644 site/src/words.test.ts create mode 100644 site/src/words.ts diff --git a/acceptance/scripts/check-readme.ts b/acceptance/scripts/check-readme.ts index a7e1d68..76a9ab1 100644 --- a/acceptance/scripts/check-readme.ts +++ b/acceptance/scripts/check-readme.ts @@ -1,12 +1,13 @@ import { existsSync, readFileSync } from 'node:fs'; import { dirname, join, relative, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; +import { QUICK_STARTS } from '../../site/src/quick-starts.ts'; const root = resolve(dirname(fileURLToPath(import.meta.url)), '../..'); interface Framework { /** As in the root README heading `## Quick start ()`. */ - heading: string; + heading: keyof typeof QUICK_STARTS; pkg: string; /** The acceptance app's quick-start file. */ file: string; @@ -72,6 +73,12 @@ for (const { heading, pkg, file } of frameworks) { `${pkgReadme}: quick start differs from README.md (${heading})`, ); + const site = QUICK_STARTS[heading]; + if (normalized(site.code).join('\n') !== lines.join('\n')) + problems.push( + `site/src/quick-starts.ts: quick start differs from README.md (${heading})`, + ); + const app = join(root, 'acceptance/apps', file); if (!existsSync(app)) { problems.push(`${relative(root, app)} does not exist (${heading})`); @@ -87,4 +94,6 @@ if (problems.length > 0) { console.error(problems.join('\n')); process.exit(1); } -console.log('README quick starts match the package READMEs and the apps'); +console.log( + 'README quick starts match the package READMEs, the site and the apps', +); diff --git a/site/src/palette.test.ts b/site/src/palette.test.ts new file mode 100644 index 0000000..5d28742 --- /dev/null +++ b/site/src/palette.test.ts @@ -0,0 +1,29 @@ +import { describe, expect, test } from 'vitest'; +import { hue, hueSpread } from './palette'; + +describe('hue', () => { + test('reads the hue of #rrggbb colors in degrees', () => { + expect(hue('#ff0000')).toBe(0); + expect(hue('#00ff00')).toBe(120); + expect(hue('#0000ff')).toBe(240); + }); + + test('rejects anything but #rrggbb', () => { + expect(() => hue('red')).toThrow(); + }); +}); + +describe('hueSpread', () => { + test('measures the smallest arc across 0 degrees', () => { + expect(hueSpread(['#ff0000', '#ff00ff'])).toBe(60); + }); + + test('measures opposite hues as half the wheel', () => { + expect(hueSpread(['#ffff00', '#0000ff'])).toBe(180); + }); + + test('is 0 for fewer than two colors', () => { + expect(hueSpread(['#ff0000'])).toBe(0); + expect(hueSpread([])).toBe(0); + }); +}); diff --git a/site/src/palette.ts b/site/src/palette.ts new file mode 100644 index 0000000..a9532d5 --- /dev/null +++ b/site/src/palette.ts @@ -0,0 +1,44 @@ +// The site's highlight colors. Under `overlap: 'multiply'`, hues from +// opposite sides of the wheel multiply into a muddy gray-brown, so every +// sample only stacks colors from one of these families; samples.test.ts +// holds every sample to a 75 degree spread at each character. + +export const PALETTES = { + /** Yellow through pink, about 64 degrees end to end. */ + warm: ['#ffd84d', '#ffbe3d', '#ffa45c', '#ff8a73', '#ff9cb8'], + /** Cyan through periwinkle, about 53 degrees. */ + cool: ['#7fd6e6', '#8fd0ff', '#8cb4ff', '#a9a4ff'], + /** One amber hue in six steps, for the layers slider. */ + mono: ['#ffe27a', '#ffd447', '#ffc53d', '#ffb547', '#ffa64d', '#ff9752'], +} as const satisfies Record; + +/** The hue of a `#rrggbb` color, in degrees in [0, 360). */ +export function hue(hex: string): number { + const match = /^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i.exec(hex); + if (!match) throw new Error(`not a #rrggbb color: ${hex}`); + const [r, g, b] = match.slice(1).map((c) => parseInt(c, 16) / 255) as [ + number, + number, + number, + ]; + const max = Math.max(r, g, b); + const delta = max - Math.min(r, g, b); + if (delta === 0) return 0; + const sector = + max === r + ? (g - b) / delta + : max === g + ? (b - r) / delta + 2 + : (r - g) / delta + 4; + return (sector * 60 + 360) % 360; +} + +/** The smallest arc of the hue wheel, in degrees, that holds every color. */ +export function hueSpread(hexes: readonly string[]): number { + if (hexes.length < 2) return 0; + const hues = hexes.map(hue).sort((a, b) => a - b); + let widestGap = 360 - hues.at(-1)! + hues[0]!; + for (let i = 1; i < hues.length; i++) + widestGap = Math.max(widestGap, hues[i]! - hues[i - 1]!); + return 360 - widestGap; +} diff --git a/site/src/quick-starts.ts b/site/src/quick-starts.ts new file mode 100644 index 0000000..31e5ebe --- /dev/null +++ b/site/src/quick-starts.ts @@ -0,0 +1,84 @@ +// The four framework quick starts, shown on both pages. Each `code` is the +// first code block under `## Quick start ()` in the root README; +// `pnpm check:readme` fails when they drift apart. + +export const QUICK_STARTS = { + 'vanilla JS': { + lang: 'ts', + code: `import '@overlapmark/core/styles.css'; +import { createHighlighter } from '@overlapmark/dom'; + +const text = 'Overlapping highlights get darker where they overlap.'; +let highlights = [{ id: 'a', start: 0, end: 20 }]; + +const hl = createHighlighter(document.getElementById('text')!, { + text, + highlights, +}); + +hl.on('select', ({ start, end }) => { + highlights = [...highlights, { id: crypto.randomUUID(), start, end }]; + hl.update({ highlights }); +}); +hl.on('click', ({ highlights: covering }) => + console.log( + 'covered by', + covering.map((h) => h.id), + ), +); +`, + }, + Vue: { + lang: 'vue', + code: ` + + +`, + }, + React: { + lang: 'tsx', + code: `import '@overlapmark/core/styles.css'; +import { useState } from 'react'; +import type { Highlight, SelectDetail } from '@overlapmark/core'; +import { Highlighter } from '@overlapmark/react'; + +export function Article({ text }: { text: string }) { + const [highlights, setHighlights] = useState([]); + const add = ({ start, end }: SelectDetail) => + setHighlights((list) => [ + ...list, + { id: crypto.randomUUID(), start, end }, + ]); + return ; +} +`, + }, + Svelte: { + lang: 'svelte', + code: ` + + +`, + }, +} as const satisfies Record; diff --git a/site/src/samples.test.ts b/site/src/samples.test.ts new file mode 100644 index 0000000..427cfbe --- /dev/null +++ b/site/src/samples.test.ts @@ -0,0 +1,56 @@ +import { computeSegments } from '@overlapmark/core'; +import { describe, expect, test } from 'vitest'; +import { hueSpread } from './palette'; +import { QUICK_STARTS } from './quick-starts'; +import { SAMPLES, type Sample } from './samples'; + +const named: [string, Sample][] = [ + ['hero', SAMPLES.hero], + ['passage', SAMPLES.passage], + ['styling', SAMPLES.styling], + ['modes', SAMPLES.modes], + ...[1, 2, 3, 4, 5, 6].map((n): [string, Sample] => [ + `layers(${n})`, + SAMPLES.layers(n), + ]), +]; + +describe.each(named)('%s', (_, sample) => { + test('stacks no colors more than 75 degrees of hue apart', () => { + for (const segment of computeSegments(sample.text, sample.highlights)) { + const colors = segment.highlights.map((h) => h.color!); + expect(hueSpread(colors)).toBeLessThanOrEqual(75); + } + }); + + test('gives every highlight a color, a label and a unique id', () => { + const ids = sample.highlights.map((h) => h.id); + expect(new Set(ids).size).toBe(ids.length); + for (const h of sample.highlights) { + expect(h.color).toMatch(/^#[0-9a-f]{6}$/i); + expect(h.data?.label).toBeTruthy(); + } + }); +}); + +test('the hue check rejects blue over orange', () => { + expect(hueSpread(['#8cbcff', '#ffb066'])).toBeGreaterThan(75); +}); + +test.each([1, 2, 3, 4, 5, 6])('layers(%i) nests that many highlights', (n) => { + const sample = SAMPLES.layers(n); + expect(sample.highlights).toHaveLength(n); + const depths = computeSegments(sample.text, sample.highlights).map( + (s) => s.highlights.length, + ); + expect(Math.max(...depths)).toBe(n); +}); + +test('QUICK_STARTS has one entry per README quick start', () => { + expect(Object.keys(QUICK_STARTS)).toEqual([ + 'vanilla JS', + 'Vue', + 'React', + 'Svelte', + ]); +}); diff --git a/site/src/samples.ts b/site/src/samples.ts new file mode 100644 index 0000000..d1f5441 --- /dev/null +++ b/site/src/samples.ts @@ -0,0 +1,119 @@ +// The texts and highlights behind every live example on the two pages. +import type { Highlight } from '@overlapmark/core'; +import { PALETTES } from './palette'; + +export interface Label { + label: string; +} + +export interface Sample { + text: string; + highlights: Highlight