176 web animation techniques, each a live demo with plain settings and a ready-made prompt for your AI assistant. No dependencies, no build step.
HTML ai-promptsanimationcss-animationsfrontendgithub-pagesjavascriptmicrointeractionsmotion-designno-dependenciesreferencescroll-animationui-animationweb-animationweb-developmentwebgl
feat: add the helpers for the guided-steps demo page
matin574b80ad4ef4
diff --git a/assets/js/demo-page.js b/assets/js/demo-page.jsnew file mode 100644index 00000000..a50ba35f--- /dev/null+++ b/assets/js/demo-page.js@@ -0,0 +1,92 @@+/* Animation Handbook — shared behaviour for the guided-steps demo pages.+ * The pure helpers are exported for tests/demo-page.test.js. */+(function (root, factory) {+ var api = factory();+ if (typeof module === 'object' && module.exports) { module.exports = api; return; }+ root.DemoPage = api;+})(typeof self !== 'undefined' ? self : this, function () {+ 'use strict';++ /* ---------- Pure helpers ---------- */++ function escapeHtml(s) {+ return String(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');+ }++ // Text with Markdown code and bold markers removed, safe to insert as HTML.+ function plain(s) {+ return escapeHtml(String(s).replace(/`/g, '').replace(/\*\*/g, ''));+ }++ // Only relative paths, "#" anchors and http(s) URLs are link targets. Anything else, including+ // protocol-relative "//host" links, is rendered as plain text instead.+ function isSafeHref(href) {+ href = String(href);+ return /^(?:https?:|#|\.{0,2}\/|[\w.-]+(?:\/|$))/i.test(href) && !/^\/\//.test(href) &&+ !/^[a-z][a-z0-9+.-]*:/i.test(href.replace(/^https?:/i, ''));+ }++ // Inline Markdown from README prose. Inline code becomes plain text: the site shows no code.+ function inline(md) {+ return escapeHtml(md)+ .replace(/`([^`]+)`/g, '$1')+ .replace(/\[([^\]]+)\]\(([^)\s]+)\)/g, function (m, label, href) {+ return isSafeHref(href) ? '<a href="' + href + '">' + label + '</a>' : label;+ })+ .replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>')+ .replace(/\*([^*\n]+)\*/g, '<em>$1</em>');+ }++ // A README split into { "What it is": "...", ... } by its "## " headings.+ function sections(md) {+ var out = {}, current = null, buf = [];+ String(md).replace(/\r\n?/g, '\n').split('\n').forEach(function (line) {+ var m = /^##\s+(.+?)\s*$/.exec(line);+ if (m) {+ if (current) out[current] = buf.join('\n').trim();+ current = m[1];+ buf = [];+ } else if (current) {+ buf.push(line);+ }+ });+ if (current) out[current] = buf.join('\n').trim();+ return out;+ }++ function paragraphs(text) {+ return String(text || '').split(/\n\s*\n/)+ .map(function (p) { return p.trim(); })+ .filter(Boolean)+ .map(function (p) { return '<p>' + inline(p.replace(/\s*\n\s*/g, ' ')) + '</p>'; })+ .join('');+ }++ // "- [Name](../slug/) — description" lines as [{ name, href, desc }].+ function seeAlso(text) {+ return String(text || '').split('\n').map(function (l) {+ var m = /^\s*[-*]\s+\[([^\]]+)\]\(([^)\s]+)\)\s*(?:[—–-]\s*(.*))?$/.exec(l);+ return m ? { name: plain(m[1]), href: m[2], desc: m[3] ? inline(m[3]) : '' } : null;+ }).filter(Boolean);+ }++ // [{ label: 'Speed', value: 'Normal' }, ...] as "Speed: Normal, ...", skipping incomplete pairs.+ function settingsLine(items) {+ return (items || [])+ .filter(function (i) { return i && i.label && i.value; })+ .map(function (i) { return i.label + ': ' + i.value; })+ .join(', ');+ }++ // The prompt as HTML with each [part to fill in] highlighted.+ function markFill(text) {+ return escapeHtml(text).replace(/\[[^\]\n]+\]/g, function (part) {+ return '<mark class="hb-fill">' + part + '</mark>';+ });+ }++ return {+ escapeHtml: escapeHtml, plain: plain, isSafeHref: isSafeHref, inline: inline, sections: sections,+ paragraphs: paragraphs, seeAlso: seeAlso, settingsLine: settingsLine, markFill: markFill+ };+});diff --git a/tests/demo-page.test.js b/tests/demo-page.test.jsnew file mode 100644index 00000000..2337f283--- /dev/null+++ b/tests/demo-page.test.js@@ -0,0 +1,72 @@+// Unit tests for the pure helpers in assets/js/demo-page.js.+// Run from the repo root: node --test "tests/*.test.js"+const test = require('node:test');+const assert = require('node:assert/strict');+const DP = require('../assets/js/demo-page.js');++test('escapeHtml escapes markup characters', () => {+ assert.equal(DP.escapeHtml('<a href="x">&</a>'), '<a href="x">&</a>');+});++test('plain drops code and bold markers and escapes', () => {+ assert.equal(DP.plain('`ease-out` **fast** <b>'), 'ease-out fast <b>');+});++test('isSafeHref allows relative, anchor and http(s) links only', () => {+ for (const ok of ['../scale-in/', 'README.md', '#top', '/animation-handbook/', 'https://example.com/p', 'http://example.com']) {+ assert.equal(DP.isSafeHref(ok), true, ok);+ }+ for (const bad of ['javascript:alert(1)', 'data:text/html,x', '//evil.example/x', 'mailto:[email protected]']) {+ assert.equal(DP.isSafeHref(bad), false, bad);+ }+});++test('inline shows code as plain text and renders links, bold and italics', () => {+ assert.equal(DP.inline('Use `opacity` on **one** *hero* [card](../scale-in/)'),+ 'Use opacity on <strong>one</strong> <em>hero</em> <a href="../scale-in/">card</a>');+ assert.equal(DP.inline('a < b'), 'a < b');+});++test('inline links only safe targets and keeps parentheses around links', () => {+ assert.ok(!DP.inline('[x](javascript:alert(1))').includes('<a'), 'no link for javascript: URLs');+ assert.ok(!DP.inline('[x](//evil.example/)').includes('<a'), 'no link for protocol-relative URLs');+ assert.equal(DP.inline('[a](https://example.com/p)'), '<a href="https://example.com/p">a</a>');+ assert.equal(DP.inline('(see [Scale In](../scale-in/))'), '(see <a href="../scale-in/">Scale In</a>)');+});++test('sections splits a README by its ## headings', () => {+ const md = '# Title\r\n\r\n## What it is\r\nLine one\r\nline two\r\n\r\n## When to use it\r\n- A\r\n- B\r\n';+ assert.deepEqual(DP.sections(md), { 'What it is': 'Line one\nline two', 'When to use it': '- A\n- B' });+});++test('sections keeps ### lines inside the section they belong to', () => {+ assert.deepEqual(DP.sections('## A\n### not a section\nx'), { A: '### not a section\nx' });+});++test('paragraphs joins wrapped lines and splits on blank lines', () => {+ assert.equal(DP.paragraphs('One\ntwo.\n\nThree `x`.'), '<p>One two.</p><p>Three x.</p>');+ assert.equal(DP.paragraphs(undefined), '');+});++test('seeAlso reads name, link and description', () => {+ const md = '- [Scale In](../scale-in/) — grows from small, with no spin\n- [Blur In](../blur-in/)\nText';+ assert.deepEqual(DP.seeAlso(md), [+ { name: 'Scale In', href: '../scale-in/', desc: 'grows from small, with no spin' },+ { name: 'Blur In', href: '../blur-in/', desc: '' }+ ]);+});++test('settingsLine writes "label: value" pairs and skips incomplete ones', () => {+ assert.equal(DP.settingsLine([+ { label: 'Speed', value: 'Normal' }, { label: 'Fades in', value: 'on' },+ { label: 'Mode', value: '' }, { label: '', value: 'x' }+ ]), 'Speed: Normal, Fades in: on');+ assert.equal(DP.settingsLine([]), '');+ assert.equal(DP.settingsLine(undefined), '');+});++test('markFill highlights the parts in square brackets and escapes the rest', () => {+ assert.equal(DP.markFill('Add it to [the icon you want] & <go>.'),+ 'Add it to <mark class="hb-fill">[the icon you want]</mark> & <go>.');+ assert.equal(DP.markFill('No brackets here.'), 'No brackets here.');+});