@riebeckite/plugin-diff 0.0.15 → 0.0.16

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/dist/client.js CHANGED
@@ -99,10 +99,9 @@ function initDiffHistory() {
99
99
  }
100
100
  function initDiffHistoryRoot(root) {
101
101
  const payload = readPayload(root);
102
- if (!payload || payload.revisions.length === 0) return;
103
- const revisions = new Map(
104
- payload.revisions.map((revision) => [revision.hash, revision])
105
- );
102
+ if (!payload) return;
103
+ let revisions = revisionsMap(payload.revisions ?? []);
104
+ let revisionsRequest = null;
106
105
  const fromSelect = root.querySelector(
107
106
  "[data-rr-diff-from]"
108
107
  );
@@ -115,23 +114,74 @@ function initDiffHistoryRoot(root) {
115
114
  button.addEventListener("click", () => {
116
115
  const toHash = button.dataset.rrDiffSelect;
117
116
  if (!toHash) return;
118
- const diff = createDiff(revisions, fromSelect.value, toHash);
119
- if (!diff) return;
120
- fromSelect.value = diff.from?.hash ?? "";
121
- toSelect.value = diff.to.hash;
122
- updateSelectedCommit(root, diff.to.hash);
123
- renderPanel(panel, diff);
117
+ void updateDiff({
118
+ root,
119
+ panel,
120
+ getRevisions: () => revisions,
121
+ setRevisions: (loaded) => {
122
+ revisions = loaded;
123
+ },
124
+ load: () => {
125
+ revisionsRequest ??= loadRevisions(payload.path);
126
+ return revisionsRequest;
127
+ },
128
+ fromHash: fromSelect.value,
129
+ toHash,
130
+ fromSelect,
131
+ toSelect
132
+ });
124
133
  });
125
134
  }
126
135
  const updateFromSelects = () => {
127
- const diff = createDiff(revisions, fromSelect.value, toSelect.value);
128
- if (!diff) return;
129
- updateSelectedCommit(root, diff.to.hash);
130
- renderPanel(panel, diff);
136
+ void updateDiff({
137
+ root,
138
+ panel,
139
+ getRevisions: () => revisions,
140
+ setRevisions: (loaded) => {
141
+ revisions = loaded;
142
+ },
143
+ load: () => {
144
+ revisionsRequest ??= loadRevisions(payload.path);
145
+ return revisionsRequest;
146
+ },
147
+ fromHash: fromSelect.value,
148
+ toHash: toSelect.value,
149
+ fromSelect,
150
+ toSelect
151
+ });
131
152
  };
132
153
  fromSelect.addEventListener("change", updateFromSelects);
133
154
  toSelect.addEventListener("change", updateFromSelects);
134
155
  }
156
+ async function updateDiff(input) {
157
+ const { root, panel, fromHash, toHash, fromSelect, toSelect } = input;
158
+ const token = `${fromHash}
159
+ ${toHash}`;
160
+ root.dataset.rrDiffPending = token;
161
+ const loaded = await input.load();
162
+ if (loaded) input.setRevisions(loaded);
163
+ if (root.dataset.rrDiffPending !== token) return;
164
+ const diff = createDiff(input.getRevisions(), fromHash, toHash);
165
+ if (!diff) return;
166
+ fromSelect.value = diff.from?.hash ?? "";
167
+ toSelect.value = diff.to.hash;
168
+ updateSelectedCommit(root, diff.to.hash);
169
+ renderPanel(panel, diff);
170
+ }
171
+ async function loadRevisions(path) {
172
+ if (!path) return null;
173
+ try {
174
+ const response = await fetch(path);
175
+ if (!response.ok) return null;
176
+ const payload = await response.json();
177
+ return revisionsMap(payload.revisions ?? []);
178
+ } catch {
179
+ return null;
180
+ }
181
+ }
182
+ function revisionsMap(revisions) {
183
+ return new Map(revisions.map((revision) => [revision.hash, revision]));
184
+ }
135
185
  function createDiff(revisions, fromHash, toHash) {
136
186
  const to = revisions.get(toHash);
137
187
  if (!to) return null;
@@ -165,15 +215,13 @@ function updateSelectedCommit(root, hash) {
165
215
  }
166
216
  }
