@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,146 @@
|
|
|
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 const READER_ANCHORS_SCRIPT = `<script>
|
|
17
|
+
(() => {
|
|
18
|
+
const root = document.querySelector('.artifact-body');
|
|
19
|
+
if (!root) return;
|
|
20
|
+
|
|
21
|
+
const selector = 'h2,h3,p,li,blockquote,pre,figcaption,td,th,dd';
|
|
22
|
+
const hash = (value) => {
|
|
23
|
+
let result = 2166136261;
|
|
24
|
+
for (let index = 0; index < value.length; index += 1) { result ^= value.charCodeAt(index); result = Math.imul(result, 16777619); }
|
|
25
|
+
return (result >>> 0).toString(36);
|
|
26
|
+
};
|
|
27
|
+
const anchors = [...root.querySelectorAll(selector)].filter((element) => !element.parentElement.closest(selector));
|
|
28
|
+
const bases = [];
|
|
29
|
+
let section = 'document';
|
|
30
|
+
for (const anchor of anchors) {
|
|
31
|
+
if ((anchor.tagName === 'H2' || anchor.tagName === 'H3') && anchor.id) section = anchor.id;
|
|
32
|
+
anchor.dataset.readerSection = section;
|
|
33
|
+
const text = (anchor.textContent || '').replace(/\\s+/g, ' ').trim();
|
|
34
|
+
bases.push(anchor.id ? 'id:' + anchor.id : section + ':' + anchor.tagName.toLowerCase() + ':' + hash(text));
|
|
35
|
+
}
|
|
36
|
+
const totals = new Map();
|
|
37
|
+
for (const base of bases) totals.set(base, (totals.get(base) || 0) + 1);
|
|
38
|
+
const occurrences = new Map();
|
|
39
|
+
for (let index = 0; index < anchors.length; index += 1) {
|
|
40
|
+
const anchor = anchors[index];
|
|
41
|
+
const base = bases[index];
|
|
42
|
+
const occurrence = (occurrences.get(base) || 0) + 1;
|
|
43
|
+
occurrences.set(base, occurrence);
|
|
44
|
+
anchor.dataset.readerAnchor = base.startsWith('id:') ? base : base + ':count:' + totals.get(base) + ':occurrence:' + occurrence;
|
|
45
|
+
}
|
|
46
|
+
const byId = new Map(anchors.map((anchor) => [anchor.dataset.readerAnchor, anchor]));
|
|
47
|
+
|
|
48
|
+
const textLength = (anchor) => (anchor.textContent || '').length;
|
|
49
|
+
const normalizeQuote = (value) => String(value == null ? '' : value).replace(/\\s+/g, ' ').trim();
|
|
50
|
+
const offsetAt = (anchor, node, offset) => {
|
|
51
|
+
const range = document.createRange(); range.selectNodeContents(anchor);
|
|
52
|
+
try { range.setEnd(node, offset); } catch { return null; }
|
|
53
|
+
return range.toString().length;
|
|
54
|
+
};
|
|
55
|
+
const pointAt = (anchor, offset) => {
|
|
56
|
+
const walker = document.createTreeWalker(anchor, NodeFilter.SHOW_TEXT);
|
|
57
|
+
let remaining = offset; let node = walker.nextNode(); let last = null;
|
|
58
|
+
while (node) {
|
|
59
|
+
if (remaining <= node.data.length) return [node, remaining];
|
|
60
|
+
remaining -= node.data.length; last = node; node = walker.nextNode();
|
|
61
|
+
}
|
|
62
|
+
return last ? [last, last.data.length] : [anchor, 0];
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// Both endpoints must name a block that still exists exactly once, and they
|
|
66
|
+
// must still be in document order. Anything else returns null: the caller has
|
|
67
|
+
// to leave the text alone rather than guess.
|
|
68
|
+
const resolve = (record) => {
|
|
69
|
+
if (!record || !record.start || !record.end) return null;
|
|
70
|
+
const first = byId.get(record.start.anchor); const last = byId.get(record.end.anchor);
|
|
71
|
+
if (!first || !last) return null;
|
|
72
|
+
const firstIndex = anchors.indexOf(first); const lastIndex = anchors.indexOf(last);
|
|
73
|
+
if (firstIndex < 0 || lastIndex < firstIndex) return null;
|
|
74
|
+
return { firstIndex, lastIndex };
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
// The text that currently sits where the record points, so a caller can
|
|
78
|
+
// require an exact quote match before painting anything.
|
|
79
|
+
const quoteAt = (record) => {
|
|
80
|
+
const resolved = resolve(record); if (!resolved) return null;
|
|
81
|
+
const [startNode, startOffset] = pointAt(anchors[resolved.firstIndex], record.start.offset);
|
|
82
|
+
const [endNode, endOffset] = pointAt(anchors[resolved.lastIndex], record.end.offset);
|
|
83
|
+
const range = document.createRange();
|
|
84
|
+
try { range.setStart(startNode, startOffset); range.setEnd(endNode, endOffset); } catch { return null; }
|
|
85
|
+
return range.toString();
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const wrap = (anchor, start, end, make) => {
|
|
89
|
+
if (end <= start) return;
|
|
90
|
+
const nodes = []; const walker = document.createTreeWalker(anchor, NodeFilter.SHOW_TEXT); let cursor = 0; let node = walker.nextNode();
|
|
91
|
+
while (node) {
|
|
92
|
+
const nodeStart = cursor; const nodeEnd = cursor + node.data.length;
|
|
93
|
+
if (nodeEnd > start && nodeStart < end) nodes.push([node, Math.max(0, start - nodeStart), Math.min(node.data.length, end - nodeStart)]);
|
|
94
|
+
cursor = nodeEnd; node = walker.nextNode();
|
|
95
|
+
}
|
|
96
|
+
for (let index = nodes.length - 1; index >= 0; index -= 1) {
|
|
97
|
+
const [textNode, from, to] = nodes[index]; if (to <= from) continue;
|
|
98
|
+
const range = document.createRange(); range.setStart(textNode, from); range.setEnd(textNode, to);
|
|
99
|
+
range.surroundContents(make());
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const paint = (record, make) => {
|
|
104
|
+
const resolved = resolve(record); if (!resolved) return false;
|
|
105
|
+
for (let index = resolved.firstIndex; index <= resolved.lastIndex; index += 1) {
|
|
106
|
+
wrap(
|
|
107
|
+
anchors[index],
|
|
108
|
+
index === resolved.firstIndex ? record.start.offset : 0,
|
|
109
|
+
index === resolved.lastIndex ? record.end.offset : textLength(anchors[index]),
|
|
110
|
+
make,
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
return true;
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const clearMarks = (markSelector) => {
|
|
117
|
+
for (const mark of root.querySelectorAll(markSelector)) mark.replaceWith(...mark.childNodes);
|
|
118
|
+
for (const anchor of anchors) anchor.normalize();
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
// A live selection turned into the stored vocabulary, or null when it does not
|
|
122
|
+
// land inside the artifact body.
|
|
123
|
+
const selectionRecord = () => {
|
|
124
|
+
const selection = getSelection(); if (!selection || selection.isCollapsed || !selection.rangeCount) return null;
|
|
125
|
+
const range = selection.getRangeAt(0);
|
|
126
|
+
const startAnchor = range.startContainer.parentElement?.closest('[data-reader-anchor]');
|
|
127
|
+
const endAnchor = range.endContainer.parentElement?.closest('[data-reader-anchor]');
|
|
128
|
+
if (!startAnchor || !endAnchor || !root.contains(startAnchor) || !root.contains(endAnchor)) return null;
|
|
129
|
+
const startOffset = offsetAt(startAnchor, range.startContainer, range.startOffset);
|
|
130
|
+
const endOffset = offsetAt(endAnchor, range.endContainer, range.endOffset);
|
|
131
|
+
if (startOffset === null || endOffset === null) return null;
|
|
132
|
+
const startIndex = anchors.indexOf(startAnchor); const endIndex = anchors.indexOf(endAnchor);
|
|
133
|
+
if (startIndex > endIndex || (startIndex === endIndex && startOffset >= endOffset)) return null;
|
|
134
|
+
return {
|
|
135
|
+
quote: selection.toString(),
|
|
136
|
+
section: startAnchor.dataset.readerSection || 'document',
|
|
137
|
+
start: { anchor: startAnchor.dataset.readerAnchor, offset: startOffset },
|
|
138
|
+
end: { anchor: endAnchor.dataset.readerAnchor, offset: endOffset },
|
|
139
|
+
rect: range.getBoundingClientRect(),
|
|
140
|
+
};
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
window.__artifactsReader = { root, anchors, byId, textLength, normalizeQuote, offsetAt, resolve, quoteAt, paint, clearMarks, selectionRecord };
|
|
144
|
+
})();
|
|
145
|
+
</script>`;
|
|
146
|
+
//# sourceMappingURL=reader-anchors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reader-anchors.js","sourceRoot":"","sources":["../../src/lib/reader-anchors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;UAiI3B,CAAC"}
|