@phnx-labs/artifacts-cli 0.4.0 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +95 -0
- package/README.md +75 -14
- package/dist/cli.js +25 -11
- package/dist/cli.js.map +1 -1
- package/dist/commands/new.js +2 -0
- package/dist/commands/new.js.map +1 -1
- package/dist/lib/agent-assets.d.ts +1 -0
- package/dist/lib/agent-assets.js +15 -0
- package/dist/lib/agent-assets.js.map +1 -0
- package/dist/lib/collab.d.ts +61 -0
- package/dist/lib/collab.js +714 -0
- package/dist/lib/collab.js.map +1 -0
- package/dist/lib/creator-cover.d.ts +7 -0
- package/dist/lib/creator-cover.js +68 -0
- package/dist/lib/creator-cover.js.map +1 -0
- package/dist/lib/fonts.d.ts +3 -0
- package/dist/lib/fonts.js +68 -12
- package/dist/lib/fonts.js.map +1 -1
- package/dist/lib/freshness.d.ts +6 -0
- package/dist/lib/freshness.js +52 -0
- package/dist/lib/freshness.js.map +1 -0
- package/dist/lib/highlights.d.ts +11 -1
- package/dist/lib/highlights.js +38 -71
- package/dist/lib/highlights.js.map +1 -1
- package/dist/lib/icons.js +2 -1
- package/dist/lib/icons.js.map +1 -1
- package/dist/lib/reader-anchors.d.ts +16 -0
- package/dist/lib/reader-anchors.js +146 -0
- package/dist/lib/reader-anchors.js.map +1 -0
- package/dist/lib/render.js +337 -38
- package/dist/lib/render.js.map +1 -1
- package/dist/lib/section-collapse.d.ts +17 -0
- package/dist/lib/section-collapse.js +113 -0
- package/dist/lib/section-collapse.js.map +1 -0
- package/dist/lib/templates.d.ts +5 -3
- package/dist/lib/templates.js +19 -4
- package/dist/lib/templates.js.map +1 -1
- package/dist/lib/tokens.d.ts +2 -0
- package/dist/lib/tokens.js +5 -1
- package/dist/lib/tokens.js.map +1 -1
- package/dist/lib/validate.js +28 -1
- package/dist/lib/validate.js.map +1 -1
- package/fonts/Inter-OFL.txt +92 -0
- package/fonts/InterVariable.woff2 +0 -0
- package/fonts/JetBrainsMono-OFL.txt +93 -0
- package/fonts/JetBrainsMono-wght.woff2 +0 -0
- package/fonts/README.md +16 -0
- package/package.json +4 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collab.js","sourceRoot":"","sources":["../../src/lib/collab.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC;;;;;;;;;;;;;;;;;;GAkBG;AAEH,sFAAsF;AACtF,MAAM,CAAC,MAAM,kBAAkB,GAAG,yBAAyB,CAAC;AAE5D;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,YAAY,CAAC,WAAmB;IAC9C,OAAO,UAAU,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,WAAW,EAAE,MAAM,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC;AACpF,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,kBAAkB,CAAC,QAAgB;IACjD,IAAI,IAAI,GAAG,MAAM,CAAC,QAAQ,IAAI,GAAG,CAAC,CAAC;IACnC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC;QAAE,IAAI,GAAG,IAAI,IAAI,EAAE,CAAC;IAC7C,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,GAAG,CAAC,CAAC;IACpC,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,kBAAkB,EAAE,GAAG,CAAC,CAAC;IAC7C,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC;QAAE,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;IACrD,OAAO,IAAI,IAAI,GAAG,CAAC;AACrB,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,eAAe,CAAC,KAAc;IAC5C,MAAM,MAAM,GAA2B;QACrC,EAAE,EAAE,SAAS;QACb,GAAG,EAAE,SA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AAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;6DA4C5B,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;UA4gB7E,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare function creatorId(harness: string): string;
|
|
2
|
+
/** Palette and motif are built in; brand marks remain shared with the favicon. */
|
|
3
|
+
export declare function coverClass(harness: string): string;
|
|
4
|
+
export declare function creatorMark(harness: string): string;
|
|
5
|
+
export declare function coverArtwork(harness: string): string;
|
|
6
|
+
export declare function creatorFavicon(harness: string): string;
|
|
7
|
+
export declare const COVER_CSS = "\n.hero.creator-cover{--cover-bg:#edf0ec;--cover-fg:#23332b;--cover-muted:#566c5e;--cover-accent:#629b77;--cover-glow:#c9dccd;position:relative;isolation:isolate;overflow:hidden;margin-top:24px;padding:32px 36px 26px;border:1px solid color-mix(in srgb,var(--cover-accent) 22%,var(--cover-bg));border-radius:18px;background:radial-gradient(ellipse at 100% 0%,var(--cover-glow),transparent 65%),var(--cover-bg);color:var(--cover-fg)}\n.hero.cover-codex,.hero.cover-cursor{--cover-bg:#141c1b;--cover-fg:#f5f7f4;--cover-muted:#b4c5bf;--cover-accent:#9ddbbb;--cover-glow:#29463d}\n.hero.cover-claude{--cover-bg:#f4e9db;--cover-fg:#3c2c24;--cover-muted:#80634e;--cover-accent:#ba6042;--cover-glow:#efd0b9}\n.hero.cover-gemini,.hero.cover-copilot{--cover-bg:#edf1fc;--cover-fg:#273452;--cover-muted:#58688b;--cover-accent:#617cdf;--cover-glow:#d9d1f4}\n.cover-top{display:flex;flex-wrap:wrap;align-items:center;justify-content:space-between;gap:20px;margin-bottom:34px;position:relative;z-index:1}\n.hero.creator-cover .cover-creator{display:flex;align-items:center;gap:12px;min-width:0;color:var(--cover-fg);font:650 17px/1.3 var(--font-body);overflow-wrap:anywhere}\n.cover-creator .chip-icon{width:36px;height:36px;margin:0;flex:0 0 36px;fill:currentColor}\n.cover-creator img.chip-icon{object-fit:contain;background:#faf9f6;border-radius:7px;padding:3px}\n.cover-project{display:inline-flex;align-items:center;gap:9px;max-width:60%;padding:7px 14px;border:1px solid color-mix(in srgb,var(--cover-accent) 32%,transparent);border-radius:18px;background:color-mix(in srgb,var(--cover-bg) 75%,transparent);color:var(--cover-fg);font:600 13px/1.4 var(--font-body);overflow-wrap:anywhere}\n.cover-project:hover{color:var(--cover-fg);border-color:var(--cover-accent)}\n.cover-project:focus-visible{outline:2px solid var(--cover-accent);outline-offset:3px}\n.hero.creator-cover .kicker{color:var(--cover-muted);font-size:10px;letter-spacing:.09em;position:relative;z-index:1}\n.hero.creator-cover .kicker-part{color:var(--cover-muted)}\n.hero.creator-cover .status-badge{color:var(--cover-fg);border-color:color-mix(in srgb,var(--cover-accent) 32%,transparent);background:color-mix(in srgb,var(--cover-accent) 12%,transparent)}\n.hero.creator-cover h1{position:relative;z-index:1;max-width:850px;margin:18px 0 18px;color:var(--cover-fg);font-size:clamp(30px,4vw,49px);line-height:1.12;font-weight:750;letter-spacing:-.035em;overflow-wrap:anywhere;text-wrap:balance}\n.hero.creator-cover .summary{position:relative;z-index:1;max-width:780px;color:var(--cover-muted);font-size:17px;line-height:1.6}\n.hero.creator-cover .summary a{color:var(--cover-fg)}\n.hero.creator-cover .creator-line{position:relative;z-index:1;display:flex;flex-wrap:wrap;align-items:center;gap:10px 22px;margin:28px 0 0;padding-top:18px;border-top:1px solid color-mix(in srgb,var(--cover-accent) 23%,transparent);color:var(--cover-muted);font:400 12px/1.5 var(--font-body)}\n.hero.creator-cover .creator-time,.hero.creator-cover .creator-human{color:var(--cover-muted)}\n.creator-time[title]{cursor:help}.creator-line .meta-sep{display:none}\n.cover-art{position:absolute;z-index:0;right:-15px;top:54px;width:300px;height:300px;opacity:.1;pointer-events:none;color:var(--cover-accent)}\n.cover-art .chip-icon{position:absolute;inset:45px;width:210px;height:210px;margin:0;fill:currentColor}\n.cover-art span{position:absolute;inset:0;border:1px solid currentColor;border-radius:50%}.cover-art span+span{inset:-45px}\n.hero-metadata{padding:0 2px 22px;border-bottom:1px solid var(--line)}.hero-metadata .meta{gap:12px;margin-top:20px}\n@media(max-width:720px){.hero.creator-cover{margin-top:18px;padding:24px 22px;border-radius:14px}.cover-top{gap:16px;margin-bottom:28px}.cover-project{max-width:100%}.cover-creator{font-size:16px}.cover-art{right:-80px;top:100px;opacity:.055}.hero.creator-cover h1{text-wrap:wrap}.hero.creator-cover .summary{font-size:16px}.hero.creator-cover .creator-line{gap:8px 18px}}\n@media print{.hero.creator-cover{break-inside:avoid;margin-top:0}.cover-art{opacity:.05}.hero-metadata details>p{display:block!important}.hero.creator-cover a{color:var(--cover-fg)}}\n";
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { harnessIconSvg } from './icons.js';
|
|
2
|
+
import { escapeHtml } from './html.js';
|
|
3
|
+
import { AGENT_ASSETS } from './agent-assets.js';
|
|
4
|
+
const ALIASES = {
|
|
5
|
+
'claude-code': 'claude', cc: 'claude', 'openai-codex': 'codex', openai: 'codex', cx: 'codex',
|
|
6
|
+
'gemini-cli': 'gemini', gx: 'gemini', 'cursor-agent': 'cursor', cr: 'cursor',
|
|
7
|
+
'github-copilot': 'copilot', 'copilot-cli': 'copilot', gh: 'copilot',
|
|
8
|
+
factory: 'droid', 'factory-ai': 'droid', 'grok-build': 'grok', 'xai-grok': 'grok',
|
|
9
|
+
'kimi-code': 'kimi', claw: 'openclaw', ocl: 'openclaw', oc: 'opencode', 'hermes-agent': 'hermes',
|
|
10
|
+
};
|
|
11
|
+
export function creatorId(harness) {
|
|
12
|
+
const id = harness.trim().toLowerCase();
|
|
13
|
+
return Object.hasOwn(ALIASES, id) ? ALIASES[id] : id;
|
|
14
|
+
}
|
|
15
|
+
/** Palette and motif are built in; brand marks remain shared with the favicon. */
|
|
16
|
+
export function coverClass(harness) {
|
|
17
|
+
const id = creatorId(harness);
|
|
18
|
+
return ['codex', 'claude', 'gemini', 'cursor', 'copilot'].includes(id) ? id : 'neutral';
|
|
19
|
+
}
|
|
20
|
+
export function creatorMark(harness) {
|
|
21
|
+
const id = creatorId(harness);
|
|
22
|
+
const asset = Object.hasOwn(AGENT_ASSETS, id) ? AGENT_ASSETS[id] : undefined;
|
|
23
|
+
return asset ? `<img class="chip-icon" src="${asset}" alt="" width="36" height="36">` : harnessIconSvg(id);
|
|
24
|
+
}
|
|
25
|
+
export function coverArtwork(harness) {
|
|
26
|
+
return `<div class="cover-art" aria-hidden="true">${creatorMark(harness)}<span></span><span></span></div>`;
|
|
27
|
+
}
|
|
28
|
+
export function creatorFavicon(harness) {
|
|
29
|
+
const id = creatorId(harness);
|
|
30
|
+
const asset = Object.hasOwn(AGENT_ASSETS, id) ? AGENT_ASSETS[id] : undefined;
|
|
31
|
+
if (asset)
|
|
32
|
+
return `<link rel="icon" type="${asset.startsWith('data:image/png') ? 'image/png' : 'image/svg+xml'}" href="${asset}">`;
|
|
33
|
+
const svg = creatorMark(harness)
|
|
34
|
+
.replace('<svg ', '<svg xmlns="http://www.w3.org/2000/svg" ')
|
|
35
|
+
.replace('viewBox="0 0 24 24"', 'viewBox="-4 -4 32 32"')
|
|
36
|
+
.replace('aria-hidden="true"', 'fill="#222" color="#222"')
|
|
37
|
+
.replace('>', '><rect x="-4" y="-4" width="32" height="32" rx="7" fill="#faf9f6"/>');
|
|
38
|
+
return `<link rel="icon" type="image/svg+xml" href="${escapeHtml('data:image/svg+xml,' + encodeURIComponent(svg))}">`;
|
|
39
|
+
}
|
|
40
|
+
export const COVER_CSS = `
|
|
41
|
+
.hero.creator-cover{--cover-bg:#edf0ec;--cover-fg:#23332b;--cover-muted:#566c5e;--cover-accent:#629b77;--cover-glow:#c9dccd;position:relative;isolation:isolate;overflow:hidden;margin-top:24px;padding:32px 36px 26px;border:1px solid color-mix(in srgb,var(--cover-accent) 22%,var(--cover-bg));border-radius:18px;background:radial-gradient(ellipse at 100% 0%,var(--cover-glow),transparent 65%),var(--cover-bg);color:var(--cover-fg)}
|
|
42
|
+
.hero.cover-codex,.hero.cover-cursor{--cover-bg:#141c1b;--cover-fg:#f5f7f4;--cover-muted:#b4c5bf;--cover-accent:#9ddbbb;--cover-glow:#29463d}
|
|
43
|
+
.hero.cover-claude{--cover-bg:#f4e9db;--cover-fg:#3c2c24;--cover-muted:#80634e;--cover-accent:#ba6042;--cover-glow:#efd0b9}
|
|
44
|
+
.hero.cover-gemini,.hero.cover-copilot{--cover-bg:#edf1fc;--cover-fg:#273452;--cover-muted:#58688b;--cover-accent:#617cdf;--cover-glow:#d9d1f4}
|
|
45
|
+
.cover-top{display:flex;flex-wrap:wrap;align-items:center;justify-content:space-between;gap:20px;margin-bottom:34px;position:relative;z-index:1}
|
|
46
|
+
.hero.creator-cover .cover-creator{display:flex;align-items:center;gap:12px;min-width:0;color:var(--cover-fg);font:650 17px/1.3 var(--font-body);overflow-wrap:anywhere}
|
|
47
|
+
.cover-creator .chip-icon{width:36px;height:36px;margin:0;flex:0 0 36px;fill:currentColor}
|
|
48
|
+
.cover-creator img.chip-icon{object-fit:contain;background:#faf9f6;border-radius:7px;padding:3px}
|
|
49
|
+
.cover-project{display:inline-flex;align-items:center;gap:9px;max-width:60%;padding:7px 14px;border:1px solid color-mix(in srgb,var(--cover-accent) 32%,transparent);border-radius:18px;background:color-mix(in srgb,var(--cover-bg) 75%,transparent);color:var(--cover-fg);font:600 13px/1.4 var(--font-body);overflow-wrap:anywhere}
|
|
50
|
+
.cover-project:hover{color:var(--cover-fg);border-color:var(--cover-accent)}
|
|
51
|
+
.cover-project:focus-visible{outline:2px solid var(--cover-accent);outline-offset:3px}
|
|
52
|
+
.hero.creator-cover .kicker{color:var(--cover-muted);font-size:10px;letter-spacing:.09em;position:relative;z-index:1}
|
|
53
|
+
.hero.creator-cover .kicker-part{color:var(--cover-muted)}
|
|
54
|
+
.hero.creator-cover .status-badge{color:var(--cover-fg);border-color:color-mix(in srgb,var(--cover-accent) 32%,transparent);background:color-mix(in srgb,var(--cover-accent) 12%,transparent)}
|
|
55
|
+
.hero.creator-cover h1{position:relative;z-index:1;max-width:850px;margin:18px 0 18px;color:var(--cover-fg);font-size:clamp(30px,4vw,49px);line-height:1.12;font-weight:750;letter-spacing:-.035em;overflow-wrap:anywhere;text-wrap:balance}
|
|
56
|
+
.hero.creator-cover .summary{position:relative;z-index:1;max-width:780px;color:var(--cover-muted);font-size:17px;line-height:1.6}
|
|
57
|
+
.hero.creator-cover .summary a{color:var(--cover-fg)}
|
|
58
|
+
.hero.creator-cover .creator-line{position:relative;z-index:1;display:flex;flex-wrap:wrap;align-items:center;gap:10px 22px;margin:28px 0 0;padding-top:18px;border-top:1px solid color-mix(in srgb,var(--cover-accent) 23%,transparent);color:var(--cover-muted);font:400 12px/1.5 var(--font-body)}
|
|
59
|
+
.hero.creator-cover .creator-time,.hero.creator-cover .creator-human{color:var(--cover-muted)}
|
|
60
|
+
.creator-time[title]{cursor:help}.creator-line .meta-sep{display:none}
|
|
61
|
+
.cover-art{position:absolute;z-index:0;right:-15px;top:54px;width:300px;height:300px;opacity:.1;pointer-events:none;color:var(--cover-accent)}
|
|
62
|
+
.cover-art .chip-icon{position:absolute;inset:45px;width:210px;height:210px;margin:0;fill:currentColor}
|
|
63
|
+
.cover-art span{position:absolute;inset:0;border:1px solid currentColor;border-radius:50%}.cover-art span+span{inset:-45px}
|
|
64
|
+
.hero-metadata{padding:0 2px 22px;border-bottom:1px solid var(--line)}.hero-metadata .meta{gap:12px;margin-top:20px}
|
|
65
|
+
@media(max-width:720px){.hero.creator-cover{margin-top:18px;padding:24px 22px;border-radius:14px}.cover-top{gap:16px;margin-bottom:28px}.cover-project{max-width:100%}.cover-creator{font-size:16px}.cover-art{right:-80px;top:100px;opacity:.055}.hero.creator-cover h1{text-wrap:wrap}.hero.creator-cover .summary{font-size:16px}.hero.creator-cover .creator-line{gap:8px 18px}}
|
|
66
|
+
@media print{.hero.creator-cover{break-inside:avoid;margin-top:0}.cover-art{opacity:.05}.hero-metadata details>p{display:block!important}.hero.creator-cover a{color:var(--cover-fg)}}
|
|
67
|
+
`;
|
|
68
|
+
//# sourceMappingURL=creator-cover.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"creator-cover.js","sourceRoot":"","sources":["../../src/lib/creator-cover.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AACvC,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD,MAAM,OAAO,GAA2B;IACtC,aAAa,EAAE,QAAQ,EAAE,EAAE,EAAE,QAAQ,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,EAAE,EAAE,OAAO;IAC5F,YAAY,EAAE,QAAQ,EAAE,EAAE,EAAE,QAAQ,EAAE,cAAc,EAAE,QAAQ,EAAE,EAAE,EAAE,QAAQ;IAC5E,gBAAgB,EAAE,SAAS,EAAE,aA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OAAO,CAAC,kCAAkC,CAAC;AAC7G,CAAC;AAED,MAAM,UAAU,cAAc,CAAC,OAAe;IAC5C,MAAM,EAAE,GAAG,SAAS,CAAC,OAAO,CAAC,CAAC;IAC9B,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,YAAY,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAC7E,IAAI,KAAK;QAAE,OAAO,0BAA0B,KAAK,CAAC,UAAU,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC,CAAC,eAAe,WAAW,KAAK,IAAI,CAAC;IACnI,MAAM,GAAG,GAAG,WAAW,CAAC,OAAO,CAAC;SAC7B,OAAO,CAAC,OAAO,EAAE,0CAA0C,CAAC;SAC5D,OAAO,CAAC,qBAAqB,EAAE,uBAAuB,CAAC;SACvD,OAAO,CAAC,oBAAoB,EAAE,0BAA0B,CAAC;SACzD,OAAO,CAAC,GAAG,EAAE,qEAAqE,CAAC,CAAC;IACvF,OAAO,+CAA+C,UAAU,CAAC,qBAAqB,GAAG,kBAAkB,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;AACxH,CAAC;AAED,MAAM,CAAC,MAAM,SAAS,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;CA2BxB,CAAC"}
|
package/dist/lib/fonts.d.ts
CHANGED
|
@@ -9,4 +9,7 @@ export interface FontResolution {
|
|
|
9
9
|
display: string;
|
|
10
10
|
};
|
|
11
11
|
}
|
|
12
|
+
/** First family in a CSS stack (`Inter, system-ui` → `Inter`). */
|
|
13
|
+
export declare function firstFamily(value: string): string;
|
|
14
|
+
export declare function bundledFontsDir(): string;
|
|
12
15
|
export declare function resolveFonts(design: Design, cwd: string, cfg: ArtifactsConfig): FontResolution;
|
package/dist/lib/fonts.js
CHANGED
|
@@ -1,15 +1,34 @@
|
|
|
1
1
|
import { execFileSync } from 'node:child_process';
|
|
2
2
|
import { existsSync, readFileSync } from 'node:fs';
|
|
3
|
-
import { extname, isAbsolute, join } from 'node:path';
|
|
3
|
+
import { dirname, extname, isAbsolute, join } from 'node:path';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
4
5
|
const MIME = {
|
|
5
6
|
'.woff2': 'font/woff2',
|
|
6
7
|
'.woff': 'font/woff',
|
|
7
8
|
'.ttf': 'font/ttf',
|
|
8
9
|
'.otf': 'font/otf',
|
|
9
10
|
};
|
|
11
|
+
/** House faces shipped in the npm package. DESIGN.md `fonts.files` still wins. */
|
|
12
|
+
const BUNDLED_FILES = {
|
|
13
|
+
Inter: 'InterVariable.woff2',
|
|
14
|
+
'JetBrains Mono': 'JetBrainsMono-wght.woff2',
|
|
15
|
+
};
|
|
10
16
|
function quoteFamily(family) {
|
|
11
17
|
return family.includes(' ') ? `"${family}"` : family;
|
|
12
18
|
}
|
|
19
|
+
/** First family in a CSS stack (`Inter, system-ui` → `Inter`). */
|
|
20
|
+
export function firstFamily(value) {
|
|
21
|
+
const trimmed = value.trim();
|
|
22
|
+
if (!trimmed)
|
|
23
|
+
return value;
|
|
24
|
+
if (trimmed.startsWith('"') || trimmed.startsWith("'")) {
|
|
25
|
+
const q = trimmed[0];
|
|
26
|
+
const end = trimmed.indexOf(q, 1);
|
|
27
|
+
if (end > 1)
|
|
28
|
+
return trimmed.slice(1, end);
|
|
29
|
+
}
|
|
30
|
+
return trimmed.split(',')[0].trim().replace(/^["']|["']$/g, '');
|
|
31
|
+
}
|
|
13
32
|
function systemFontAvailable(family) {
|
|
14
33
|
try {
|
|
15
34
|
const out = execFileSync('fc-match', [family], { stdio: ['ignore', 'pipe', 'ignore'] }).toString().toLowerCase();
|
|
@@ -25,34 +44,71 @@ function resolveFontFile(path, cwd, cfg) {
|
|
|
25
44
|
candidates.push(join(dir, path));
|
|
26
45
|
return candidates.find((candidate) => existsSync(candidate)) ?? null;
|
|
27
46
|
}
|
|
47
|
+
export function bundledFontsDir() {
|
|
48
|
+
// src/lib/fonts.ts and dist/lib/fonts.js are both two levels above the package root.
|
|
49
|
+
return join(dirname(fileURLToPath(import.meta.url)), '..', '..', 'fonts');
|
|
50
|
+
}
|
|
51
|
+
function bundledFontPath(family) {
|
|
52
|
+
const file = BUNDLED_FILES[family];
|
|
53
|
+
if (!file)
|
|
54
|
+
return null;
|
|
55
|
+
const path = join(bundledFontsDir(), file);
|
|
56
|
+
return existsSync(path) ? path : null;
|
|
57
|
+
}
|
|
58
|
+
function embedFace(family, fontPath) {
|
|
59
|
+
const ext = extname(fontPath).toLowerCase();
|
|
60
|
+
const mime = MIME[ext];
|
|
61
|
+
if (!mime) {
|
|
62
|
+
return { css: '', warning: `Font file for "${family}" has unsupported extension ${ext}; expected woff2, woff, ttf, or otf.` };
|
|
63
|
+
}
|
|
64
|
+
const data = readFileSync(fontPath).toString('base64');
|
|
65
|
+
return {
|
|
66
|
+
css: `@font-face{font-family:${quoteFamily(family)};src:url(data:${mime};base64,${data}) format("${ext.slice(1)}");font-weight:100 900;font-style:normal;font-display:swap;}\n`,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
28
69
|
export function resolveFonts(design, cwd, cfg) {
|
|
29
70
|
const warnings = [];
|
|
30
71
|
let css = '';
|
|
72
|
+
const embedded = new Set();
|
|
31
73
|
for (const [family, source] of Object.entries(design.fonts.files ?? {})) {
|
|
32
74
|
const fontPath = resolveFontFile(source, cwd, cfg);
|
|
33
75
|
if (!fontPath) {
|
|
34
|
-
warnings.push(`Font file for "${family}" not found at ${source}; using
|
|
76
|
+
warnings.push(`Font file for "${family}" not found at ${source}; using the bundled face when one exists.`);
|
|
35
77
|
continue;
|
|
36
78
|
}
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
79
|
+
const face = embedFace(family, fontPath);
|
|
80
|
+
if (face.warning)
|
|
81
|
+
warnings.push(face.warning);
|
|
82
|
+
else {
|
|
83
|
+
css += face.css;
|
|
84
|
+
embedded.add(family);
|
|
42
85
|
}
|
|
43
|
-
const data = readFileSync(fontPath).toString('base64');
|
|
44
|
-
css += `@font-face{font-family:${quoteFamily(family)};src:url(data:${mime};base64,${data}) format("${ext.slice(1)}");font-weight:100 900;font-style:normal;font-display:swap;}\n`;
|
|
45
86
|
}
|
|
46
|
-
for (const
|
|
47
|
-
|
|
87
|
+
for (const raw of new Set([design.fonts.body, design.fonts.mono, design.fonts.display])) {
|
|
88
|
+
const family = firstFamily(raw);
|
|
89
|
+
if (embedded.has(family))
|
|
48
90
|
continue;
|
|
91
|
+
const bundled = bundledFontPath(family);
|
|
92
|
+
if (bundled) {
|
|
93
|
+
const face = embedFace(family, bundled);
|
|
94
|
+
if (face.warning)
|
|
95
|
+
warnings.push(face.warning);
|
|
96
|
+
else {
|
|
97
|
+
css += face.css;
|
|
98
|
+
embedded.add(family);
|
|
99
|
+
}
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
49
102
|
const available = systemFontAvailable(family);
|
|
50
103
|
if (available === false)
|
|
51
104
|
warnings.push(`Font "${family}" was not found locally; generated HTML will use fallback fonts on this machine.`);
|
|
52
105
|
if (available === null)
|
|
53
106
|
warnings.push(`Could not verify local font "${family}" because fc-match is unavailable; setup can still continue.`);
|
|
54
107
|
}
|
|
55
|
-
|
|
108
|
+
const body = quoteFamily(firstFamily(design.fonts.body));
|
|
109
|
+
const display = quoteFamily(firstFamily(design.fonts.display));
|
|
110
|
+
const mono = quoteFamily(firstFamily(design.fonts.mono));
|
|
111
|
+
css += `:root{--font-body:${body},Inter,system-ui,-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif;--font-display:${display},Inter,system-ui,sans-serif;--font-mono:${mono},"JetBrains Mono","SFMono-Regular",Consolas,monospace;}`;
|
|
56
112
|
return { css, warnings, families: { body: design.fonts.body, mono: design.fonts.mono, display: design.fonts.display } };
|
|
57
113
|
}
|
|
58
114
|
//# sourceMappingURL=fonts.js.map
|
package/dist/lib/fonts.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fonts.js","sourceRoot":"","sources":["../../src/lib/fonts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACnD,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;
|
|
1
|
+
{"version":3,"file":"fonts.js","sourceRoot":"","sources":["../../src/lib/fonts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACnD,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAC/D,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAczC,MAAM,IAAI,GAA2B;IACnC,QAAQ,EAAE,YAAY;IACtB,OAAO,EAAE,WAAW;IACpB,MAAM,EAAE,UAAU;IAClB,MAAM,EAAE,UAAU;CACnB,CAAC;AAEF,kFAAkF;AAClF,MAAM,aAAa,GAA2B;IAC5C,KAAK,EAAE,qBAAqB;IAC5B,gBAAgB,EAAE,0BAA0B;CAC7C,CAAC;AAEF,SAAS,WAAW,CAAC,MAAc;IACjC,OAAO,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,MAAM,GAAG,CAAC,CAAC,CAAC,MAAM,CAAC;AACvD,CAAC;AAED,kEAAkE;AAClE,MAAM,UAAU,WAAW,CAAC,KAAa;IACvC,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC;IAC7B,IAAI,CAAC,OAAO;QAAE,OAAO,KAAK,CAAC;IAC3B,IAAI,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,OAAO,CAAC,UAAU,CAAC,G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E,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;iBACzC,CAAC;gBACJ,GAAG,IAAI,IAAI,CAAC,GAAG,CAAC;gBAChB,QAAQ,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;YACvB,CAAC;YACD,SAAS;QACX,CAAC;QACD,MAAM,SAAS,GAAG,mBAAmB,CAAC,MAAM,CAAC,CAAC;QAC9C,IAAI,SAAS,KAAK,KAAK;YAAE,QAAQ,CAAC,IAAI,CAAC,SAAS,MAAM,kFAAkF,CAAC,CAAC;QAC1I,IAAI,SAAS,KAAK,IAAI;YAAE,QAAQ,CAAC,IAAI,CAAC,gCAAgC,MAAM,8DAA8D,CAAC,CAAC;IAC9I,CAAC;IAED,MAAM,IAAI,GAAG,WAAW,CAAC,WAAW,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;IACzD,MAAM,OAAO,GAAG,WAAW,CAAC,WAAW,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC;IAC/D,MAAM,IAAI,GAAG,WAAW,CAAC,WAAW,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;IACzD,GAAG,IAAI,qBAAqB,IAAI,0FAA0F,OAAO,2CAA2C,IAAI,yDAAyD,CAAC;IAC1O,OAAO,EAAE,GAAG,EAAE,QAAQ,EAAE,QAAQ,EAAE,EAAE,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,IAAI,EAAE,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,OAAO,EAAE,MAAM,CAAC,KAAK,CAAC,OAAO,EAAE,EAAE,CAAC;AAC1H,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** Date-only values deliberately carry no invented time-of-day. */
|
|
2
|
+
export declare function artifactTimestamp(value: unknown): {
|
|
3
|
+
iso: string;
|
|
4
|
+
relative: boolean;
|
|
5
|
+
} | undefined;
|
|
6
|
+
export declare const FRESHNESS_SCRIPT = "<script>\n(() => {\n const times = [...document.querySelectorAll('time[data-relative=\"true\"]')];\n const update = () => {\n const now = Date.now();\n for (const time of times) {\n const instant = new Date(time.dateTime);\n const seconds = Math.floor((now - instant.getTime()) / 1000);\n if (!Number.isFinite(seconds)) continue;\n const label = time.dataset.label;\n // Future dates are never called \"just now\".\n let value = seconds < 0 ? instant.toLocaleString() : instant.toISOString().slice(0, 10);\n if (seconds >= 0 && seconds < 60) value = 'just now';\n else if (seconds >= 60 && seconds < 3600) { const n = Math.floor(seconds / 60); value = n + (n === 1 ? ' minute ago' : ' minutes ago'); }\n else if (seconds >= 3600 && seconds < 86400) { const n = Math.floor(seconds / 3600); value = n + (n === 1 ? ' hour ago' : ' hours ago'); }\n else if (seconds >= 86400 && seconds < 604800) { const n = Math.floor(seconds / 86400); value = n + (n === 1 ? ' day ago' : ' days ago'); }\n time.textContent = label + ' ' + value;\n time.title = label + ' ' + instant.toLocaleString() + ' (' + time.dateTime + ')';\n }\n };\n update();\n if (times.length) setInterval(() => { if (!document.hidden) update(); }, 30000);\n document.addEventListener('visibilitychange', () => { if (!document.hidden) update(); });\n})();\n</script>";
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/** Date-only values deliberately carry no invented time-of-day. */
|
|
2
|
+
export function artifactTimestamp(value) {
|
|
3
|
+
if (value instanceof Date) {
|
|
4
|
+
if (!Number.isFinite(value.getTime()))
|
|
5
|
+
return undefined;
|
|
6
|
+
// YAML's date scalar loses precision information. Midnight stays absolute.
|
|
7
|
+
return { iso: value.toISOString(), relative: value.toISOString().slice(11) !== '00:00:00.000Z' };
|
|
8
|
+
}
|
|
9
|
+
if (typeof value !== 'string')
|
|
10
|
+
return undefined;
|
|
11
|
+
const input = value.trim();
|
|
12
|
+
if (!/^\d{4}-\d{2}-\d{2}(?:T\d{2}:\d{2}:\d{2}(?:\.\d{1,3})?(?:Z|[+-]\d{2}:\d{2}))?$/.test(input))
|
|
13
|
+
return undefined;
|
|
14
|
+
const calendar = input.slice(0, 10);
|
|
15
|
+
const day = new Date(calendar);
|
|
16
|
+
if (!Number.isFinite(day.getTime()) || day.toISOString().slice(0, 10) !== calendar)
|
|
17
|
+
return undefined;
|
|
18
|
+
if (input.length > 10 && (Number(input.slice(11, 13)) > 23 || Number(input.slice(14, 16)) > 59 || Number(input.slice(17, 19)) > 59))
|
|
19
|
+
return undefined;
|
|
20
|
+
const date = new Date(input);
|
|
21
|
+
if (!Number.isFinite(date.getTime()))
|
|
22
|
+
return undefined;
|
|
23
|
+
if (input.length === 10 && date.toISOString().slice(0, 10) !== input)
|
|
24
|
+
return undefined;
|
|
25
|
+
return { iso: input.length === 10 ? input : date.toISOString(), relative: input.length > 10 };
|
|
26
|
+
}
|
|
27
|
+
export const FRESHNESS_SCRIPT = `<script>
|
|
28
|
+
(() => {
|
|
29
|
+
const times = [...document.querySelectorAll('time[data-relative="true"]')];
|
|
30
|
+
const update = () => {
|
|
31
|
+
const now = Date.now();
|
|
32
|
+
for (const time of times) {
|
|
33
|
+
const instant = new Date(time.dateTime);
|
|
34
|
+
const seconds = Math.floor((now - instant.getTime()) / 1000);
|
|
35
|
+
if (!Number.isFinite(seconds)) continue;
|
|
36
|
+
const label = time.dataset.label;
|
|
37
|
+
// Future dates are never called "just now".
|
|
38
|
+
let value = seconds < 0 ? instant.toLocaleString() : instant.toISOString().slice(0, 10);
|
|
39
|
+
if (seconds >= 0 && seconds < 60) value = 'just now';
|
|
40
|
+
else if (seconds >= 60 && seconds < 3600) { const n = Math.floor(seconds / 60); value = n + (n === 1 ? ' minute ago' : ' minutes ago'); }
|
|
41
|
+
else if (seconds >= 3600 && seconds < 86400) { const n = Math.floor(seconds / 3600); value = n + (n === 1 ? ' hour ago' : ' hours ago'); }
|
|
42
|
+
else if (seconds >= 86400 && seconds < 604800) { const n = Math.floor(seconds / 86400); value = n + (n === 1 ? ' day ago' : ' days ago'); }
|
|
43
|
+
time.textContent = label + ' ' + value;
|
|
44
|
+
time.title = label + ' ' + instant.toLocaleString() + ' (' + time.dateTime + ')';
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
update();
|
|
48
|
+
if (times.length) setInterval(() => { if (!document.hidden) update(); }, 30000);
|
|
49
|
+
document.addEventListener('visibilitychange', () => { if (!document.hidden) update(); });
|
|
50
|
+
})();
|
|
51
|
+
</script>`;
|
|
52
|
+
//# sourceMappingURL=freshness.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"freshness.js","sourceRoot":"","sources":["../../src/lib/freshness.ts"],"names":[],"mappings":"AAAA,mEAAmE;AACnE,MAAM,UAAU,iBAAiB,CAAC,KAAc;IAC9C,IAAI,KAAK,YAAY,IAAI,EAAE,CAAC;QAC1B,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC;YAAE,OAAO,SAAS,CAAC;QACxD,2EAA2E;QAC3E,OAAO,EAAE,GAAG,EAAE,KAAK,CAAC,WAAW,EAAE,EAAE,QAAQ,EAAE,KAAK,CAAC,WAAW,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,eAAe,EAAE,CAAC;IACnG,CAAC;IACD,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,SAAS,CAAC;IAChD,MAAM,KAAK,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC;IAC3B,IAAI,CAAC,+EAA+E,CAAC,IAAI,CAAC,KAAK,CAAC;QAAE,OAAO,SAAS,CAAC;IACnH,MAAM,QAAQ,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;IACpC,MAAM,GAAG,GAAG,IAAI,IAAI,CAAC,QAAQ,CAAC,CAAC;IAC/B,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,EAAE,CAAC,IAAI,GAAG,CAAC,WAAW,EAAE,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,KAAK,QAAQ;QAAE,OAAO,SAAS,CAAC;IACrG,IAAI,KAAK,CAAC,MAAM,GAAG,EAAE,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,GAAG,EAAE,IAAI,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,GAAG,EAAE,IAAI,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,GAAG,EAAE,CAAC;QAAE,OAAO,SAAS,CAAC;IACtJ,MAAM,IAAI,GAAG,IAAI,IAAI,CAAC,KAAK,CAAC,CAAC;IAC7B,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;QAAE,OAAO,SAAS,CAAC;IACvD,IAAI,KAAK,CAAC,MAAM,KAAK,EAAE,IAAI,IAAI,CAAC,WAAW,EAAE,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,KAAK,KAAK;QAAE,OAAO,SAAS,CAAC;IACvF,OAAO,EAAE,GAAG,EAAE,KAAK,CAAC,MAAM,KAAK,EAAE,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,CAAC,WAAW,EAAE,EAAE,QAAQ,EAAE,KAAK,CAAC,MAAM,GAAG,EAAE,EAAE,CAAC;AAChG,CAAC;AAED,MAAM,CAAC,MAAM,gBAAgB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;UAwBtB,CAAC"}
|
package/dist/lib/highlights.d.ts
CHANGED
|
@@ -1 +1,11 @@
|
|
|
1
|
-
export declare const HIGHLIGHTS_SCRIPT = "<script>\n(() => {\n const root = document.querySelector('.artifact-body');\n if (!root) return;\n\n const storageKey = 'artifacts-highlights:' + location.origin + location.pathname + ':' + (document.title || '');\n const selector = 'h2,h3,p,li,blockquote,pre,figcaption,td,th,dd';\n const hash = (value) => {\n let result = 2166136261;\n for (let index = 0; index < value.length; index += 1) { result ^= value.charCodeAt(index); result = Math.imul(result, 16777619); }\n return (result >>> 0).toString(36);\n };\n const anchors = [...root.querySelectorAll(selector)].filter((element) => !element.parentElement.closest(selector));\n const bases = [];\n let section = 'document';\n for (const anchor of anchors) {\n if ((anchor.tagName === 'H2' || anchor.tagName === 'H3') && anchor.id) section = anchor.id;\n anchor.dataset.readerSection = section;\n const text = (anchor.textContent || '').replace(/\\s+/g, ' ').trim();\n bases.push(anchor.id ? 'id:' + anchor.id : section + ':' + anchor.tagName.toLowerCase() + ':' + hash(text));\n }\n const totals = new Map();\n for (const base of bases) totals.set(base, (totals.get(base) || 0) + 1);\n const occurrences = new Map();\n for (let index = 0; index < anchors.length; index += 1) {\n const anchor = anchors[index];\n const base = bases[index];\n const occurrence = (occurrences.get(base) || 0) + 1;\n occurrences.set(base, occurrence);\n anchor.dataset.readerAnchor = base.startsWith('id:') ? base : base + ':count:' + totals.get(base) + ':occurrence:' + occurrence;\n }\n const byId = new Map(anchors.map((anchor) => [anchor.dataset.readerAnchor, anchor]));\n\n const read = () => {\n try {\n const value = JSON.parse(localStorage.getItem(storageKey) || '[]');\n return Array.isArray(value) ? value.filter((item) => item && item.id && item.start && item.end).map((item) => ({\n quote: '', note: '', section: 'document', created_at: new Date().toISOString(), ...item,\n })) : [];\n } catch { return []; }\n };\n let highlights = read();\n let postTimer;\n const post = () => {\n if (location.protocol !== 'http:' || !['127.0.0.1', 'localhost', '::1'].includes(location.hostname)) return;\n fetch('/notes', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ schema: 'artifacts.notes.v1', notes: highlights }) }).catch(() => {});\n };\n const write = () => {\n try { localStorage.setItem(storageKey, JSON.stringify(highlights)); } catch {}\n clearTimeout(postTimer); postTimer = setTimeout(post, 600);\n };\n const textLength = (anchor) => (anchor.textContent || '').length;\n const offsetAt = (anchor, node, offset) => {\n const range = document.createRange(); range.selectNodeContents(anchor);\n try { range.setEnd(node, offset); } catch { return null; }\n return range.toString().length;\n };\n const wrap = (anchor, start, end, id) => {\n if (end <= start) return;\n const nodes = []; const walker = document.createTreeWalker(anchor, NodeFilter.SHOW_TEXT); let cursor = 0; let node = walker.nextNode();\n while (node) {\n const nodeStart = cursor; const nodeEnd = cursor + node.data.length;\n if (nodeEnd > start && nodeStart < end) nodes.push([node, Math.max(0, start - nodeStart), Math.min(node.data.length, end - nodeStart)]);\n cursor = nodeEnd; node = walker.nextNode();\n }\n for (let index = nodes.length - 1; index >= 0; index -= 1) {\n const [textNode, from, to] = nodes[index]; if (to <= from) continue;\n const range = document.createRange(); range.setStart(textNode, from); range.setEnd(textNode, to);\n const mark = document.createElement('mark'); mark.className = 'reader-highlight'; mark.dataset.highlightId = id; mark.title = 'Open note';\n range.surroundContents(mark);\n }\n };\n const clearMarks = () => {\n for (const mark of root.querySelectorAll('mark[data-highlight-id]')) mark.replaceWith(...mark.childNodes);\n for (const anchor of anchors) anchor.normalize();\n };\n const render = () => {\n clearMarks();\n for (const highlight of highlights) {\n const first = byId.get(highlight.start.anchor); const last = byId.get(highlight.end.anchor);\n const firstIndex = anchors.indexOf(first); const lastIndex = anchors.indexOf(last);\n if (!first || !last || firstIndex < 0 || lastIndex < firstIndex) continue;\n for (let index = firstIndex; index <= lastIndex; index += 1) wrap(anchors[index], index === firstIndex ? highlight.start.offset : 0, index === lastIndex ? highlight.end.offset : textLength(anchors[index]), highlight.id);\n }\n };\n\n const style = document.createElement('style');\n style.textContent = '.reader-toolbar,.reader-popover{position:fixed;z-index:40;display:flex;align-items:center;gap:6px;padding:7px;border:1px solid var(--line,#ccc);border-radius:10px;background:var(--surface,#fff);color:var(--text,#111);box-shadow:0 10px 35px rgba(0,0,0,.18);font:500 12px/1.2 var(--font-mono,monospace)}.reader-toolbar[hidden],.reader-popover[hidden]{display:none}.reader-toolbar-status{padding:0 6px;color:var(--muted,#666)}.reader-toolbar button,.reader-popover button{border:1px solid var(--line,#ccc);border-radius:999px;background:var(--surface-alt,#f5f5f5);color:inherit;padding:6px 9px;font:inherit;cursor:pointer}.reader-popover{width:min(360px,calc(100vw - 24px));align-items:stretch;flex-direction:column;padding:12px}.reader-popover-quote{margin:0;color:var(--muted,#666);font-style:italic;line-height:1.45}.reader-popover textarea{width:100%;min-height:84px;resize:vertical;border:1px solid var(--line,#ccc);border-radius:7px;padding:8px;background:var(--bg,#fff);color:inherit;font:13px/1.4 var(--font-body,sans-serif)}.reader-popover-actions{display:flex;gap:6px}.reader-remove{margin-left:auto;color:var(--danger,#b91c1c)!important}';\n document.head.append(style);\n const toolbar = document.createElement('div'); toolbar.className = 'reader-toolbar'; toolbar.hidden = true;\n toolbar.innerHTML = '<span class=\"reader-toolbar-status\">Copied</span><span aria-hidden=\"true\">\u00B7</span><button type=\"button\" data-reader-action=\"highlight\">Highlight</button><span aria-hidden=\"true\">\u00B7</span><button type=\"button\" data-reader-action=\"note\">Note</button>';\n document.body.append(toolbar);\n const popover = document.createElement('div'); popover.className = 'reader-popover'; popover.hidden = true;\n popover.innerHTML = '<p class=\"reader-popover-quote\"></p><textarea aria-label=\"Note\" placeholder=\"Attach a note\u2026\"></textarea><div class=\"reader-popover-actions\"><button type=\"button\" data-reader-action=\"copy\">Copy</button><button type=\"button\" class=\"reader-remove\" data-reader-action=\"remove\">Remove</button></div>';\n document.body.append(popover);\n let pending = null; let activeId = null; let noteTimer;\n const place = (element, rect) => { element.style.left = Math.max(8, Math.min(innerWidth - element.offsetWidth - 8, rect.left)) + 'px'; element.style.top = Math.max(8, rect.bottom + 8) + 'px'; };\n const openNote = (item, rect) => {\n activeId = item.id; toolbar.hidden = true; popover.hidden = false;\n popover.querySelector('.reader-popover-quote').textContent = '\u201C' + item.quote + '\u201D'; popover.querySelector('textarea').value = item.note || '';\n place(popover, rect);\n };\n const persistPending = (open) => {\n if (!pending) return;\n const duplicate = highlights.find((item) => item.start.anchor === pending.start.anchor && item.start.offset === pending.start.offset && item.end.anchor === pending.end.anchor && item.end.offset === pending.end.offset);\n const item = duplicate || { id: Date.now().toString(36) + Math.random().toString(36).slice(2), ...pending, created_at: new Date().toISOString() };\n if (!duplicate) highlights.push(item);\n write(); toolbar.hidden = true; render();\n if (open) openNote(item, pending.rect);\n };\n\n root.addEventListener('mouseup', () => {\n const selection = getSelection(); if (!selection || selection.isCollapsed || !selection.rangeCount) return;\n const range = selection.getRangeAt(0); const startAnchor = range.startContainer.parentElement?.closest('[data-reader-anchor]'); const endAnchor = range.endContainer.parentElement?.closest('[data-reader-anchor]');\n if (!startAnchor || !endAnchor || !root.contains(startAnchor) || !root.contains(endAnchor)) return;\n const startOffset = offsetAt(startAnchor, range.startContainer, range.startOffset); const endOffset = offsetAt(endAnchor, range.endContainer, range.endOffset);\n if (startOffset === null || endOffset === null) return;\n const startIndex = anchors.indexOf(startAnchor); const endIndex = anchors.indexOf(endAnchor);\n if (startIndex > endIndex || (startIndex === endIndex && startOffset >= endOffset)) return;\n const quote = selection.toString(); const rect = range.getBoundingClientRect();\n pending = { quote, note: '', section: startAnchor.dataset.readerSection || 'document', start: { anchor: startAnchor.dataset.readerAnchor, offset: startOffset }, end: { anchor: endAnchor.dataset.readerAnchor, offset: endOffset }, rect };\n navigator.clipboard?.writeText(quote).catch(() => {});\n popover.hidden = true; toolbar.hidden = false; requestAnimationFrame(() => place(toolbar, rect));\n });\n toolbar.addEventListener('mousedown', (event) => event.preventDefault());\n toolbar.addEventListener('click', (event) => {\n const action = event.target.closest('button')?.dataset.readerAction;\n if (action === 'highlight') persistPending(false);\n if (action === 'note') persistPending(true);\n });\n root.addEventListener('click', (event) => {\n const mark = event.target.closest('mark[data-highlight-id]'); if (!mark || !root.contains(mark)) return;\n event.preventDefault(); event.stopPropagation();\n const item = highlights.find((value) => value.id === mark.dataset.highlightId); if (item) openNote(item, mark.getBoundingClientRect());\n });\n popover.querySelector('textarea').addEventListener('input', (event) => {\n clearTimeout(noteTimer); noteTimer = setTimeout(() => {\n const item = highlights.find((value) => value.id === activeId); if (item) { item.note = event.target.value; write(); }\n }, 600);\n });\n popover.addEventListener('click', (event) => {\n const action = event.target.closest('button')?.dataset.readerAction; const item = highlights.find((value) => value.id === activeId); if (!item) return;\n if (action === 'copy') navigator.clipboard?.writeText(item.quote).catch(() => {});\n if (action === 'remove') { highlights = highlights.filter((value) => value.id !== activeId); write(); popover.hidden = true; render(); }\n });\n render();\n})();\n</script>";
|
|
1
|
+
/**
|
|
2
|
+
* Local reader review: highlight a selection, attach a private note, keep both in
|
|
3
|
+
* `localStorage` and — on a loopback dev server only — mirror them to a sidecar.
|
|
4
|
+
*
|
|
5
|
+
* Ranges come from the shared anchor runtime in `reader-anchors.ts`; this module
|
|
6
|
+
* owns the selection toolbar, the note popover, and local persistence. The
|
|
7
|
+
* toolbar is extensible (`window.__artifactsHighlights.addAction`) so a hosted
|
|
8
|
+
* surface can add its own affordance next to Highlight and Note without forking
|
|
9
|
+
* the selection handling.
|
|
10
|
+
*/
|
|
11
|
+
export declare const HIGHLIGHTS_SCRIPT = "<script>\n(() => {\n const reader = window.__artifactsReader;\n if (!reader) return;\n const root = reader.root;\n\n const storageKey = 'artifacts-highlights:' + location.origin + location.pathname + ':' + (document.title || '');\n const read = () => {\n try {\n const value = JSON.parse(localStorage.getItem(storageKey) || '[]');\n return Array.isArray(value) ? value.filter((item) => item && item.id && item.start && item.end).map((item) => ({\n quote: '', note: '', section: 'document', created_at: new Date().toISOString(), ...item,\n })) : [];\n } catch { return []; }\n };\n let highlights = read();\n let postTimer;\n const post = () => {\n if (location.protocol !== 'http:' || !['127.0.0.1', 'localhost', '::1'].includes(location.hostname)) return;\n fetch('/notes', { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ schema: 'artifacts.notes.v1', notes: highlights }) }).catch(() => {});\n };\n const write = () => {\n try { localStorage.setItem(storageKey, JSON.stringify(highlights)); } catch {}\n clearTimeout(postTimer); postTimer = setTimeout(post, 600);\n };\n const render = () => {\n reader.clearMarks('mark[data-highlight-id]');\n for (const highlight of highlights) {\n reader.paint(highlight, () => {\n const mark = document.createElement('mark');\n mark.className = 'reader-highlight'; mark.dataset.highlightId = highlight.id; mark.title = 'Open note';\n return mark;\n });\n }\n };\n\n const style = document.createElement('style');\n style.textContent = '.reader-toolbar,.reader-popover{position:fixed;z-index:40;display:flex;align-items:center;gap:6px;padding:7px;border:1px solid var(--line,#ccc);border-radius:10px;background:var(--surface,#fff);color:var(--text,#111);box-shadow:0 10px 35px rgba(0,0,0,.18);font:500 12px/1.2 var(--font-mono,monospace)}.reader-toolbar[hidden],.reader-popover[hidden]{display:none}.reader-toolbar-status{padding:0 6px;color:var(--muted,#666)}.reader-toolbar button,.reader-popover button{border:1px solid var(--line,#ccc);border-radius:999px;background:var(--surface-alt,#f5f5f5);color:inherit;padding:6px 9px;font:inherit;cursor:pointer}.reader-popover{width:min(360px,calc(100vw - 24px));align-items:stretch;flex-direction:column;padding:12px}.reader-popover-quote{margin:0;color:var(--muted,#666);font-style:italic;line-height:1.45}.reader-popover textarea{width:100%;min-height:84px;resize:vertical;border:1px solid var(--line,#ccc);border-radius:7px;padding:8px;background:var(--bg,#fff);color:inherit;font:13px/1.4 var(--font-body,sans-serif)}.reader-popover-actions{display:flex;gap:6px}.reader-remove{margin-left:auto;color:var(--danger,#b91c1c)!important}';\n document.head.append(style);\n const toolbar = document.createElement('div'); toolbar.className = 'reader-toolbar'; toolbar.hidden = true;\n toolbar.innerHTML = '<span class=\"reader-toolbar-status\">Copied</span><span aria-hidden=\"true\">\u00B7</span><button type=\"button\" data-reader-action=\"highlight\">Highlight</button><span aria-hidden=\"true\">\u00B7</span><button type=\"button\" data-reader-action=\"note\">Note</button>';\n document.body.append(toolbar);\n const popover = document.createElement('div'); popover.className = 'reader-popover'; popover.hidden = true;\n popover.innerHTML = '<p class=\"reader-popover-quote\"></p><textarea aria-label=\"Note\" placeholder=\"Attach a note\u2026\"></textarea><div class=\"reader-popover-actions\"><button type=\"button\" data-reader-action=\"copy\">Copy</button><button type=\"button\" class=\"reader-remove\" data-reader-action=\"remove\">Remove</button></div>';\n document.body.append(popover);\n let pending = null; let activeId = null; let noteTimer;\n const place = (element, rect) => { element.style.left = Math.max(8, Math.min(innerWidth - element.offsetWidth - 8, rect.left)) + 'px'; element.style.top = Math.max(8, rect.bottom + 8) + 'px'; };\n const openNote = (item, rect) => {\n activeId = item.id; toolbar.hidden = true; popover.hidden = false;\n popover.querySelector('.reader-popover-quote').textContent = '\u201C' + item.quote + '\u201D'; popover.querySelector('textarea').value = item.note || '';\n place(popover, rect);\n };\n const persistPending = (open) => {\n if (!pending) return;\n const duplicate = highlights.find((item) => item.start.anchor === pending.start.anchor && item.start.offset === pending.start.offset && item.end.anchor === pending.end.anchor && item.end.offset === pending.end.offset);\n const item = duplicate || { id: Date.now().toString(36) + Math.random().toString(36).slice(2), ...pending, created_at: new Date().toISOString() };\n if (!duplicate) highlights.push(item);\n write(); toolbar.hidden = true; render();\n if (open) openNote(item, pending.rect);\n };\n\n const extras = new Map();\n window.__artifactsHighlights = {\n // Adds a button beside Highlight and Note. The handler receives the pending\n // selection record in the shared anchor vocabulary.\n addAction: (id, label, handler) => {\n extras.set(id, handler);\n const separator = document.createElement('span'); separator.setAttribute('aria-hidden', 'true'); separator.textContent = '\u00B7';\n const button = document.createElement('button'); button.type = 'button'; button.dataset.readerAction = id; button.textContent = label;\n toolbar.append(separator, button);\n },\n dismiss: () => { toolbar.hidden = true; },\n };\n\n root.addEventListener('mouseup', () => {\n const record = reader.selectionRecord(); if (!record) return;\n pending = { ...record, note: '' };\n navigator.clipboard?.writeText(record.quote).catch(() => {});\n popover.hidden = true; toolbar.hidden = false; requestAnimationFrame(() => place(toolbar, record.rect));\n });\n toolbar.addEventListener('mousedown', (event) => event.preventDefault());\n toolbar.addEventListener('click', (event) => {\n const action = event.target.closest('button')?.dataset.readerAction;\n if (action === 'highlight') persistPending(false);\n else if (action === 'note') persistPending(true);\n else if (extras.has(action)) extras.get(action)(pending);\n });\n root.addEventListener('click', (event) => {\n const mark = event.target.closest('mark[data-highlight-id]'); if (!mark || !root.contains(mark)) return;\n event.preventDefault(); event.stopPropagation();\n const item = highlights.find((value) => value.id === mark.dataset.highlightId); if (item) openNote(item, mark.getBoundingClientRect());\n });\n popover.querySelector('textarea').addEventListener('input', (event) => {\n clearTimeout(noteTimer); noteTimer = setTimeout(() => {\n const item = highlights.find((value) => value.id === activeId); if (item) { item.note = event.target.value; write(); }\n }, 600);\n });\n popover.addEventListener('click', (event) => {\n const action = event.target.closest('button')?.dataset.readerAction; const item = highlights.find((value) => value.id === activeId); if (!item) return;\n if (action === 'copy') navigator.clipboard?.writeText(item.quote).catch(() => {});\n if (action === 'remove') { highlights = highlights.filter((value) => value.id !== activeId); write(); popover.hidden = true; render(); }\n });\n render();\n})();\n</script>";
|
package/dist/lib/highlights.js
CHANGED
|
@@ -1,36 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Local reader review: highlight a selection, attach a private note, keep both in
|
|
3
|
+
* `localStorage` and — on a loopback dev server only — mirror them to a sidecar.
|
|
4
|
+
*
|
|
5
|
+
* Ranges come from the shared anchor runtime in `reader-anchors.ts`; this module
|
|
6
|
+
* owns the selection toolbar, the note popover, and local persistence. The
|
|
7
|
+
* toolbar is extensible (`window.__artifactsHighlights.addAction`) so a hosted
|
|
8
|
+
* surface can add its own affordance next to Highlight and Note without forking
|
|
9
|
+
* the selection handling.
|
|
10
|
+
*/
|
|
1
11
|
export const HIGHLIGHTS_SCRIPT = `<script>
|
|
2
12
|
(() => {
|
|
3
|
-
const
|
|
4
|
-
if (!
|
|
13
|
+
const reader = window.__artifactsReader;
|
|
14
|
+
if (!reader) return;
|
|
15
|
+
const root = reader.root;
|
|
5
16
|
|
|
6
17
|
const storageKey = 'artifacts-highlights:' + location.origin + location.pathname + ':' + (document.title || '');
|
|
7
|
-
const selector = 'h2,h3,p,li,blockquote,pre,figcaption,td,th,dd';
|
|
8
|
-
const hash = (value) => {
|
|
9
|
-
let result = 2166136261;
|
|
10
|
-
for (let index = 0; index < value.length; index += 1) { result ^= value.charCodeAt(index); result = Math.imul(result, 16777619); }
|
|
11
|
-
return (result >>> 0).toString(36);
|
|
12
|
-
};
|
|
13
|
-
const anchors = [...root.querySelectorAll(selector)].filter((element) => !element.parentElement.closest(selector));
|
|
14
|
-
const bases = [];
|
|
15
|
-
let section = 'document';
|
|
16
|
-
for (const anchor of anchors) {
|
|
17
|
-
if ((anchor.tagName === 'H2' || anchor.tagName === 'H3') && anchor.id) section = anchor.id;
|
|
18
|
-
anchor.dataset.readerSection = section;
|
|
19
|
-
const text = (anchor.textContent || '').replace(/\\s+/g, ' ').trim();
|
|
20
|
-
bases.push(anchor.id ? 'id:' + anchor.id : section + ':' + anchor.tagName.toLowerCase() + ':' + hash(text));
|
|
21
|
-
}
|
|
22
|
-
const totals = new Map();
|
|
23
|
-
for (const base of bases) totals.set(base, (totals.get(base) || 0) + 1);
|
|
24
|
-
const occurrences = new Map();
|
|
25
|
-
for (let index = 0; index < anchors.length; index += 1) {
|
|
26
|
-
const anchor = anchors[index];
|
|
27
|
-
const base = bases[index];
|
|
28
|
-
const occurrence = (occurrences.get(base) || 0) + 1;
|
|
29
|
-
occurrences.set(base, occurrence);
|
|
30
|
-
anchor.dataset.readerAnchor = base.startsWith('id:') ? base : base + ':count:' + totals.get(base) + ':occurrence:' + occurrence;
|
|
31
|
-
}
|
|
32
|
-
const byId = new Map(anchors.map((anchor) => [anchor.dataset.readerAnchor, anchor]));
|
|
33
|
-
|
|
34
18
|
const read = () => {
|
|
35
19
|
try {
|
|
36
20
|
const value = JSON.parse(localStorage.getItem(storageKey) || '[]');
|
|
@@ -49,38 +33,14 @@ export const HIGHLIGHTS_SCRIPT = `<script>
|
|
|
49
33
|
try { localStorage.setItem(storageKey, JSON.stringify(highlights)); } catch {}
|
|
50
34
|
clearTimeout(postTimer); postTimer = setTimeout(post, 600);
|
|
51
35
|
};
|
|
52
|
-
const textLength = (anchor) => (anchor.textContent || '').length;
|
|
53
|
-
const offsetAt = (anchor, node, offset) => {
|
|
54
|
-
const range = document.createRange(); range.selectNodeContents(anchor);
|
|
55
|
-
try { range.setEnd(node, offset); } catch { return null; }
|
|
56
|
-
return range.toString().length;
|
|
57
|
-
};
|
|
58
|
-
const wrap = (anchor, start, end, id) => {
|
|
59
|
-
if (end <= start) return;
|
|
60
|
-
const nodes = []; const walker = document.createTreeWalker(anchor, NodeFilter.SHOW_TEXT); let cursor = 0; let node = walker.nextNode();
|
|
61
|
-
while (node) {
|
|
62
|
-
const nodeStart = cursor; const nodeEnd = cursor + node.data.length;
|
|
63
|
-
if (nodeEnd > start && nodeStart < end) nodes.push([node, Math.max(0, start - nodeStart), Math.min(node.data.length, end - nodeStart)]);
|
|
64
|
-
cursor = nodeEnd; node = walker.nextNode();
|
|
65
|
-
}
|
|
66
|
-
for (let index = nodes.length - 1; index >= 0; index -= 1) {
|
|
67
|
-
const [textNode, from, to] = nodes[index]; if (to <= from) continue;
|
|
68
|
-
const range = document.createRange(); range.setStart(textNode, from); range.setEnd(textNode, to);
|
|
69
|
-
const mark = document.createElement('mark'); mark.className = 'reader-highlight'; mark.dataset.highlightId = id; mark.title = 'Open note';
|
|
70
|
-
range.surroundContents(mark);
|
|
71
|
-
}
|
|
72
|
-
};
|
|
73
|
-
const clearMarks = () => {
|
|
74
|
-
for (const mark of root.querySelectorAll('mark[data-highlight-id]')) mark.replaceWith(...mark.childNodes);
|
|
75
|
-
for (const anchor of anchors) anchor.normalize();
|
|
76
|
-
};
|
|
77
36
|
const render = () => {
|
|
78
|
-
clearMarks();
|
|
37
|
+
reader.clearMarks('mark[data-highlight-id]');
|
|
79
38
|
for (const highlight of highlights) {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
39
|
+
reader.paint(highlight, () => {
|
|
40
|
+
const mark = document.createElement('mark');
|
|
41
|
+
mark.className = 'reader-highlight'; mark.dataset.highlightId = highlight.id; mark.title = 'Open note';
|
|
42
|
+
return mark;
|
|
43
|
+
});
|
|
84
44
|
}
|
|
85
45
|
};
|
|
86
46
|
|
|
@@ -109,24 +69,31 @@ export const HIGHLIGHTS_SCRIPT = `<script>
|
|
|
109
69
|
if (open) openNote(item, pending.rect);
|
|
110
70
|
};
|
|
111
71
|
|
|
72
|
+
const extras = new Map();
|
|
73
|
+
window.__artifactsHighlights = {
|
|
74
|
+
// Adds a button beside Highlight and Note. The handler receives the pending
|
|
75
|
+
// selection record in the shared anchor vocabulary.
|
|
76
|
+
addAction: (id, label, handler) => {
|
|
77
|
+
extras.set(id, handler);
|
|
78
|
+
const separator = document.createElement('span'); separator.setAttribute('aria-hidden', 'true'); separator.textContent = '·';
|
|
79
|
+
const button = document.createElement('button'); button.type = 'button'; button.dataset.readerAction = id; button.textContent = label;
|
|
80
|
+
toolbar.append(separator, button);
|
|
81
|
+
},
|
|
82
|
+
dismiss: () => { toolbar.hidden = true; },
|
|
83
|
+
};
|
|
84
|
+
|
|
112
85
|
root.addEventListener('mouseup', () => {
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
if (startOffset === null || endOffset === null) return;
|
|
118
|
-
const startIndex = anchors.indexOf(startAnchor); const endIndex = anchors.indexOf(endAnchor);
|
|
119
|
-
if (startIndex > endIndex || (startIndex === endIndex && startOffset >= endOffset)) return;
|
|
120
|
-
const quote = selection.toString(); const rect = range.getBoundingClientRect();
|
|
121
|
-
pending = { quote, note: '', section: startAnchor.dataset.readerSection || 'document', start: { anchor: startAnchor.dataset.readerAnchor, offset: startOffset }, end: { anchor: endAnchor.dataset.readerAnchor, offset: endOffset }, rect };
|
|
122
|
-
navigator.clipboard?.writeText(quote).catch(() => {});
|
|
123
|
-
popover.hidden = true; toolbar.hidden = false; requestAnimationFrame(() => place(toolbar, rect));
|
|
86
|
+
const record = reader.selectionRecord(); if (!record) return;
|
|
87
|
+
pending = { ...record, note: '' };
|
|
88
|
+
navigator.clipboard?.writeText(record.quote).catch(() => {});
|
|
89
|
+
popover.hidden = true; toolbar.hidden = false; requestAnimationFrame(() => place(toolbar, record.rect));
|
|
124
90
|
});
|
|
125
91
|
toolbar.addEventListener('mousedown', (event) => event.preventDefault());
|
|
126
92
|
toolbar.addEventListener('click', (event) => {
|
|
127
93
|
const action = event.target.closest('button')?.dataset.readerAction;
|
|
128
94
|
if (action === 'highlight') persistPending(false);
|
|
129
|
-
if (action === 'note') persistPending(true);
|
|
95
|
+
else if (action === 'note') persistPending(true);
|
|
96
|
+
else if (extras.has(action)) extras.get(action)(pending);
|
|
130
97
|
});
|
|
131
98
|
root.addEventListener('click', (event) => {
|
|
132
99
|
const mark = event.target.closest('mark[data-highlight-id]'); if (!mark || !root.contains(mark)) return;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"highlights.js","sourceRoot":"","sources":["../../src/lib/highlights.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,iBAAiB,GAAG
|
|
1
|
+
{"version":3,"file":"highlights.js","sourceRoot":"","sources":["../../src/lib/highlights.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;UAwGvB,CAAC"}
|
package/dist/lib/icons.js
CHANGED
|
@@ -14,7 +14,8 @@ export function humanIconSvg() {
|
|
|
14
14
|
return `<svg class="chip-icon" viewBox="0 0 24 24" aria-hidden="true">${HUMAN_ICON}</svg>`;
|
|
15
15
|
}
|
|
16
16
|
export function harnessIconSvg(harness) {
|
|
17
|
-
const
|
|
17
|
+
const id = harness.trim().toLowerCase();
|
|
18
|
+
const path = Object.hasOwn(HARNESS_ICON_PATHS, id) ? HARNESS_ICON_PATHS[id] : undefined;
|
|
18
19
|
const body = path ? `<path d="${path}"/>` : FALLBACK_ICON;
|
|
19
20
|
return `<svg class="chip-icon" viewBox="0 0 24 24" aria-hidden="true">${body}</svg>`;
|
|
20
21
|
}
|
package/dist/lib/icons.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"icons.js","sourceRoot":"","sources":["../../src/lib/icons.ts"],"names":[],"mappings":"AAAA,wEAAwE;AACxE,0EAA0E;AAC1E,oCAAoC;AAEpC,MAAM,kBAAkB,GAA2B;IACjD,MAAM,EACJ,qxDAAqxD;IACvxD,KAAK,EACH,s7CAAs7C;IACx7C,MAAM,EACJ,8RAA8R;IAChS,MAAM,EACJ,0VAA0V;IAC5V,OAAO,EACL,4lDAA4lD;CAC/lD,CAAC;AAEF,MAAM,aAAa,GACjB,uOAAuO,CAAC;AAE1O,MAAM,UAAU,GACd,wKAAwK,CAAC;AAE3K,MAAM,UAAU,YAAY;IAC1B,OAAO,iEAAiE,UAAU,QAAQ,CAAC;AAC7F,CAAC;AAED,MAAM,UAAU,cAAc,CAAC,OAAe;IAC5C,MAAM,
|
|
1
|
+
{"version":3,"file":"icons.js","sourceRoot":"","sources":["../../src/lib/icons.ts"],"names":[],"mappings":"AAAA,wEAAwE;AACxE,0EAA0E;AAC1E,oCAAoC;AAEpC,MAAM,kBAAkB,GAA2B;IACjD,MAAM,EACJ,qxDAAqxD;IACvxD,KAAK,EACH,s7CAAs7C;IACx7C,MAAM,EACJ,8RAA8R;IAChS,MAAM,EACJ,0VAA0V;IAC5V,OAAO,EACL,4lDAA4lD;CAC/lD,CAAC;AAEF,MAAM,aAAa,GACjB,uOAAuO,CAAC;AAE1O,MAAM,UAAU,GACd,wKAAwK,CAAC;AAE3K,MAAM,UAAU,YAAY;IAC1B,OAAO,iEAAiE,UAAU,QAAQ,CAAC;AAC7F,CAAC;AAED,MAAM,UAAU,cAAc,CAAC,OAAe;IAC5C,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;IACxC,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,kBAAkB,EAAE,EAAE,CAAC,CAAC,CAAC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tG,CAAC;AAED,SAAS,WAAW,CAAC,GAAW;IAC9B,IAAI,CAAC;QACH,OAAO,IAAI,GAAG,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC,WAAW,EAAE,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;IACnE,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC;AASD,MAAM,iBAAiB,GAAoB,EAAE,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,cAAc,EAAE,CAAC;AAE1F;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAAC,GAAW,EAAE,UAA2B,iBAAiB;IACpF,MAAM,IAAI,GAAG,WAAW,CAAC,GAAG,CAAC,CAAC;IAC9B,IAAI,CAAC,IAAI;QAAE,OAAO,SAAS,CAAC;IAE5B,KAAK,MAAM,IAAI,IAAI,eAAe,EAAE,CAAC;QACnC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC;YAAE,SAAS;QAChC,MAAM,IAAI,GAAG,gBAAgB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAC1C,IAAI,IAAI;YAAE,OAAO,YAAY,CAAC,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IACpD,CAAC;IAED,2EAA2E;IAC3E,OAAO,eAAe,OAAO,CAAC,OAAO,oDAAoD,kBAAkB,CAAC,IAAI,CAAC,sGAAsG,CAAC;AAC1N,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The reader anchor runtime: one block-relative selector vocabulary shared by
|
|
3
|
+
* every reading surface that has to point at a range of rendered text.
|
|
4
|
+
*
|
|
5
|
+
* A range is stored as `{ quote, section, start: { anchor, offset }, end: { anchor, offset } }`
|
|
6
|
+
* where `anchor` is a block's stable id and `offset` is a character offset inside
|
|
7
|
+
* that block's text. The id is `id:<heading-id>` when the block carries one, and
|
|
8
|
+
* otherwise `<section>:<tag>:<hash>:count:<total>:occurrence:<n>` — the total is
|
|
9
|
+
* part of the id on purpose, so the moment a document gains a second identical
|
|
10
|
+
* block every previously saved id stops matching and the range fails closed
|
|
11
|
+
* instead of painting the wrong text.
|
|
12
|
+
*
|
|
13
|
+
* Local highlights (`highlights.ts`) and hosted review threads (`collab.ts`) both
|
|
14
|
+
* consume this; neither owns a second selector format.
|
|
15
|
+
*/
|
|
16
|
+
export declare const READER_ANCHORS_SCRIPT = "<script>\n(() => {\n const root = document.querySelector('.artifact-body');\n if (!root) return;\n\n const selector = 'h2,h3,p,li,blockquote,pre,figcaption,td,th,dd';\n const hash = (value) => {\n let result = 2166136261;\n for (let index = 0; index < value.length; index += 1) { result ^= value.charCodeAt(index); result = Math.imul(result, 16777619); }\n return (result >>> 0).toString(36);\n };\n const anchors = [...root.querySelectorAll(selector)].filter((element) => !element.parentElement.closest(selector));\n const bases = [];\n let section = 'document';\n for (const anchor of anchors) {\n if ((anchor.tagName === 'H2' || anchor.tagName === 'H3') && anchor.id) section = anchor.id;\n anchor.dataset.readerSection = section;\n const text = (anchor.textContent || '').replace(/\\s+/g, ' ').trim();\n bases.push(anchor.id ? 'id:' + anchor.id : section + ':' + anchor.tagName.toLowerCase() + ':' + hash(text));\n }\n const totals = new Map();\n for (const base of bases) totals.set(base, (totals.get(base) || 0) + 1);\n const occurrences = new Map();\n for (let index = 0; index < anchors.length; index += 1) {\n const anchor = anchors[index];\n const base = bases[index];\n const occurrence = (occurrences.get(base) || 0) + 1;\n occurrences.set(base, occurrence);\n anchor.dataset.readerAnchor = base.startsWith('id:') ? base : base + ':count:' + totals.get(base) + ':occurrence:' + occurrence;\n }\n const byId = new Map(anchors.map((anchor) => [anchor.dataset.readerAnchor, anchor]));\n\n const textLength = (anchor) => (anchor.textContent || '').length;\n const normalizeQuote = (value) => String(value == null ? '' : value).replace(/\\s+/g, ' ').trim();\n const offsetAt = (anchor, node, offset) => {\n const range = document.createRange(); range.selectNodeContents(anchor);\n try { range.setEnd(node, offset); } catch { return null; }\n return range.toString().length;\n };\n const pointAt = (anchor, offset) => {\n const walker = document.createTreeWalker(anchor, NodeFilter.SHOW_TEXT);\n let remaining = offset; let node = walker.nextNode(); let last = null;\n while (node) {\n if (remaining <= node.data.length) return [node, remaining];\n remaining -= node.data.length; last = node; node = walker.nextNode();\n }\n return last ? [last, last.data.length] : [anchor, 0];\n };\n\n // Both endpoints must name a block that still exists exactly once, and they\n // must still be in document order. Anything else returns null: the caller has\n // to leave the text alone rather than guess.\n const resolve = (record) => {\n if (!record || !record.start || !record.end) return null;\n const first = byId.get(record.start.anchor); const last = byId.get(record.end.anchor);\n if (!first || !last) return null;\n const firstIndex = anchors.indexOf(first); const lastIndex = anchors.indexOf(last);\n if (firstIndex < 0 || lastIndex < firstIndex) return null;\n return { firstIndex, lastIndex };\n };\n\n // The text that currently sits where the record points, so a caller can\n // require an exact quote match before painting anything.\n const quoteAt = (record) => {\n const resolved = resolve(record); if (!resolved) return null;\n const [startNode, startOffset] = pointAt(anchors[resolved.firstIndex], record.start.offset);\n const [endNode, endOffset] = pointAt(anchors[resolved.lastIndex], record.end.offset);\n const range = document.createRange();\n try { range.setStart(startNode, startOffset); range.setEnd(endNode, endOffset); } catch { return null; }\n return range.toString();\n };\n\n const wrap = (anchor, start, end, make) => {\n if (end <= start) return;\n const nodes = []; const walker = document.createTreeWalker(anchor, NodeFilter.SHOW_TEXT); let cursor = 0; let node = walker.nextNode();\n while (node) {\n const nodeStart = cursor; const nodeEnd = cursor + node.data.length;\n if (nodeEnd > start && nodeStart < end) nodes.push([node, Math.max(0, start - nodeStart), Math.min(node.data.length, end - nodeStart)]);\n cursor = nodeEnd; node = walker.nextNode();\n }\n for (let index = nodes.length - 1; index >= 0; index -= 1) {\n const [textNode, from, to] = nodes[index]; if (to <= from) continue;\n const range = document.createRange(); range.setStart(textNode, from); range.setEnd(textNode, to);\n range.surroundContents(make());\n }\n };\n\n const paint = (record, make) => {\n const resolved = resolve(record); if (!resolved) return false;\n for (let index = resolved.firstIndex; index <= resolved.lastIndex; index += 1) {\n wrap(\n anchors[index],\n index === resolved.firstIndex ? record.start.offset : 0,\n index === resolved.lastIndex ? record.end.offset : textLength(anchors[index]),\n make,\n );\n }\n return true;\n };\n\n const clearMarks = (markSelector) => {\n for (const mark of root.querySelectorAll(markSelector)) mark.replaceWith(...mark.childNodes);\n for (const anchor of anchors) anchor.normalize();\n };\n\n // A live selection turned into the stored vocabulary, or null when it does not\n // land inside the artifact body.\n const selectionRecord = () => {\n const selection = getSelection(); if (!selection || selection.isCollapsed || !selection.rangeCount) return null;\n const range = selection.getRangeAt(0);\n const startAnchor = range.startContainer.parentElement?.closest('[data-reader-anchor]');\n const endAnchor = range.endContainer.parentElement?.closest('[data-reader-anchor]');\n if (!startAnchor || !endAnchor || !root.contains(startAnchor) || !root.contains(endAnchor)) return null;\n const startOffset = offsetAt(startAnchor, range.startContainer, range.startOffset);\n const endOffset = offsetAt(endAnchor, range.endContainer, range.endOffset);\n if (startOffset === null || endOffset === null) return null;\n const startIndex = anchors.indexOf(startAnchor); const endIndex = anchors.indexOf(endAnchor);\n if (startIndex > endIndex || (startIndex === endIndex && startOffset >= endOffset)) return null;\n return {\n quote: selection.toString(),\n section: startAnchor.dataset.readerSection || 'document',\n start: { anchor: startAnchor.dataset.readerAnchor, offset: startOffset },\n end: { anchor: endAnchor.dataset.readerAnchor, offset: endOffset },\n rect: range.getBoundingClientRect(),\n };\n };\n\n window.__artifactsReader = { root, anchors, byId, textLength, normalizeQuote, offsetAt, resolve, quoteAt, paint, clearMarks, selectionRecord };\n})();\n</script>";
|