167
217
  function renderPanel(panel, diff) {
168
- const fromLabel = diff.from?.shortHash ?? "Beginning";
169
- panel.innerHTML = `<details class="rr-diff-history__diff" data-rr-diff-rendered open>
170
- <summary class="rr-diff-history__diff-head">${escapeHtml(fromLabel)} → ${escapeHtml(diff.to.shortHash)}</summary>
218
+ panel.innerHTML = `<div class="rr-diff-history__diff" data-rr-diff-rendered>
171
219
  <div class="rr-diff-history__table-wrap">
172
220
  <table class="rr-diff-history__table">
173
221
  <tbody>${diff.lines.map(renderLine).join("")}</tbody>
174
222
  </table>
175
223
  </div>
176
- </details>`;
224
+ </div>`;
177
225
  }
178
226
  function renderLine(line) {
179
227
  const prefix = line.type === "added" ? "+" : line.type === "removed" ? "-" : " ";
package/dist/index.js CHANGED
@@ -53,15 +53,13 @@ function renderDiffViewer({
53
53
  </div>`;
54
54
  }
55
55
  function renderDiffPanel(diff2) {
56
- const fromLabel = diff2.from?.shortHash ?? "Beginning";
57
- return `<details class="rr-diff-history__diff" data-rr-diff-rendered open>
58
- <summary class="rr-diff-history__diff-head">${escapeHtml(fromLabel)} → ${escapeHtml(diff2.to.shortHash)}</summary>
56
+ return `<div class="rr-diff-history__diff" data-rr-diff-rendered>
59
57
  <div class="rr-diff-history__table-wrap">
60
58
  <table class="rr-diff-history__table">
61
59
  <tbody>${diff2.lines.map((line) => renderDiffLine({ line })).join("")}</tbody>
62
60
  </table>
63
61
  </div>
64
- </details>`;
62
+ </div>`;
65
63
  }
66
64
  function renderRevisionSelect(kind, history, value) {
67
65
  const emptyOption = kind === "from" ? `<option value="">Beginning</option>` : "";
@@ -74,19 +72,16 @@ function renderRevisionSelect(kind, history, value) {
74
72
  function renderDiffHistory(input) {
75
73
  const history = input.revisions;
76
74
  const selected = input.selected;
77
- const payload = JSON.stringify({ revisions: input.revisions }).replace(
78
- /</g,
79
- "\\u003c"
80
- );
75
+ const payloadPath = input.payloadPath;
81
76
  return `<section class="rr-diff-history" data-rr-diff-history>
82
- <details class="rr-diff-history__panel" open>
77
+ <details class="rr-diff-history__panel">
83
78
  <summary class="rr-diff-history__header">
84
79
  <h2 class="rr-diff-history__title">History</h2>
85
80
  <span class="rr-diff-history__header-meta"><span class="rr-diff-history__count">${history.length} changes</span><span class="rr-diff-history__toggle-label rr-diff-history__toggle-label--open">Collapse</span><span class="rr-diff-history__toggle-label rr-diff-history__toggle-label--closed">Expand</span><span class="rr-diff-history__toggle" aria-hidden="true"><svg class="rr-diff-history__toggle-icon" viewBox="0 0 16 16" width="16" height="16" focusable="false"><path d="M5.2 3.4 10.8 8l-5.6 4.6" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.8"/></svg></span></span>
86
81
  </summary>
87
- ${history.length === 0 || !selected ? renderEmptyState() : renderContent(history, selected)}
82
+ ${history.length === 0 || !selected ? renderEmptyState() : renderContent(history, selected)}
88
83
  </details>
89
- <script type="application/json" data-rr-diff-history-data>${payload}</script>
84
+ ${payloadPath ? `<script type="application/json" data-rr-diff-history-data>${JSON.stringify({ path: payloadPath }).replace(/</g, "\\u003c")}</script>` : ""}
90
85
  </section>`;
91
86
  }
92
87
  function renderContent(history, selected) {
@@ -416,13 +411,25 @@ function diff(options = {}) {
416
411
  );
417
412
  const revisions = await getMarkdownRevisions(api, filePath, history);
418
413
  const selected = await api.getCurrentDiff(filePath);
414
+ const payloadPath = revisions.length > 0 ? diffPayloadPath(context.slug) : void 0;
415
+ if (payloadPath) {
416
+ context.output.emit({
417
+ path: payloadPath,
418
+ content: JSON.stringify({ revisions }).replace(/</g, "\\u003c"),
419
+ dependencies: [{ type: "content", slug: context.slug }]
420
+ });
421
+ }
419
422
  context.content.html += renderDiffHistory({
420
423
  revisions,
421
- selected
424
+ selected,
425
+ payloadPath: payloadPath ? `/${payloadPath}` : void 0
422
426
  });
423
427
  }
424
428
  });
425
429
  }
430
+ function diffPayloadPath(slug) {
431
+ return `_riebeckite/diff/${Buffer.from(slug).toString("base64url")}.json`;
432
+ }
426
433
  function createPostDiffApi(options = {}) {
427
434
  const reader = new GitMarkdownHistoryReader(options);
428
435
  return {
@@ -2,5 +2,6 @@ import type { MarkdownRevision, PostDiff } from "../types.js";
2
2
  export type DiffHistoryProps = {
3
3
  revisions: MarkdownRevision[];
4
4
  selected: PostDiff | null;
5
+ payloadPath?: string;
5
6
  };
6
7
  export declare function renderDiffHistory(input: DiffHistoryProps): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@riebeckite/plugin-diff",
3
- "version": "0.0.15",
3
+ "version": "0.0.16",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -21,12 +21,12 @@
21
21
  "./style.css": "./style.css"
22
22
  },
23
23
  "dependencies": {
24
- "@riebeckite/core": "0.0.15"
24
+ "@riebeckite/core": "0.0.16"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@types/node": "^24.5.2",
28
28
  "tsx": "^4.22.4",
29
- "@riebeckite/test": "0.0.15"
29
+ "@riebeckite/test": "0.0.16"
30
30
  },
31
31
  "publishConfig": {
32
32
  "access": "public"
package/style.css CHANGED
@@ -183,7 +183,6 @@
183
183
  }
184
184
 
185
185
  .rr-diff-history__commit:focus-visible,
186
- .rr-diff-history__diff-head:focus-visible,
187
186
  .rr-diff-history__controls select:focus-visible {
188
187
  outline: 2px solid var(--rb-color-accent, #f66620);
189
188
  outline-offset: 2px;
@@ -222,39 +221,16 @@
222
221
  var(--rb-color-border, color-mix(in srgb, currentColor 18%, transparent));
223
222
  }
224
223
 
225
- .rr-diff-history__diff-head {
226
- display: flex;
227
- align-items: center;
228
- gap: 0.5rem;
229
- border-block-end: 1px solid
230
- var(--rb-color-border, color-mix(in srgb, currentColor 18%, transparent));
231
- padding: 0.625rem 0.75rem;
232
- font-family: var(
233
- --rb-font-mono,
234
- ui-monospace,
235
- SFMono-Regular,
236
- Consolas,
237
- monospace
238
- );
239
- font-size: 0.8125rem;
240
- cursor: pointer;
241
- user-select: none;
242
- }
243
-
244
- .rr-diff-history__diff-head::marker {
245
- color: var(
246
- --rb-color-muted,
247
- color-mix(in srgb, currentColor 62%, transparent)
248
- );
249
- }
250
-
251
224
  .rr-diff-history__table-wrap {
252
225
  overflow-x: auto;
253
226
  }
254
227
 
255
228
  .rr-diff-history__table {
229
+ display: table;
256
230
  width: 100%;
257
231
  min-width: 42rem;
232
+ margin-block: 0;
233
+ overflow-x: visible;
258
234
  border-collapse: collapse;
259
235
  font-family: var(
260
236
  --rb-font-mono,