@phnx-labs/artifacts-cli 0.3.1 → 0.3.3
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 +62 -0
- package/README.md +39 -1
- package/dist/cli.js +6 -42
- package/dist/cli.js.map +1 -1
- package/dist/commands/new.d.ts +9 -0
- package/dist/commands/new.js +62 -0
- package/dist/commands/new.js.map +1 -0
- package/dist/lib/charts.d.ts +14 -0
- package/dist/lib/charts.js +68 -0
- package/dist/lib/charts.js.map +1 -0
- package/dist/lib/highlights.d.ts +1 -0
- package/dist/lib/highlights.js +129 -0
- package/dist/lib/highlights.js.map +1 -0
- package/dist/lib/html.d.ts +8 -0
- package/dist/lib/html.js +18 -0
- package/dist/lib/html.js.map +1 -0
- package/dist/lib/icons.d.ts +11 -4
- package/dist/lib/icons.js +10 -8
- package/dist/lib/icons.js.map +1 -1
- package/dist/lib/links.d.ts +28 -0
- package/dist/lib/links.js +150 -0
- package/dist/lib/links.js.map +1 -1
- package/dist/lib/render.js +194 -26
- package/dist/lib/render.js.map +1 -1
- package/dist/lib/validate.js +62 -2
- package/dist/lib/validate.js.map +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,67 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## Unreleased
|
|
4
|
+
|
|
5
|
+
## 0.3.3 — 2026-08-15
|
|
6
|
+
|
|
7
|
+
- **Theme-aware figures and data-driven bar charts.** Rendered pages expose
|
|
8
|
+
`--artifact-fg`, `--artifact-muted`, and `--artifact-accent` for inline SVG;
|
|
9
|
+
the always-dark diagram surface now scopes those colors so its caption stays
|
|
10
|
+
readable in light and dark themes. A fenced `bar-chart` component accepts a
|
|
11
|
+
two-column Markdown table and emits accessible, automatically scaled SVG with
|
|
12
|
+
no author-supplied coordinates or hex colors.
|
|
13
|
+
- **Authoring diagnostics are shorter and more actionable.** Repeated embedded
|
|
14
|
+
HTML violations collapse by root cause with occurrence counts, unsupported
|
|
15
|
+
SVG classes point to `fill=`, `font-size=`, and `font-family=`, and
|
|
16
|
+
`stroke-dashoffset` is accepted as an SVG animation target. Frontmatter
|
|
17
|
+
summaries render safe inline Markdown, including code spans, and
|
|
18
|
+
`artifacts new <kind> --blank --stdout` prints a required-section skeleton
|
|
19
|
+
without writing a file.
|
|
20
|
+
|
|
21
|
+
- **Reader highlights persist in each browser.** Selecting text in a rendered
|
|
22
|
+
artifact saves a stable section-and-block-relative range under the artifact's
|
|
23
|
+
existing URL-and-title identity. Highlights return after a reload or reopened
|
|
24
|
+
tab, support overlaps without changing the document text, and disappear when
|
|
25
|
+
clicked. Storage failures stay silent, and the themed mark color remains
|
|
26
|
+
readable in light and dark mode without adding an authored script/style surface.
|
|
27
|
+
- **Body links render as link pills.** Every outbound `http(s)` link an agent writes
|
|
28
|
+
as plain Markdown — `[text](url)` or a pasted bare URL — now renders with the
|
|
29
|
+
source's brand mark (GitHub, Linear, Jira, Notion, GitLab, Bitbucket, Asana,
|
|
30
|
+
Trello, Slack, Figma) or its favicon, in a bordered, clickable pill, so sources
|
|
31
|
+
are identifiable at a glance without an agent hand-writing any HTML. A bare URL
|
|
32
|
+
is labelled `PR #1914` / `RUSH-2513` / the host instead of printed in full, using
|
|
33
|
+
the same short form as the frontmatter `links` chips. Links in headings, `code`,
|
|
34
|
+
and `<pre>`, in-page anchors, `mailto:`/`tel:`, relative paths, and image links
|
|
35
|
+
stay plain anchors. The pass runs after the body sanitizer, so its generated
|
|
36
|
+
markup survives the tag and class allowlist.
|
|
37
|
+
- **A `References` appendix is generated automatically.** Every outbound link in
|
|
38
|
+
the body is collected into a numbered section at the end — deduped, in order of
|
|
39
|
+
first appearance, each row carrying the source's mark, label, and host, reusing
|
|
40
|
+
the same short label as the pill. An author-written `References` / `Sources` /
|
|
41
|
+
`Bibliography` / `Citations` heading suppresses it.
|
|
42
|
+
- **The table of contents is now a contents rail.** It lists subsections as well
|
|
43
|
+
as sections, and above 1380px becomes a fixed side rail that tracks the section
|
|
44
|
+
being read. Heading ids and the rail are now produced by one pass over the
|
|
45
|
+
rendered HTML, which fixes three bugs: two subsections sharing a title used to
|
|
46
|
+
get one shared id (so the rail's second entry jumped to the first heading and
|
|
47
|
+
scroll-spy could never select it), a heading containing a link or an `&`
|
|
48
|
+
slugified differently in the rail than in its own id, and a `##` line inside a
|
|
49
|
+
fenced code block was treated as a heading. A heading inside an embedded panel
|
|
50
|
+
or figure still gets an id but is excluded from the rail.
|
|
51
|
+
- **Artifacts keep a local read count**, shown in the document header, stored per
|
|
52
|
+
browser in `localStorage` and scoped to the artifact's own URL and title.
|
|
53
|
+
- **`summary` and `links` are soft-required.** Their absence is now a warning, not
|
|
54
|
+
an error: the artifact still compiles, but the author is reminded that a TL;DR
|
|
55
|
+
and the related tickets/PRs/docs are what give a reader context. An overlong
|
|
56
|
+
summary is flagged too.
|
|
57
|
+
|
|
58
|
+
## 0.3.2 — 2026-08-10
|
|
59
|
+
|
|
60
|
+
- `artifacts --version` now reads the version from `package.json` at runtime instead
|
|
61
|
+
of the hardcoded string compiled into `dist/cli.js`. Previously every release
|
|
62
|
+
required a separate source edit to keep the reported version in sync; the 0.3.1
|
|
63
|
+
release shipped with `.version('0.3.0')` still embedded in the binary.
|
|
64
|
+
|
|
3
65
|
## 0.3.1 — 2026-08-10
|
|
4
66
|
|
|
5
67
|
- The **`artifact-behavior` figure class** is now supported. A plan containing an
|
package/README.md
CHANGED
|
@@ -79,7 +79,7 @@ Four commands. Everything else is a flag on one of them.
|
|
|
79
79
|
|
|
80
80
|
| Command | Purpose |
|
|
81
81
|
| --- | --- |
|
|
82
|
-
| `artifacts new <plan\|report\|visual\|design> [--out file] [--blank]` | Scaffolds an artifact and pre-fills detectable provenance. `--blank` leaves every field empty; `design` writes a starter `DESIGN.md`. |
|
|
82
|
+
| `artifacts new <plan\|report\|visual\|design> [--out file] [--blank] [--stdout]` | Scaffolds an artifact and pre-fills detectable provenance. `--blank` leaves every field empty; `--stdout` prints the skeleton without writing; `design` writes a starter `DESIGN.md`. |
|
|
83
83
|
| `artifacts check file.md` | Validates frontmatter metadata, required sections, asset paths, and embedded HTML/SVG safety. |
|
|
84
84
|
| `artifacts render file.md [--out file] [--open]` | Renders Markdown to self-contained HTML next to the source. `--open` shows it in your default browser. Embeds the Markdown source for recovery. |
|
|
85
85
|
| `artifacts share file.md` | Renders Markdown when needed and publishes through the configured R2/Worker endpoint. |
|
|
@@ -184,6 +184,44 @@ The committed [representative plan](examples/plan.md) measures:
|
|
|
184
184
|
|
|
185
185
|
That example is an **82.1% authoring-token reduction** when the alternative is having the agent emit the same self-contained HTML. It includes complete metadata, ordinary Markdown, a table, embedded layout HTML, inline SVG, theme CSS, responsive rules, print rules, and the theme toggle. Run `artifacts estimate examples/plan.md` to reproduce it — the command is absent from `--help` but still runs.
|
|
186
186
|
|
|
187
|
+
## Links
|
|
188
|
+
|
|
189
|
+
Write links as ordinary Markdown. Every outbound `http(s)` link in the body renders as a **link pill**: the source's brand mark (GitHub, Linear, Jira, Notion, GitLab, Bitbucket, Asana, Trello, Slack, Figma) or its favicon, followed by the link text, in a bordered, clickable container. No HTML to hand-write.
|
|
190
|
+
|
|
191
|
+
```markdown
|
|
192
|
+
See [the merge guard](https://github.com/phnx-labs/agents-cli/pull/1914) for context.
|
|
193
|
+
Ticket: [RUSH-2513](https://linear.app/phnx/issue/RUSH-2513/version-drift)
|
|
194
|
+
https://github.com/phnx-labs/artifacts-cli
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
A pasted bare URL is labelled instead of printed in full — `PR #1914`, `RUSH-2513`, or the host — the same short form the frontmatter `links` chips use.
|
|
198
|
+
|
|
199
|
+
These stay plain anchors, because a pill would fight the surrounding element or misrepresent the link:
|
|
200
|
+
|
|
201
|
+
| Link | Why it stays plain |
|
|
202
|
+
| --- | --- |
|
|
203
|
+
| In a heading, `code`, or a `<pre>` block | The pill would break the type. |
|
|
204
|
+
| In-page anchor (`#section`) | Not an outbound source. |
|
|
205
|
+
| `mailto:` and `tel:` | Not a web source. |
|
|
206
|
+
| Relative path (`./notes.html`) | Not an outbound source. |
|
|
207
|
+
| An image wrapped in a link | The image is the content, not a citation. |
|
|
208
|
+
|
|
209
|
+
Brand marks are bundled, so a known host needs no network. Unknown hosts fall back to a remote favicon that removes itself if it cannot load.
|
|
210
|
+
|
|
211
|
+
## Reading Surfaces
|
|
212
|
+
|
|
213
|
+
Four reader affordances are generated from the artifact itself. An agent writes nothing extra for any of them.
|
|
214
|
+
|
|
215
|
+
**References appendix.** Every outbound link in the body is collected into a numbered `References` section at the end — deduped, in order of first appearance, each row carrying the source's mark, its label, and its host. A bare URL lists under its short label (`PR #1914`), not the raw address. Writing your own `References`, `Sources`, `Bibliography`, or `Citations` heading suppresses the generated one.
|
|
216
|
+
|
|
217
|
+
**Contents rail.** The table of contents lists sections and their subsections. At viewports wider than 1380px it becomes a fixed side rail that tracks the section you are reading; below that it stays inline at the top with subsections hidden. Heading ids are unique even when two subsections share a title, so every rail entry addresses its own heading. A heading inside an embedded panel or figure is that block's caption and is deliberately left out of the rail.
|
|
218
|
+
|
|
219
|
+
**Read count.** Each artifact keeps a per-browser read count in `localStorage`, shown in the document header (`first read`, then `2 reads`). It is scoped to the artifact's own URL and title, so two artifacts published under the same title do not share a counter. Nothing is sent anywhere.
|
|
220
|
+
|
|
221
|
+
**Highlights.** Select text in the artifact body to save a highlight in this browser. Highlights survive reloads and reopening the page; click any highlighted text to remove that selection. They use the same URL-and-title artifact identity as the read count, stay on the reader's device, and need no server or account.
|
|
222
|
+
|
|
223
|
+
Themes already follow the reader's system preference and switch live when the OS theme changes, unless the reader picks one explicitly with the in-page toggle.
|
|
224
|
+
|
|
187
225
|
## Embedded HTML And SVG
|
|
188
226
|
|
|
189
227
|
Use Markdown for prose, lists, tables, and code. When Markdown cannot express a figure or comparison, write semantic HTML directly in the Markdown file. The renderer sanitizes it, applies the active `DESIGN.md` theme, and keeps the output responsive.
|
package/dist/cli.js
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import {
|
|
2
|
+
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { fileURLToPath } from 'node:url';
|
|
3
4
|
import { dirname, join, resolve } from 'node:path';
|
|
4
5
|
import { spawn } from 'node:child_process';
|
|
5
6
|
import { Command, Option } from 'commander';
|
|
6
7
|
import chalk from 'chalk';
|
|
8
|
+
import { addNewCommand } from './commands/new.js';
|
|
7
9
|
import { exportPdf, parsePdfLayout } from './lib/pdf.js';
|
|
8
10
|
import { compileArtifact, decompileHtml } from './lib/render.js';
|
|
9
11
|
import { estimateArtifactTokens } from './lib/tokens.js';
|
|
10
|
-
import { detectArtifactContext, STARTER_DESIGN, templateFor } from './lib/templates.js';
|
|
11
12
|
import { validateMarkdown } from './lib/validate.js';
|
|
12
13
|
import { shareAnalytics, shareDelete, shareJoin, sharePublish, shareSetup, shareStatus } from './commands/share.js';
|
|
14
|
+
const { version } = JSON.parse(readFileSync(join(dirname(fileURLToPath(import.meta.url)), '../package.json'), 'utf-8'));
|
|
13
15
|
const program = new Command();
|
|
14
16
|
function printDiagnostics(diagnostics) {
|
|
15
17
|
for (const diagnostic of diagnostics) {
|
|
@@ -34,7 +36,7 @@ function openPath(path) {
|
|
|
34
36
|
program
|
|
35
37
|
.name('artifacts')
|
|
36
38
|
.description('Render Markdown artifacts into polished HTML and shareable static links.')
|
|
37
|
-
.version(
|
|
39
|
+
.version(version)
|
|
38
40
|
.addHelpText('after', `
|
|
39
41
|
Happy path:
|
|
40
42
|
1. artifacts new plan --out <file>.md # frontmatter needs only kind + title;
|
|
@@ -44,45 +46,7 @@ Happy path:
|
|
|
44
46
|
|
|
45
47
|
Run artifacts <command> --help for details.
|
|
46
48
|
`);
|
|
47
|
-
program
|
|
48
|
-
.command('new')
|
|
49
|
-
.description('Scaffold a Markdown artifact: plan, report, visual, or design.')
|
|
50
|
-
.argument('<kind>', 'plan, report, visual, or design')
|
|
51
|
-
.option('--out <file>', 'output file')
|
|
52
|
-
.option('--blank', 'write an unfilled template instead of a context-filled artifact')
|
|
53
|
-
.option('--force', 'overwrite the default output file if it already exists')
|
|
54
|
-
.addHelpText('after', `
|
|
55
|
-
Examples:
|
|
56
|
-
$ artifacts new plan --out plan.md # provenance filled from git and the agent env
|
|
57
|
-
$ artifacts new plan --blank # plan.template.md, every field left empty
|
|
58
|
-
$ artifacts new design # DESIGN.md, project-wide branding
|
|
59
|
-
`)
|
|
60
|
-
.action((kind, opts) => {
|
|
61
|
-
// A defaulted filename is a guess, so it must never clobber authored work.
|
|
62
|
-
// An explicit --out is the caller naming the target, which stays as-is.
|
|
63
|
-
const writeArtifact = (out, content, defaulted) => {
|
|
64
|
-
if (defaulted && !opts.force && existsSync(out)) {
|
|
65
|
-
throw new Error(`${out} already exists; pass --out <file> to write elsewhere, or --force to overwrite.`);
|
|
66
|
-
}
|
|
67
|
-
writeFileSync(out, content);
|
|
68
|
-
console.log(`${chalk.bold('wrote')} ${out}`);
|
|
69
|
-
};
|
|
70
|
-
// design is a different file shape entirely — its own frontmatter, no
|
|
71
|
-
// required sections — so it never goes through templateFor().
|
|
72
|
-
if (kind === 'design') {
|
|
73
|
-
if (opts.blank)
|
|
74
|
-
throw new Error('--blank does not apply to design; DESIGN.md has no context-filled variant.');
|
|
75
|
-
writeArtifact(opts.out || 'DESIGN.md', STARTER_DESIGN, !opts.out);
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
if (!['plan', 'report', 'visual'].includes(kind)) {
|
|
79
|
-
throw new Error(`Unknown artifact kind: ${kind}; use plan, report, visual, or design.`);
|
|
80
|
-
}
|
|
81
|
-
const artifactKind = kind;
|
|
82
|
-
const out = opts.out || (opts.blank ? `${artifactKind}.template.md` : `${artifactKind}.md`);
|
|
83
|
-
const content = opts.blank ? templateFor(artifactKind) : templateFor(artifactKind, detectArtifactContext());
|
|
84
|
-
writeArtifact(out, content, !opts.out);
|
|
85
|
-
});
|
|
49
|
+
addNewCommand(program);
|
|
86
50
|
program
|
|
87
51
|
.command('check')
|
|
88
52
|
.alias('validate')
|
package/dist/cli.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACjE,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AACzC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACnD,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,WAAW,CAAC;AAC5C,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,cAAc,EAAkB,MAAM,cAAc,CAAC;AACzE,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AACjE,OAAO,EAAE,sBAAsB,EAAE,MAAM,iBAAiB,CAAC;AACzD,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AACrD,OAAO,EAAE,cAAc,EAAE,WAAW,EAAE,SAAS,EAAE,YAAY,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAEpH,MAAM,EAAE,OAAO,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,iBAAiB,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC;A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Q,CAAC,CAAC,WAAW,CAAC,4CAA4C,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;AAE5H,OAAO,CAAC,UAAU,EAAE,CAAC,KAAK,CAAC,CAAC,KAAc,EAAE,EAAE;IAC5C,OAAO,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,IAAI,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;IACjG,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAC;AACvB,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Command } from 'commander';
|
|
2
|
+
export type NewOptions = {
|
|
3
|
+
out?: string;
|
|
4
|
+
blank?: boolean;
|
|
5
|
+
force?: boolean;
|
|
6
|
+
stdout?: boolean;
|
|
7
|
+
};
|
|
8
|
+
export declare function runNewCommand(kind: string, opts: NewOptions): void;
|
|
9
|
+
export declare function addNewCommand(program: Command): void;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { existsSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import chalk from 'chalk';
|
|
3
|
+
import { detectArtifactContext, STARTER_DESIGN, templateFor } from '../lib/templates.js';
|
|
4
|
+
export function runNewCommand(kind, opts) {
|
|
5
|
+
if (opts.stdout && !opts.blank)
|
|
6
|
+
throw new Error('--stdout requires --blank.');
|
|
7
|
+
if (opts.stdout && opts.out)
|
|
8
|
+
throw new Error('--stdout cannot be combined with --out.');
|
|
9
|
+
if (opts.stdout && opts.force)
|
|
10
|
+
throw new Error('--stdout cannot be combined with --force.');
|
|
11
|
+
// A defaulted filename is a guess, so it must never clobber authored work.
|
|
12
|
+
// An explicit --out is the caller naming the target, which stays as-is.
|
|
13
|
+
const writeArtifact = (out, content, defaulted) => {
|
|
14
|
+
if (defaulted && !opts.force && existsSync(out)) {
|
|
15
|
+
throw new Error(`${out} already exists; pass --out <file> to write elsewhere, or --force to overwrite.`);
|
|
16
|
+
}
|
|
17
|
+
writeFileSync(out, content);
|
|
18
|
+
console.log(`${chalk.bold('wrote')} ${out}`);
|
|
19
|
+
};
|
|
20
|
+
// design is a different file shape entirely: its own frontmatter, no
|
|
21
|
+
// required sections, so it never goes through templateFor().
|
|
22
|
+
if (kind === 'design') {
|
|
23
|
+
if (opts.blank)
|
|
24
|
+
throw new Error('--blank does not apply to design; DESIGN.md has no context-filled variant.');
|
|
25
|
+
if (opts.stdout) {
|
|
26
|
+
process.stdout.write(STARTER_DESIGN);
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
writeArtifact(opts.out || 'DESIGN.md', STARTER_DESIGN, !opts.out);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
if (!['plan', 'report', 'visual'].includes(kind)) {
|
|
33
|
+
throw new Error(`Unknown artifact kind: ${kind}; use plan, report, visual, or design.`);
|
|
34
|
+
}
|
|
35
|
+
const artifactKind = kind;
|
|
36
|
+
const content = opts.blank ? templateFor(artifactKind) : templateFor(artifactKind, detectArtifactContext());
|
|
37
|
+
if (opts.stdout) {
|
|
38
|
+
process.stdout.write(content);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const out = opts.out || (opts.blank ? `${artifactKind}.template.md` : `${artifactKind}.md`);
|
|
42
|
+
writeArtifact(out, content, !opts.out);
|
|
43
|
+
}
|
|
44
|
+
export function addNewCommand(program) {
|
|
45
|
+
program
|
|
46
|
+
.command('new')
|
|
47
|
+
.description('Scaffold a Markdown artifact: plan, report, visual, or design.')
|
|
48
|
+
.argument('<kind>', 'plan, report, visual, or design')
|
|
49
|
+
.option('--out <file>', 'output file')
|
|
50
|
+
.option('--blank', 'write an unfilled template instead of a context-filled artifact')
|
|
51
|
+
.option('--stdout', 'print the artifact to stdout without writing a file')
|
|
52
|
+
.option('--force', 'overwrite the default output file if it already exists')
|
|
53
|
+
.addHelpText('after', `
|
|
54
|
+
Examples:
|
|
55
|
+
$ artifacts new plan --out plan.md # provenance filled from git and the agent env
|
|
56
|
+
$ artifacts new plan --blank # plan.template.md, every field left empty
|
|
57
|
+
$ artifacts new plan --blank --stdout # print the required section skeleton
|
|
58
|
+
$ artifacts new design # DESIGN.md, project-wide branding
|
|
59
|
+
`)
|
|
60
|
+
.action(runNewCommand);
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=new.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"new.js","sourceRoot":"","sources":["../../src/commands/new.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAEpD,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,EAAE,qBAAqB,EAAE,cAAc,EAAE,WAAW,EAAqB,MAAM,qBAAqB,CAAC;AAS5G,MAAM,UAAU,aAAa,CAAC,IAAY,EAAE,IAAgB;IAC1D,IAAI,IAAI,CAAC,MAAM,IAAI,CAAC,IAAI,CAAC,KAAK;QAAE,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;IAC9E,IAAI,IAAI,CAAC,MAAM,IAAI,IAAI,CAAC,GAAG;QAAE,MAAM,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAC;IACxF,IAAI,IAAI,CAAC,MAAM,IAAI,IAAI,CAAC,KAAK;QAAE,MAAM,IAAI,KAAK,CAAC,2CAA2C,CAAC,CAAC;IAE5F,2EAA2E;IAC3E,wEAAwE;IACxE,MAAM,aAAa,GAAG,CAAC,GAAW,EAAE,OAAe,EAAE,SAAkB,EAAQ,EAAE;QAC/E,IAAI,SAAS,IAAI,CAAC,IAAI,CAAC,KAAK,IAAI,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;YAChD,MAAM,IAAI,KAAK,CAAC,GAAG,GAAG,iFAAiF,CAAC,CAAC;QAC3G,CAA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IAC5F,aAAa,CAAC,GAAG,EAAE,OAAO,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACzC,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,OAAgB;IAC5C,OAAO;SACJ,OAAO,CAAC,KAAK,CAAC;SACd,WAAW,CAAC,gEAAgE,CAAC;SAC7E,QAAQ,CAAC,QAAQ,EAAE,iCAAiC,CAAC;SACrD,MAAM,CAAC,cAAc,EAAE,aAAa,CAAC;SACrC,MAAM,CAAC,SAAS,EAAE,iEAAiE,CAAC;SACpF,MAAM,CAAC,UAAU,EAAE,qDAAqD,CAAC;SACzE,MAAM,CAAC,SAAS,EAAE,wDAAwD,CAAC;SAC3E,WAAW,CAAC,OAAO,EAAE;;;;;;CAMzB,CAAC;SACG,MAAM,CAAC,aAAa,CAAC,CAAC;AAC3B,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface BarChartDatum {
|
|
2
|
+
label: string;
|
|
3
|
+
value: number;
|
|
4
|
+
displayValue: string;
|
|
5
|
+
}
|
|
6
|
+
export interface BarChartData {
|
|
7
|
+
labelHeading: string;
|
|
8
|
+
valueHeading: string;
|
|
9
|
+
rows: BarChartDatum[];
|
|
10
|
+
}
|
|
11
|
+
/** Parse a two-column Markdown table supplied by a `bar-chart` fence. */
|
|
12
|
+
export declare function parseBarChartTable(source: string): BarChartData | undefined;
|
|
13
|
+
/** Render a fenced Markdown data table as a theme-aware horizontal bar chart. */
|
|
14
|
+
export declare function renderBarChart(source: string): string;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { escapeHtml } from './html.js';
|
|
2
|
+
const CHART_WIDTH = 720;
|
|
3
|
+
const LABEL_WIDTH = 180;
|
|
4
|
+
const VALUE_WIDTH = 88;
|
|
5
|
+
const PLOT_X = 196;
|
|
6
|
+
const PLOT_WIDTH = CHART_WIDTH - PLOT_X - VALUE_WIDTH;
|
|
7
|
+
const ROW_HEIGHT = 42;
|
|
8
|
+
const CHART_TOP = 42;
|
|
9
|
+
const CHART_BOTTOM = 18;
|
|
10
|
+
function tableCells(line) {
|
|
11
|
+
const trimmed = line.trim().replace(/^\|/, '').replace(/\|$/, '');
|
|
12
|
+
return trimmed.split('|').map((cell) => cell.trim());
|
|
13
|
+
}
|
|
14
|
+
function isDivider(cells) {
|
|
15
|
+
return cells.length >= 2 && cells.every((cell) => /^:?-{3,}:?$/.test(cell));
|
|
16
|
+
}
|
|
17
|
+
function numericValue(value) {
|
|
18
|
+
const normalized = value.replace(/,/g, '').replace(/^[$£€¥]/, '').replace(/%$/, '').trim();
|
|
19
|
+
if (!/^(?:\d+(?:\.\d+)?|\.\d+)$/.test(normalized))
|
|
20
|
+
return undefined;
|
|
21
|
+
const parsed = Number(normalized);
|
|
22
|
+
return Number.isFinite(parsed) ? parsed : undefined;
|
|
23
|
+
}
|
|
24
|
+
/** Parse a two-column Markdown table supplied by a `bar-chart` fence. */
|
|
25
|
+
export function parseBarChartTable(source) {
|
|
26
|
+
const lines = source.split(/\r?\n/).map((line) => line.trim()).filter(Boolean);
|
|
27
|
+
if (lines.length < 3)
|
|
28
|
+
return undefined;
|
|
29
|
+
const header = tableCells(lines[0]);
|
|
30
|
+
const divider = tableCells(lines[1]);
|
|
31
|
+
if (header.length !== 2 || divider.length !== 2 || !isDivider(divider) || header.some((cell) => !cell)) {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
const rows = [];
|
|
35
|
+
for (const line of lines.slice(2)) {
|
|
36
|
+
const cells = tableCells(line);
|
|
37
|
+
if (cells.length !== 2 || !cells[0] || !cells[1])
|
|
38
|
+
return undefined;
|
|
39
|
+
const value = numericValue(cells[1]);
|
|
40
|
+
if (value === undefined)
|
|
41
|
+
return undefined;
|
|
42
|
+
rows.push({ label: cells[0], value, displayValue: cells[1] });
|
|
43
|
+
}
|
|
44
|
+
if (rows.length === 0)
|
|
45
|
+
return undefined;
|
|
46
|
+
return { labelHeading: header[0], valueHeading: header[1], rows };
|
|
47
|
+
}
|
|
48
|
+
/** Render a fenced Markdown data table as a theme-aware horizontal bar chart. */
|
|
49
|
+
export function renderBarChart(source) {
|
|
50
|
+
const chart = parseBarChartTable(source);
|
|
51
|
+
if (!chart)
|
|
52
|
+
return `<pre><code class="language-bar-chart">${escapeHtml(source)}</code></pre>`;
|
|
53
|
+
const height = CHART_TOP + chart.rows.length * ROW_HEIGHT + CHART_BOTTOM;
|
|
54
|
+
const maximum = Math.max(...chart.rows.map((row) => row.value));
|
|
55
|
+
const title = `${chart.valueHeading} by ${chart.labelHeading}`;
|
|
56
|
+
const rows = chart.rows.map((row, index) => {
|
|
57
|
+
const y = CHART_TOP + index * ROW_HEIGHT;
|
|
58
|
+
const barWidth = maximum === 0 ? 0 : (row.value / maximum) * PLOT_WIDTH;
|
|
59
|
+
return [
|
|
60
|
+
`<text x="${LABEL_WIDTH}" y="${y + 22}" text-anchor="end" fill="var(--artifact-fg)">${escapeHtml(row.label)}</text>`,
|
|
61
|
+
`<line x1="${PLOT_X}" y1="${y + 30}" x2="${PLOT_X + PLOT_WIDTH}" y2="${y + 30}" stroke="var(--artifact-muted)" stroke-opacity="0.22" />`,
|
|
62
|
+
`<rect x="${PLOT_X}" y="${y + 8}" width="${barWidth}" height="22" rx="4" fill="var(--artifact-accent)" />`,
|
|
63
|
+
`<text x="${CHART_WIDTH - 8}" y="${y + 24}" text-anchor="end" fill="var(--artifact-fg)" font-weight="600">${escapeHtml(row.displayValue)}</text>`,
|
|
64
|
+
].join('');
|
|
65
|
+
}).join('');
|
|
66
|
+
return `<figure class="artifact-figure"><svg class="artifact-diagram" viewBox="0 0 ${CHART_WIDTH} ${height}" role="img" aria-label="${escapeHtml(title)}" xmlns="http://www.w3.org/2000/svg"><title>${escapeHtml(title)}</title><text x="${LABEL_WIDTH}" y="22" text-anchor="end" fill="var(--artifact-muted)" font-size="12" font-weight="600">${escapeHtml(chart.labelHeading)}</text><text x="${CHART_WIDTH - 8}" y="22" text-anchor="end" fill="var(--artifact-muted)" font-size="12" font-weight="600">${escapeHtml(chart.valueHeading)}</text>${rows}</svg></figure>`;
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=charts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"charts.js","sourceRoot":"","sources":["../../src/lib/charts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAcvC,MAAM,WAAW,GAAG,GAAG,CAAC;AACxB,MAAM,WAAW,GAAG,GAAG,CAAC;AACxB,MAAM,WAAW,GAAG,EAAE,CAAC;AACvB,MAAM,MAAM,GAAG,GAAG,CAAC;AACnB,MAAM,UAAU,GAAG,WAAW,GAAG,MAAM,GAAG,WAAW,CAAC;AACtD,MAAM,UAAU,GAAG,EAAE,CAAC;AACtB,MAAM,SAAS,GAAG,EAAE,CAAC;AACrB,MAAM,YAAY,GAAG,EAAE,CAAC;AAExB,SAAS,UAAU,CAAC,IAAY;IAC9B,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,EAAE,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IAClE,OAAO,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;AACvD,CAAC;AAED,SAAS,SAAS,CAAC,KAAe;IAChC,OAAO,KAAK,CAAC,MAAM,IAAI,CAAC,IAAI,KAAK,CAAC,KAAK,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC;AAC9E,CAAC;AAED,SAAS,YAAY,CAAC,KAAa;IACjC,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC,OAAO,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,MAAM,QAAQ,GAAG,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,KAAK,GAAG,OAAO,CAAC,GAAG,UAAU,CAAC;QACxE,OAAO;YACL,YAAY,WAAW,QAAQ,CAAC,GAAG,EAAE,iDAAiD,UAAU,CAAC,GAAG,CAAC,KAAK,CAAC,SAAS;YACpH,aAAa,MAAM,SAAS,CAAC,GAAG,EAAE,SAAS,MAAM,GAAG,UAAU,SAAS,CAAC,GAAG,EAAE,2DAA2D;YACxI,YAAY,MAAM,QAAQ,CAAC,GAAG,CAAC,YAAY,QAAQ,uDAAuD;YAC1G,YAAY,WAAW,GAAG,CAAC,QAAQ,CAAC,GAAG,EAAE,mEAAmE,UAAU,CAAC,GAAG,CAAC,YAAY,CAAC,SAAS;SAClJ,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACb,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IAEZ,OAAO,8EAA8E,WAAW,IAAI,MAAM,4BAA4B,UAAU,CAAC,KAAK,CAAC,+CAA+C,UAAU,CAAC,KAAK,CAAC,oBAAoB,WAAW,4FAA4F,UAAU,CAAC,KAAK,CAAC,YAAY,CAAC,mBAAmB,WAAW,GAAG,CAAC,4FAA4F,UAAU,CAAC,KAAK,CAAC,YAAY,CAAC,UAAU,IAAI,iBAAiB,CAAC;AAC9iB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
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) {\n result ^= value.charCodeAt(index);\n result = Math.imul(result, 16777619);\n }\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 const text = (anchor.textContent || '').replace(/\\s+/g, ' ').trim();\n const base = anchor.id ? 'id:' + anchor.id : section + ':' + anchor.tagName.toLowerCase() + ':' + hash(text);\n bases.push(base);\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) : [];\n } catch { return []; }\n };\n let highlights = read();\n const write = () => { try { localStorage.setItem(storageKey, JSON.stringify(highlights)); } catch {} };\n const textLength = (anchor) => (anchor.textContent || '').length;\n const offsetAt = (anchor, node, offset) => {\n const range = document.createRange();\n 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 = [];\n const walker = document.createTreeWalker(anchor, NodeFilter.SHOW_TEXT);\n let cursor = 0;\n let node = walker.nextNode();\n while (node) {\n const nodeStart = cursor;\n 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;\n node = walker.nextNode();\n }\n for (let index = nodes.length - 1; index >= 0; index -= 1) {\n const [textNode, from, to] = nodes[index];\n if (to <= from) continue;\n const range = document.createRange();\n range.setStart(textNode, from);\n range.setEnd(textNode, to);\n const mark = document.createElement('mark');\n mark.className = 'reader-highlight';\n mark.dataset.highlightId = id;\n mark.title = 'Click to remove highlight';\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);\n const last = byId.get(highlight.end.anchor);\n const firstIndex = anchors.indexOf(first);\n const lastIndex = anchors.indexOf(last);\n if (!first || !last || firstIndex < 0 || lastIndex < firstIndex) continue;\n for (let index = firstIndex; index <= lastIndex; index += 1) {\n const anchor = anchors[index];\n wrap(anchor, index === firstIndex ? highlight.start.offset : 0, index === lastIndex ? highlight.end.offset : textLength(anchor), highlight.id);\n }\n }\n };\n\n root.addEventListener('mouseup', () => {\n const selection = getSelection();\n if (!selection || selection.isCollapsed || !selection.rangeCount) return;\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;\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;\n const startIndex = anchors.indexOf(startAnchor);\n const endIndex = anchors.indexOf(endAnchor);\n if (startIndex > endIndex || (startIndex === endIndex && startOffset >= endOffset)) return;\n const candidate = { start: { anchor: startAnchor.dataset.readerAnchor, offset: startOffset }, end: { anchor: endAnchor.dataset.readerAnchor, offset: endOffset } };\n if (highlights.some((item) => item.start.anchor === candidate.start.anchor && item.start.offset === candidate.start.offset && item.end.anchor === candidate.end.anchor && item.end.offset === candidate.end.offset)) return;\n highlights.push({ id: Date.now().toString(36) + Math.random().toString(36).slice(2), ...candidate });\n write();\n selection.removeAllRanges();\n render();\n });\n root.addEventListener('click', (event) => {\n const mark = event.target.closest('mark[data-highlight-id]');\n if (!mark || !root.contains(mark)) return;\n event.preventDefault();\n event.stopPropagation();\n highlights = highlights.filter((item) => item.id !== mark.dataset.highlightId);\n write();\n render();\n });\n render();\n})();\n</script>";
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
export const HIGHLIGHTS_SCRIPT = `<script>
|
|
2
|
+
(() => {
|
|
3
|
+
const root = document.querySelector('.artifact-body');
|
|
4
|
+
if (!root) return;
|
|
5
|
+
|
|
6
|
+
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) {
|
|
11
|
+
result ^= value.charCodeAt(index);
|
|
12
|
+
result = Math.imul(result, 16777619);
|
|
13
|
+
}
|
|
14
|
+
return (result >>> 0).toString(36);
|
|
15
|
+
};
|
|
16
|
+
const anchors = [...root.querySelectorAll(selector)].filter((element) => !element.parentElement.closest(selector));
|
|
17
|
+
const bases = [];
|
|
18
|
+
let section = 'document';
|
|
19
|
+
for (const anchor of anchors) {
|
|
20
|
+
if ((anchor.tagName === 'H2' || anchor.tagName === 'H3') && anchor.id) section = anchor.id;
|
|
21
|
+
const text = (anchor.textContent || '').replace(/\\s+/g, ' ').trim();
|
|
22
|
+
const base = anchor.id ? 'id:' + anchor.id : section + ':' + anchor.tagName.toLowerCase() + ':' + hash(text);
|
|
23
|
+
bases.push(base);
|
|
24
|
+
}
|
|
25
|
+
const totals = new Map();
|
|
26
|
+
for (const base of bases) totals.set(base, (totals.get(base) || 0) + 1);
|
|
27
|
+
const occurrences = new Map();
|
|
28
|
+
for (let index = 0; index < anchors.length; index += 1) {
|
|
29
|
+
const anchor = anchors[index];
|
|
30
|
+
const base = bases[index];
|
|
31
|
+
const occurrence = (occurrences.get(base) || 0) + 1;
|
|
32
|
+
occurrences.set(base, occurrence);
|
|
33
|
+
anchor.dataset.readerAnchor = base.startsWith('id:') ? base : base + ':count:' + totals.get(base) + ':occurrence:' + occurrence;
|
|
34
|
+
}
|
|
35
|
+
const byId = new Map(anchors.map((anchor) => [anchor.dataset.readerAnchor, anchor]));
|
|
36
|
+
|
|
37
|
+
const read = () => {
|
|
38
|
+
try {
|
|
39
|
+
const value = JSON.parse(localStorage.getItem(storageKey) || '[]');
|
|
40
|
+
return Array.isArray(value) ? value.filter((item) => item && item.id && item.start && item.end) : [];
|
|
41
|
+
} catch { return []; }
|
|
42
|
+
};
|
|
43
|
+
let highlights = read();
|
|
44
|
+
const write = () => { try { localStorage.setItem(storageKey, JSON.stringify(highlights)); } catch {} };
|
|
45
|
+
const textLength = (anchor) => (anchor.textContent || '').length;
|
|
46
|
+
const offsetAt = (anchor, node, offset) => {
|
|
47
|
+
const range = document.createRange();
|
|
48
|
+
range.selectNodeContents(anchor);
|
|
49
|
+
try { range.setEnd(node, offset); } catch { return null; }
|
|
50
|
+
return range.toString().length;
|
|
51
|
+
};
|
|
52
|
+
const wrap = (anchor, start, end, id) => {
|
|
53
|
+
if (end <= start) return;
|
|
54
|
+
const nodes = [];
|
|
55
|
+
const walker = document.createTreeWalker(anchor, NodeFilter.SHOW_TEXT);
|
|
56
|
+
let cursor = 0;
|
|
57
|
+
let node = walker.nextNode();
|
|
58
|
+
while (node) {
|
|
59
|
+
const nodeStart = cursor;
|
|
60
|
+
const nodeEnd = cursor + node.data.length;
|
|
61
|
+
if (nodeEnd > start && nodeStart < end) nodes.push([node, Math.max(0, start - nodeStart), Math.min(node.data.length, end - nodeStart)]);
|
|
62
|
+
cursor = nodeEnd;
|
|
63
|
+
node = walker.nextNode();
|
|
64
|
+
}
|
|
65
|
+
for (let index = nodes.length - 1; index >= 0; index -= 1) {
|
|
66
|
+
const [textNode, from, to] = nodes[index];
|
|
67
|
+
if (to <= from) continue;
|
|
68
|
+
const range = document.createRange();
|
|
69
|
+
range.setStart(textNode, from);
|
|
70
|
+
range.setEnd(textNode, to);
|
|
71
|
+
const mark = document.createElement('mark');
|
|
72
|
+
mark.className = 'reader-highlight';
|
|
73
|
+
mark.dataset.highlightId = id;
|
|
74
|
+
mark.title = 'Click to remove highlight';
|
|
75
|
+
range.surroundContents(mark);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
const clearMarks = () => {
|
|
79
|
+
for (const mark of root.querySelectorAll('mark[data-highlight-id]')) mark.replaceWith(...mark.childNodes);
|
|
80
|
+
for (const anchor of anchors) anchor.normalize();
|
|
81
|
+
};
|
|
82
|
+
const render = () => {
|
|
83
|
+
clearMarks();
|
|
84
|
+
for (const highlight of highlights) {
|
|
85
|
+
const first = byId.get(highlight.start.anchor);
|
|
86
|
+
const last = byId.get(highlight.end.anchor);
|
|
87
|
+
const firstIndex = anchors.indexOf(first);
|
|
88
|
+
const lastIndex = anchors.indexOf(last);
|
|
89
|
+
if (!first || !last || firstIndex < 0 || lastIndex < firstIndex) continue;
|
|
90
|
+
for (let index = firstIndex; index <= lastIndex; index += 1) {
|
|
91
|
+
const anchor = anchors[index];
|
|
92
|
+
wrap(anchor, index === firstIndex ? highlight.start.offset : 0, index === lastIndex ? highlight.end.offset : textLength(anchor), highlight.id);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
root.addEventListener('mouseup', () => {
|
|
98
|
+
const selection = getSelection();
|
|
99
|
+
if (!selection || selection.isCollapsed || !selection.rangeCount) return;
|
|
100
|
+
const range = selection.getRangeAt(0);
|
|
101
|
+
const startAnchor = range.startContainer.parentElement?.closest('[data-reader-anchor]');
|
|
102
|
+
const endAnchor = range.endContainer.parentElement?.closest('[data-reader-anchor]');
|
|
103
|
+
if (!startAnchor || !endAnchor || !root.contains(startAnchor) || !root.contains(endAnchor)) return;
|
|
104
|
+
const startOffset = offsetAt(startAnchor, range.startContainer, range.startOffset);
|
|
105
|
+
const endOffset = offsetAt(endAnchor, range.endContainer, range.endOffset);
|
|
106
|
+
if (startOffset === null || endOffset === null) return;
|
|
107
|
+
const startIndex = anchors.indexOf(startAnchor);
|
|
108
|
+
const endIndex = anchors.indexOf(endAnchor);
|
|
109
|
+
if (startIndex > endIndex || (startIndex === endIndex && startOffset >= endOffset)) return;
|
|
110
|
+
const candidate = { start: { anchor: startAnchor.dataset.readerAnchor, offset: startOffset }, end: { anchor: endAnchor.dataset.readerAnchor, offset: endOffset } };
|
|
111
|
+
if (highlights.some((item) => item.start.anchor === candidate.start.anchor && item.start.offset === candidate.start.offset && item.end.anchor === candidate.end.anchor && item.end.offset === candidate.end.offset)) return;
|
|
112
|
+
highlights.push({ id: Date.now().toString(36) + Math.random().toString(36).slice(2), ...candidate });
|
|
113
|
+
write();
|
|
114
|
+
selection.removeAllRanges();
|
|
115
|
+
render();
|
|
116
|
+
});
|
|
117
|
+
root.addEventListener('click', (event) => {
|
|
118
|
+
const mark = event.target.closest('mark[data-highlight-id]');
|
|
119
|
+
if (!mark || !root.contains(mark)) return;
|
|
120
|
+
event.preventDefault();
|
|
121
|
+
event.stopPropagation();
|
|
122
|
+
highlights = highlights.filter((item) => item.id !== mark.dataset.highlightId);
|
|
123
|
+
write();
|
|
124
|
+
render();
|
|
125
|
+
});
|
|
126
|
+
render();
|
|
127
|
+
})();
|
|
128
|
+
</script>`;
|
|
129
|
+
//# sourceMappingURL=highlights.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"highlights.js","sourceRoot":"","sources":["../../src/lib/highlights.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,iBAAiB,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;UA+HvB,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Escape text for interpolation into an HTML attribute or text node. */
|
|
2
|
+
export declare function escapeHtml(value: string): string;
|
|
3
|
+
/**
|
|
4
|
+
* Inverse of escapeHtml, for reading a value back out of rendered HTML (an
|
|
5
|
+
* href, say) before parsing or re-escaping it. `&` resolves last so an
|
|
6
|
+
* escaped entity in the source text is not decoded twice.
|
|
7
|
+
*/
|
|
8
|
+
export declare function unescapeHtml(value: string): string;
|
package/dist/lib/html.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/** Escape text for interpolation into an HTML attribute or text node. */
|
|
2
|
+
export function escapeHtml(value) {
|
|
3
|
+
return value.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Inverse of escapeHtml, for reading a value back out of rendered HTML (an
|
|
7
|
+
* href, say) before parsing or re-escaping it. `&` resolves last so an
|
|
8
|
+
* escaped entity in the source text is not decoded twice.
|
|
9
|
+
*/
|
|
10
|
+
export function unescapeHtml(value) {
|
|
11
|
+
return value
|
|
12
|
+
.replace(/"/g, '"')
|
|
13
|
+
.replace(/�*39;/g, "'")
|
|
14
|
+
.replace(/</g, '<')
|
|
15
|
+
.replace(/>/g, '>')
|
|
16
|
+
.replace(/&/g, '&');
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=html.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"html.js","sourceRoot":"","sources":["../../src/lib/html.ts"],"names":[],"mappings":"AAAA,yEAAyE;AACzE,MAAM,UAAU,UAAU,CAAC,KAAa;IACtC,OAAO,KAAK,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;AAC1G,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,YAAY,CAAC,KAAa;IACxC,OAAO,KAAK;SACT,OAAO,CAAC,SAAS,EAAE,GAAG,CAAC;SACvB,OAAO,CAAC,UAAU,EAAE,GAAG,CAAC;SACxB,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC;SACrB,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC;SACrB,OAAO,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC;AAC5B,CAAC"}
|
package/dist/lib/icons.d.ts
CHANGED
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
export declare function humanIconSvg(): string;
|
|
2
2
|
export declare function harnessIconSvg(harness: string): string;
|
|
3
|
+
export interface LinkIconClasses {
|
|
4
|
+
/** Class for the inline brand mark (an <svg> that inherits currentColor). */
|
|
5
|
+
icon: string;
|
|
6
|
+
/** Class for the remote favicon fallback (an <img>). */
|
|
7
|
+
favicon: string;
|
|
8
|
+
}
|
|
3
9
|
/**
|
|
4
|
-
* Icon HTML for a
|
|
5
|
-
*
|
|
6
|
-
*
|
|
10
|
+
* Icon HTML for a link. Prefer an offline brand mark (simple-icons) when the
|
|
11
|
+
* host is known; otherwise try a remote favicon grab that removes itself if it
|
|
12
|
+
* fails to load. Hero metadata chips and body link pills both come through
|
|
13
|
+
* here, differing only in the class names they ask for.
|
|
7
14
|
*/
|
|
8
|
-
export declare function linkIconHtml(url: string): string | undefined;
|
|
15
|
+
export declare function linkIconHtml(url: string, classes?: LinkIconClasses): string | undefined;
|