Skip to content
Merged
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
48 changes: 48 additions & 0 deletions packages/docs/src/app/docs/_doc-overlays.scss
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,51 @@
transform: scale(0.92) translateY(8px);
}
}

/* A small confirmation that rises from the bottom, e.g. after copying a link. */
.toast {
position: fixed;
z-index: 40;
left: 50%;
bottom: var(--space-6);
display: inline-flex;
align-items: center;
gap: var(--space-2);
padding: var(--space-2) var(--space-4);
border-radius: var(--radius-full);
background: var(--color-neutral-900);
color: var(--color-neutral-50);
box-shadow: var(--shadow-lg);
font-size: 0.875rem;
line-height: 1.25rem;
font-weight: 500;
opacity: 0;
translate: -50% 12px;
pointer-events: none;
transition:
opacity var(--duration-base) ease-out,
translate var(--duration-base) var(--ease-out);

svg {
width: 1rem;
height: 1rem;
color: var(--color-success-400);
}
}

.toast-visible {
opacity: 1;
translate: -50% 0;
}

:root[data-theme='dark'] .toast {
background: var(--color-neutral-50);
color: var(--color-neutral-900);
}

@media (prefers-color-scheme: dark) {
:root:not([data-theme='light']) .toast {
background: var(--color-neutral-50);
color: var(--color-neutral-900);
}
}
15 changes: 15 additions & 0 deletions packages/docs/src/app/docs/doc-page.html
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,21 @@ <h1 class="doc-title enter" style="--enter: 1" tabindex="-1">{{ doc().page.title

<div class="read-progress" aria-hidden="true"></div>

<div class="toast" [class.toast-visible]="toast()" role="status" aria-live="polite">
<svg
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2.5"
stroke-linecap="round"
stroke-linejoin="round"
aria-hidden="true"
>
<path d="m5 12.5 4.5 4.5L19 7.5" />
</svg>
{{ toast() }}
</div>

<button
class="back-to-top"
type="button"
Expand Down
35 changes: 34 additions & 1 deletion packages/docs/src/app/docs/doc-page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ export class DocPage {
return toc.find((entry) => entry.id === this.activeId()) ?? toc[0];
});

/** Short confirmation shown at the bottom of the screen, e.g. "Link copied". */
readonly toast = signal<string | null>(null);
private toastTimer?: ReturnType<typeof setTimeout>;

/** The back-to-top button shows once the reader is a screen down. */
readonly showBackToTop = signal(false);

Expand Down Expand Up @@ -136,6 +140,9 @@ export class DocPage {
if (hash?.startsWith('#')) {
event.preventDefault();
this.goToHeading(hash.slice(1));
if (anchor?.classList.contains('heading-anchor')) {
this.copyLink(hash.slice(1));
}
return;
}
const internal = anchor?.dataset['internal'];
Expand Down Expand Up @@ -344,6 +351,25 @@ export class DocPage {
dialog.showModal();
}

/** Copies the full URL of a section, for sharing. */
private copyLink(id: string) {
const win = this.document.defaultView;
if (!win) {
return;
}
const url = `${win.location.origin}${win.location.pathname}#${id}`;
navigator.clipboard?.writeText(url).then(
() => this.showToast('Link copied'),
() => undefined,
);
}

private showToast(message: string) {
clearTimeout(this.toastTimer);
this.toast.set(message);
this.toastTimer = setTimeout(() => this.toast.set(null), 1800);
}

private copyCode(button: HTMLButtonElement) {
const block = button.closest('.code-block');
const code =
Expand All @@ -361,7 +387,14 @@ export class DocPage {
.join('\n')
.replace(/\n{3,}/g, '\n\n')
.trim()
: (code.textContent ?? '');
: code.classList.contains('language-diff')
? // A diff copies the result: removed lines go, markers are stripped.
(code.textContent ?? '')
.split('\n')
.filter((line) => !line.startsWith('-'))
.map((line) => line.slice(1))
.join('\n')
: (code.textContent ?? '');
const markCopied = () => {
button.classList.add('copied');
button.setAttribute('aria-label', 'Copied');
Expand Down
83 changes: 80 additions & 3 deletions packages/docs/src/app/docs/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import json from 'highlight.js/lib/languages/json';
import yaml from 'highlight.js/lib/languages/yaml';
import bash from 'highlight.js/lib/languages/bash';
import plaintext from 'highlight.js/lib/languages/plaintext';
import diff from 'highlight.js/lib/languages/diff';

hljs.registerLanguage('typescript', typescript);
hljs.registerLanguage('ts', typescript);
Expand All @@ -19,6 +20,7 @@ hljs.registerLanguage('bash', bash);
hljs.registerLanguage('sh', bash);
hljs.registerLanguage('shell', bash);
hljs.registerLanguage('text', plaintext);
hljs.registerLanguage('diff', diff);

export interface TocEntry {
id: string;
Expand Down Expand Up @@ -49,6 +51,7 @@ const LANGUAGE_LABELS: Record<string, string> = {
bash: 'Terminal',
sh: 'Terminal',
shell: 'Terminal',
diff: 'Diff',
};

const COPY_BUTTON =
Expand All @@ -69,6 +72,61 @@ const CALLOUTS: Record<string, string> = {
IMPORTANT: 'Important',
};

const calloutIcon = (paths: string) =>
`<svg class="callout-icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">${paths}</svg>`;

/** Icons for callouts: a light bulb, an info circle, a flag and a warning triangle. */
const CALLOUT_ICONS: Record<string, string> = {
TIP: calloutIcon(
'<path d="M9 18h6M10 22h4"/><path d="M12 2a7 7 0 0 0-4 12.74V16h8v-1.26A7 7 0 0 0 12 2z"/>',
),
NOTE: calloutIcon(
'<circle cx="12" cy="12" r="10"/><path d="M12 16v-4M12 8h.01"/>',
),
IMPORTANT: calloutIcon(
'<path d="M4 22V4a1 1 0 0 1 1-1h13l-2.5 5L18 13H5"/>',
),
WARNING: calloutIcon(
'<path d="M10.29 3.86 1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z"/><path d="M12 9v4M12 17h.01"/>',
),
};

const LINK_ICON =
'<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"/><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"/></svg>';

/** Line numbers from a `{1,3-4}` fence attribute. */
function lineRanges(meta: string): Set<number> {
const lines = new Set<number>();
const spec = meta.match(/\{([\d,\s-]+)\}/)?.[1];
for (const part of spec?.split(',') ?? []) {
const [from, to = from] = part.trim().split('-').map(Number);
for (let n = from; n <= to; n++) {
lines.add(n);
}
}
return lines;
}

/**
* Splits highlighted HTML into lines. Highlighting can open a span on one
* line and close it on a later one, so open spans are closed at each line
* end and reopened on the next line.
*/
function splitLines(html: string): string[] {
const open: string[] = [];
return html.split('\n').map((line) => {
const prefix = open.join('');
for (const tag of line.match(/<span[^>]*>|<\/span>/g) ?? []) {
if (tag === '</span>') {
open.pop();
} else {
open.push(tag);
}
}
return prefix + line + '</span>'.repeat(open.length);
});
}

/** Consecutive fenced blocks with a `tab="…"` attribute, shown as one tabbed block. */
interface CodeTabsToken {
type: 'codeTabs';
Expand Down Expand Up @@ -213,11 +271,30 @@ function codeBody({ text, lang }: Tokens.Code) {
const title = meta.join(' ').match(/title="?([^"]+)"?/)?.[1];
const shell = SHELL_LANGUAGES.has(language);
const known = language && hljs.getLanguage(language);
const body = shell
const highlighted = lineRanges(meta.join(' '));
let body = shell
? renderShell(text)
: known
? hljs.highlight(text, { language }).value
: escapeHtml(text);
// Highlighted lines and diffs wrap each line, so it can be marked.
if (!shell && (highlighted.size || language === 'diff')) {
const raw = text.split('\n');
body = splitLines(body)
.map((line, i) => {
const classes = ['code-line'];
if (highlighted.has(i + 1)) {
classes.push('is-highlighted');
}
if (language === 'diff' && (raw[i] ?? '').startsWith('+')) {
classes.push('is-added');
} else if (language === 'diff' && (raw[i] ?? '').startsWith('-')) {
classes.push('is-removed');
}
return `<span class="${classes.join(' ')}">${line}</span>`;
})
.join('\n');
}
return {
title,
shell,
Expand Down Expand Up @@ -375,7 +452,7 @@ export function renderMarkdown(
: inner;
const anchor =
depth > 1
? `<a class="heading-anchor" href="#${id}" aria-label="Link to this section">#</a>`
? `<a class="heading-anchor" href="#${id}" aria-label="Copy link to this section" title="Copy link">${LINK_ICON}</a>`
: '';
return `<h${depth} id="${id}"${step ? ' class="step-heading"' : ''}>${label}${anchor}</h${depth}>\n`;
},
Expand Down Expand Up @@ -414,7 +491,7 @@ export function renderMarkdown(
const kind = match[1];
const rest = inner.slice(match[0].length);
const body = rest.startsWith('</p>') ? rest.slice(4) : `<p>${rest}`;
return `<aside class="callout callout-${kind.toLowerCase()}"><p class="callout-title">${CALLOUTS[kind]}</p>${body}</aside>\n`;
return `<aside class="callout callout-${kind.toLowerCase()}"><p class="callout-title">${CALLOUT_ICONS[kind]}${CALLOUTS[kind]}</p>${body}</aside>\n`;
},
table({ header, rows }: Tokens.Table) {
const labels = header.map((c) =>
Expand Down
16 changes: 16 additions & 0 deletions packages/docs/src/content/cli/init.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ Point `--rcPath` at the file you want to change and pass only the values that sh
npx user-flow init --rcPath .user-flowrc.ci.json --outPath tmp/measures
```

Only the value you passed changes:

```diff title=".user-flowrc.ci.json"
{
"collect": {
"url": "https://coffee-cart.netlify.app/",
"ufPath": "./user-flows"
},
"persist": {
- "outPath": "./measures",
+ "outPath": "tmp/measures",
"format": ["html"]
}
}
```

> [!TIP]
> Combine `init` with `--verbose` to print the resolved rc file without changing anything:
> `npx user-flow init --rcPath .user-flowrc.ci.json --verbose`
Expand Down
4 changes: 2 additions & 2 deletions packages/docs/src/content/user-flows/recorder-exports.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ One timespan over everything is a coarse measurement. To see each interaction on
| `startTimespan` / `endTimespan` | `flow.startTimespan()` / `flow.endTimespan()` |
| `snapshot` | `flow.snapshot()` |

```json title="recordings/order-coffee.replay.json"
```json {4,11,12} title="recordings/order-coffee.replay.json"
{
"title": "Order coffee",
"steps": [
Expand Down Expand Up @@ -102,7 +102,7 @@ const interactions: UserFlowInteractionsFn = async (ctx: UserFlowContext): Promi

Measurement steps accept the same options as their Lighthouse counterparts through `stepOptions`. A `name` makes the report readable:

```json title="recordings/order-coffee.replay.json"
```json {5,16} title="recordings/order-coffee.replay.json"
{
"steps": [
{
Expand Down
Loading
Loading