Skip to content

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.

test: read README sections with a test helper instead of handbook.js

matin11fd6eecfee7
diff --git a/tests/helpers/markdown.js b/tests/helpers/markdown.js
new file mode 100644
index 00000000..a380336c
--- /dev/null
+++ b/tests/helpers/markdown.js
@@ -0,0 +1,65 @@
+/* Markdown parsing helpers for tests. Extracted from assets/js/handbook.js. */
+'use strict';
+
+function escapeHtml(s) {
+ return String(s).replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
+}
+
+// Text with Markdown code and bold markers removed, safe to insert as HTML.
+function plain(s) {
+ return escapeHtml(String(s).replace(/`/g, '').replace(/\*\*/g, ''));
+}
+
+// Allow only relative paths, "#" anchors and http(s) URLs as link targets; anything else
+// (e.g. a javascript: URL) is rejected and rendered as plain link text instead.
+function isSafeHref(href) {
+ return /^(?:https?:|#|\.{0,2}\/|[\w.-]+(?:\/|$))/i.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;
+}
+
+// "Duration (`--dur`)" becomes "Duration": parentheticals that hold code are dropped.
+function cleanName(s) {
+ return plain(String(s).replace(/\s*\(`[^`]*`[^)]*\)/g, ''));
+}
+
+// The Key parameters table as [{ name, value, effect }], without its header and divider rows.
+function table(text) {
+ var rows = String(text || '').split('\n')
+ .filter(function (l) { return /^\s*\|/.test(l); })
+ .map(function (l) {
+ return l.trim().replace(/^\|/, '').replace(/\|$/, '').split('|').map(function (c) { return c.trim(); });
+ });
+ return rows.slice(1)
+ .filter(function (r) { return !r.every(function (c) { return /^:?-{2,}:?$/.test(c); }); })
+ .map(function (r) { return { name: cleanName(r[0] || ''), value: plain(r[1] || ''), effect: inline(r[2] || '') }; });
+}
+
+module.exports = { sections, table };
diff --git a/tests/pages.test.js b/tests/pages.test.js
index 39624481..b0c575cf 100644
--- a/tests/pages.test.js
+++ b/tests/pages.test.js
@@ -4,7 +4,7 @@ const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
-const { sections, table } = require('../assets/js/handbook.js');
+const { sections, table } = require('./helpers/markdown.js');
const ROOT = path.resolve(__dirname, '..');
const ANIM = path.join(ROOT, 'animations');