@riebeckite/plugin-diff 0.0.1
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/LICENSE +201 -0
- package/README.md +90 -0
- package/README_ja.md +41 -0
- package/dist/client/diff-viewer.d.ts +1 -0
- package/dist/client.d.ts +1 -0
- package/dist/client.js +89 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.js +394 -0
- package/dist/src/components/diff-history.d.ts +6 -0
- package/dist/src/components/diff-line.d.ts +6 -0
- package/dist/src/components/diff-viewer.d.ts +7 -0
- package/dist/src/diff/line_diff.d.ts +2 -0
- package/dist/src/git/history_reader.d.ts +7 -0
- package/dist/src/types.d.ts +34 -0
- package/package.json +64 -0
- package/style.css +229 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright Rerurate_514
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# @riebeckite/plugin-diff
|
|
2
|
+
|
|
3
|
+
Git-backed diff and revision history for Markdown notes: read commit history,
|
|
4
|
+
retrieve past revisions, and compute line-level diffs between them.
|
|
5
|
+
|
|
6
|
+
[日本語](./README_ja.md)
|
|
7
|
+
|
|
8
|
+
## Overview
|
|
9
|
+
|
|
10
|
+
`createPostDiffApi()` wraps a local Git repository and exposes revision
|
|
11
|
+
history plus line diffs for any Markdown file, even when the note was renamed
|
|
12
|
+
or moved (`git log --follow`). `createLineDiff()` is the pure diff engine used
|
|
13
|
+
by the API and is also exported for standalone use.
|
|
14
|
+
|
|
15
|
+
The `diff()` plugin registers a `diff` entry in the plugin list; the
|
|
16
|
+
programmatic API is the primary interface.
|
|
17
|
+
|
|
18
|
+
## Usage (plugin)
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { defineConfig } from "@riebeckite/core";
|
|
22
|
+
import { diff } from "@riebeckite/plugin-diff";
|
|
23
|
+
|
|
24
|
+
export default defineConfig({
|
|
25
|
+
// ...
|
|
26
|
+
plugins: [diff({ cwd: "./content" })],
|
|
27
|
+
});
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
`diff(options?)` accepts the same `GitHistoryReaderOptions` as the API.
|
|
31
|
+
|
|
32
|
+
## Programmatic API
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { createPostDiffApi } from "@riebeckite/plugin-diff";
|
|
36
|
+
|
|
37
|
+
const api = createPostDiffApi({ cwd: "./content" });
|
|
38
|
+
|
|
39
|
+
const history = await api.getHistory("notes/hello.md");
|
|
40
|
+
const previous = await api.getRevisionMarkdown("notes/hello.md", history[1].hash);
|
|
41
|
+
const current = await api.getCurrentDiff("notes/hello.md");
|
|
42
|
+
const compare = await api.compareRevisions({
|
|
43
|
+
filePath: "notes/hello.md",
|
|
44
|
+
fromHash: history[1].hash,
|
|
45
|
+
toHash: history[0].hash,
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
- `getHistory(filePath)` — commit history for the file (newest first) as
|
|
50
|
+
`DiffRevision[]`
|
|
51
|
+
- `getRevisionMarkdown(filePath, hash)` — Markdown source at a given revision,
|
|
52
|
+
or `null`
|
|
53
|
+
- `getCurrentDiff(filePath)` — line diff between the latest revision and its
|
|
54
|
+
predecessor (or from an empty source when there is no predecessor)
|
|
55
|
+
- `compareRevisions({ filePath, fromHash, toHash })` — line diff between two
|
|
56
|
+
revisions; `fromHash: null` diffs from an empty source
|
|
57
|
+
|
|
58
|
+
## Options
|
|
59
|
+
|
|
60
|
+
`createPostDiffApi(options?)` accepts `GitHistoryReaderOptions`:
|
|
61
|
+
|
|
62
|
+
| Option | Type | Default | Description |
|
|
63
|
+
| ------ | ---- | ------- | ----------- |
|
|
64
|
+
| `cwd` | `string` | `process.cwd()` | Working directory for Git commands |
|
|
65
|
+
|
|
66
|
+
When `cwd` is not inside a Git repository, API calls resolve to empty results
|
|
67
|
+
(`[]` / `null`) instead of throwing.
|
|
68
|
+
|
|
69
|
+
## Types
|
|
70
|
+
|
|
71
|
+
| Type | Description |
|
|
72
|
+
| ---- | ----------- |
|
|
73
|
+
| `DiffRevision` | Commit metadata: `hash`, `shortHash`, `date`, `message`, `author` |
|
|
74
|
+
| `MarkdownRevision` | `DiffRevision` with the Markdown source |
|
|
75
|
+
| `PostDiff` | `from`, `to`, and `lines` |
|
|
76
|
+
| `DiffLine` | A single diff line: `{ type, content }` |
|
|
77
|
+
| `DiffLineType` | `"context" \| "added" \| "removed"` |
|
|
78
|
+
| `RevisionComparisonInput` | `{ filePath, fromHash: string \| null, toHash }` |
|
|
79
|
+
|
|
80
|
+
## Exports
|
|
81
|
+
|
|
82
|
+
- `diff(options?)` — plugin factory
|
|
83
|
+
- `createPostDiffApi(options?)` — programmatic API factory
|
|
84
|
+
- `createLineDiff(from, to)` — LCS-based line diff
|
|
85
|
+
- `GitMarkdownHistoryReader` — Git-backed history reader class
|
|
86
|
+
- Types listed above
|
|
87
|
+
|
|
88
|
+
## See also
|
|
89
|
+
|
|
90
|
+
- [Plugin guide](../../docs/plugins_en.md)
|
package/README_ja.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# @riebeckite/plugin-diff
|
|
2
|
+
|
|
3
|
+
ローカル Git リポジトリから、Markdown ノートの履歴と行単位の差分を取得するプラグインです。
|
|
4
|
+
|
|
5
|
+
[English](./README.md)
|
|
6
|
+
|
|
7
|
+
## できること
|
|
8
|
+
|
|
9
|
+
中心となる API は `createPostDiffApi()` です。リネームや移動後のファイルも `git log --follow` で追跡し、履歴、指定リビジョンの本文、任意の二つのリビジョン間の差分を返します。`createLineDiff()` は単独でも使える純粋な差分関数です。
|
|
10
|
+
|
|
11
|
+
## 設定と利用例
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { defineConfig } from "@riebeckite/core";
|
|
15
|
+
import { createPostDiffApi, diff } from "@riebeckite/plugin-diff";
|
|
16
|
+
|
|
17
|
+
export default defineConfig({ plugins: [diff({ cwd: "./content" })] });
|
|
18
|
+
|
|
19
|
+
const api = createPostDiffApi({ cwd: "./content" });
|
|
20
|
+
const history = await api.getHistory("notes/hello.md");
|
|
21
|
+
const current = await api.getCurrentDiff("notes/hello.md");
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
| API | 返す内容 |
|
|
25
|
+
| --- | --- |
|
|
26
|
+
| `getHistory(filePath)` | 新しい順の `DiffRevision[]` |
|
|
27
|
+
| `getRevisionMarkdown(filePath, hash)` | その時点の Markdown。なければ `null` |
|
|
28
|
+
| `getCurrentDiff(filePath)` | 最新と一つ前のリビジョンの差分 |
|
|
29
|
+
| `compareRevisions(input)` | 指定した二つのリビジョンの差分 |
|
|
30
|
+
|
|
31
|
+
`cwd` は Git コマンドを実行するディレクトリで、既定値は `process.cwd()` です。Git リポジトリ外を指定しても例外は投げず、空の結果を返します。
|
|
32
|
+
|
|
33
|
+
## 公開 API
|
|
34
|
+
|
|
35
|
+
- `diff(options?)`、`createPostDiffApi(options?)`
|
|
36
|
+
- `createLineDiff(from, to)`、`GitMarkdownHistoryReader`
|
|
37
|
+
- `DiffRevision`、`MarkdownRevision`、`PostDiff`、`DiffLine`、`DiffLineType`、`RevisionComparisonInput`
|
|
38
|
+
|
|
39
|
+
## 関連資料
|
|
40
|
+
|
|
41
|
+
- [プラグインシステム](../../../docs/ja/plugin-system.md)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function initDiffHistory(): void;
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { initDiffHistory } from "./client/diff-viewer.js";
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// src/components/diff-line.tsx
|
|
2
|
+
function escapeHtml(value) {
|
|
3
|
+
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
// client/diff-viewer.ts
|
|
7
|
+
function initDiffHistory() {
|
|
8
|
+
for (const root of document.querySelectorAll(
|
|
9
|
+
"[data-rr-diff-history]"
|
|
10
|
+
)) {
|
|
11
|
+
initDiffHistoryRoot(root);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function initDiffHistoryRoot(root) {
|
|
15
|
+
const payload = readPayload(root);
|
|
16
|
+
if (!payload || payload.diffs.length === 0) return;
|
|
17
|
+
const fromSelect = root.querySelector(
|
|
18
|
+
"[data-rr-diff-from]"
|
|
19
|
+
);
|
|
20
|
+
const toSelect = root.querySelector("[data-rr-diff-to]");
|
|
21
|
+
const panel = root.querySelector("[data-rr-diff-panel]");
|
|
22
|
+
if (!fromSelect || !toSelect || !panel) return;
|
|
23
|
+
for (const button of root.querySelectorAll(
|
|
24
|
+
"[data-rr-diff-select]"
|
|
25
|
+
)) {
|
|
26
|
+
button.addEventListener("click", () => {
|
|
27
|
+
const toHash = button.dataset.rrDiffSelect;
|
|
28
|
+
const diff = payload.diffs.find((entry) => entry.to.hash === toHash);
|
|
29
|
+
if (!diff) return;
|
|
30
|
+
fromSelect.value = diff.from?.hash ?? "";
|
|
31
|
+
toSelect.value = diff.to.hash;
|
|
32
|
+
updateSelectedCommit(root, diff.to.hash);
|
|
33
|
+
renderPanel(panel, diff);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
const updateFromSelects = () => {
|
|
37
|
+
const diff = payload.diffs.find(
|
|
38
|
+
(entry) => (entry.from?.hash ?? "") === fromSelect.value && entry.to.hash === toSelect.value
|
|
39
|
+
);
|
|
40
|
+
if (!diff) return;
|
|
41
|
+
updateSelectedCommit(root, diff.to.hash);
|
|
42
|
+
renderPanel(panel, diff);
|
|
43
|
+
};
|
|
44
|
+
fromSelect.addEventListener("change", updateFromSelects);
|
|
45
|
+
toSelect.addEventListener("change", updateFromSelects);
|
|
46
|
+
}
|
|
47
|
+
function readPayload(root) {
|
|
48
|
+
const script = root.querySelector(
|
|
49
|
+
"[data-rr-diff-history-data]"
|
|
50
|
+
);
|
|
51
|
+
if (!script?.textContent) return null;
|
|
52
|
+
try {
|
|
53
|
+
return JSON.parse(script.textContent);
|
|
54
|
+
} catch {
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function updateSelectedCommit(root, hash) {
|
|
59
|
+
for (const button of root.querySelectorAll(
|
|
60
|
+
"[data-rr-diff-select]"
|
|
61
|
+
)) {
|
|
62
|
+
button.setAttribute(
|
|
63
|
+
"aria-pressed",
|
|
64
|
+
String(button.dataset.rrDiffSelect === hash)
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
function renderPanel(panel, diff) {
|
|
69
|
+
const fromLabel = diff.from?.shortHash ?? "Beginning";
|
|
70
|
+
panel.innerHTML = `<div class="rr-diff-history__diff" data-rr-diff-rendered>
|
|
71
|
+
<div class="rr-diff-history__diff-head">${escapeHtml(fromLabel)} → ${escapeHtml(diff.to.shortHash)}</div>
|
|
72
|
+
<div class="rr-diff-history__table-wrap">
|
|
73
|
+
<table class="rr-diff-history__table">
|
|
74
|
+
<tbody>${diff.lines.map(renderLine).join("")}</tbody>
|
|
75
|
+
</table>
|
|
76
|
+
</div>
|
|
77
|
+
</div>`;
|
|
78
|
+
}
|
|
79
|
+
function renderLine(line) {
|
|
80
|
+
const prefix = line.type === "added" ? "+" : line.type === "removed" ? "-" : " ";
|
|
81
|
+
return `<tr class="rr-diff-history__line rr-diff-history__line--${line.type}">
|
|
82
|
+
<td class="rr-diff-history__num">${line.oldLineNumber ?? ""}</td>
|
|
83
|
+
<td class="rr-diff-history__num">${line.newLineNumber ?? ""}</td>
|
|
84
|
+
<td class="rr-diff-history__code"><code>${prefix} ${escapeHtml(line.content)}</code></td>
|
|
85
|
+
</tr>`;
|
|
86
|
+
}
|
|
87
|
+
export {
|
|
88
|
+
initDiffHistory
|
|
89
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export { renderDiffHistory } from "./src/components/diff-history.js";
|
|
2
|
+
export { renderDiffLine } from "./src/components/diff-line.js";
|
|
3
|
+
export { renderDiffPanel, renderDiffViewer, } from "./src/components/diff-viewer.js";
|
|
4
|
+
import type { DiffPluginUiOptions, DiffRevision, GitHistoryReaderOptions, PostDiff, RevisionComparisonInput } from "./src/types.js";
|
|
5
|
+
export { createLineDiff } from "./src/diff/line_diff.js";
|
|
6
|
+
export { GitMarkdownHistoryReader } from "./src/git/history_reader.js";
|
|
7
|
+
export type { DiffLine, DiffLineType, DiffPluginUiOptions, DiffRevision, GitHistoryReaderOptions, MarkdownRevision, PostDiff, RevisionComparisonInput, } from "./src/types.js";
|
|
8
|
+
export type DiffPluginOptions = GitHistoryReaderOptions & {
|
|
9
|
+
ui?: DiffPluginUiOptions;
|
|
10
|
+
};
|
|
11
|
+
export type PostDiffApi = {
|
|
12
|
+
getHistory(filePath: string): Promise<DiffRevision[]>;
|
|
13
|
+
getRevisionMarkdown(filePath: string, hash: string): Promise<string | null>;
|
|
14
|
+
getCurrentDiff(filePath: string): Promise<PostDiff | null>;
|
|
15
|
+
compareRevisions(input: RevisionComparisonInput): Promise<PostDiff | null>;
|
|
16
|
+
};
|
|
17
|
+
export declare function diff(options?: DiffPluginOptions): import("@riebeckite/core").RiebeckitePlugin<DiffPluginOptions>;
|
|
18
|
+
export declare function createPostDiffApi(options?: GitHistoryReaderOptions): PostDiffApi;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,394 @@
|
|
|
1
|
+
// index.ts
|
|
2
|
+
import {
|
|
3
|
+
createClientEntry,
|
|
4
|
+
createStyleAsset,
|
|
5
|
+
definePlugin
|
|
6
|
+
} from "@riebeckite/core";
|
|
7
|
+
|
|
8
|
+
// src/components/diff-line.tsx
|
|
9
|
+
function renderDiffLine({ line }) {
|
|
10
|
+
const prefix = line.type === "added" ? "+" : line.type === "removed" ? "-" : " ";
|
|
11
|
+
return `<tr class="rr-diff-history__line rr-diff-history__line--${line.type}">
|
|
12
|
+
<td class="rr-diff-history__num">${line.oldLineNumber ?? ""}</td>
|
|
13
|
+
<td class="rr-diff-history__num">${line.newLineNumber ?? ""}</td>
|
|
14
|
+
<td class="rr-diff-history__code"><code>${prefix} ${escapeHtml(line.content)}</code></td>
|
|
15
|
+
</tr>`;
|
|
16
|
+
}
|
|
17
|
+
function escapeHtml(value) {
|
|
18
|
+
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// src/components/diff-viewer.tsx
|
|
22
|
+
function renderDiffViewer({
|
|
23
|
+
history,
|
|
24
|
+
selected
|
|
25
|
+
}) {
|
|
26
|
+
return `<div class="rr-diff-history__viewer">
|
|
27
|
+
<div class="rr-diff-history__controls">
|
|
28
|
+
<label>From ${renderRevisionSelect("from", history, selected.from?.hash ?? "")}</label>
|
|
29
|
+
<label>To ${renderRevisionSelect("to", history, selected.to.hash)}</label>
|
|
30
|
+
</div>
|
|
31
|
+
<div data-rr-diff-panel>${renderDiffPanel(selected)}</div>
|
|
32
|
+
</div>`;
|
|
33
|
+
}
|
|
34
|
+
function renderDiffPanel(diff2) {
|
|
35
|
+
const fromLabel = diff2.from?.shortHash ?? "Beginning";
|
|
36
|
+
return `<div class="rr-diff-history__diff" data-rr-diff-rendered>
|
|
37
|
+
<div class="rr-diff-history__diff-head">${escapeHtml(fromLabel)} → ${escapeHtml(diff2.to.shortHash)}</div>
|
|
38
|
+
<div class="rr-diff-history__table-wrap">
|
|
39
|
+
<table class="rr-diff-history__table">
|
|
40
|
+
<tbody>${diff2.lines.map((line) => renderDiffLine({ line })).join("")}</tbody>
|
|
41
|
+
</table>
|
|
42
|
+
</div>
|
|
43
|
+
</div>`;
|
|
44
|
+
}
|
|
45
|
+
function renderRevisionSelect(kind, history, value) {
|
|
46
|
+
const emptyOption = kind === "from" ? `<option value="">Beginning</option>` : "";
|
|
47
|
+
return `<select data-rr-diff-${kind}>${emptyOption}${history.map(
|
|
48
|
+
(revision) => `<option value="${escapeHtml(revision.hash)}"${revision.hash === value ? " selected" : ""}>${escapeHtml(revision.shortHash)} · ${escapeHtml(formatDate(revision.date))}</option>`
|
|
49
|
+
).join("")}</select>`;
|
|
50
|
+
}
|
|
51
|
+
function formatDate(value) {
|
|
52
|
+
const date = new Date(value);
|
|
53
|
+
if (Number.isNaN(date.getTime())) return value;
|
|
54
|
+
return date.toLocaleDateString("en", { month: "short", day: "numeric" });
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// src/components/diff-history.tsx
|
|
58
|
+
function renderDiffHistory(input) {
|
|
59
|
+
const selected = input.diffs[0] ?? null;
|
|
60
|
+
const payload = JSON.stringify(input).replace(/</g, "\\u003c");
|
|
61
|
+
return `<section class="rr-diff-history" data-rr-diff-history>
|
|
62
|
+
<div class="rr-diff-history__header">
|
|
63
|
+
<h2 class="rr-diff-history__title">History</h2>
|
|
64
|
+
<span class="rr-diff-history__count">${input.history.length} changes</span>
|
|
65
|
+
</div>
|
|
66
|
+
${input.history.length === 0 || !selected ? renderEmptyState() : renderContent(input, selected)}
|
|
67
|
+
<script type="application/json" data-rr-diff-history-data>${payload}</script>
|
|
68
|
+
</section>`;
|
|
69
|
+
}
|
|
70
|
+
function renderContent(input, selected) {
|
|
71
|
+
return `<div class="rr-diff-history__layout">
|
|
72
|
+
<ol class="rr-diff-history__list" aria-label="Commit history">
|
|
73
|
+
${input.history.map((revision, index) => renderRevisionButton(revision, index === 0)).join("")}
|
|
74
|
+
</ol>
|
|
75
|
+
${renderDiffViewer({ history: input.history, selected })}
|
|
76
|
+
</div>`;
|
|
77
|
+
}
|
|
78
|
+
function renderEmptyState() {
|
|
79
|
+
return `<p class="rr-diff-history__empty">No Git history is available for this article.</p>`;
|
|
80
|
+
}
|
|
81
|
+
function renderRevisionButton(revision, selected) {
|
|
82
|
+
return `<li class="rr-diff-history__item">
|
|
83
|
+
<button class="rr-diff-history__commit" type="button" data-rr-diff-select="${escapeHtml(revision.hash)}" aria-pressed="${selected}">
|
|
84
|
+
<span class="rr-diff-history__date">${escapeHtml(formatDate2(revision.date))}</span>
|
|
85
|
+
<span class="rr-diff-history__message">${escapeHtml(revision.message || "Untitled change")}</span>
|
|
86
|
+
<span class="rr-diff-history__meta">${escapeHtml(revision.shortHash)} · ${escapeHtml(revision.author)}</span>
|
|
87
|
+
</button>
|
|
88
|
+
</li>`;
|
|
89
|
+
}
|
|
90
|
+
function formatDate2(value) {
|
|
91
|
+
const date = new Date(value);
|
|
92
|
+
if (Number.isNaN(date.getTime())) return value;
|
|
93
|
+
return date.toLocaleDateString("en", { month: "short", day: "numeric" });
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// src/diff/line_diff.ts
|
|
97
|
+
function createLineDiff(fromMarkdown, toMarkdown) {
|
|
98
|
+
const fromLines = splitLines(fromMarkdown);
|
|
99
|
+
const toLines = splitLines(toMarkdown);
|
|
100
|
+
const table = buildLongestCommonSubsequenceTable(fromLines, toLines);
|
|
101
|
+
const lines = [];
|
|
102
|
+
let fromIndex = 0;
|
|
103
|
+
let toIndex = 0;
|
|
104
|
+
let oldLineNumber = 1;
|
|
105
|
+
let newLineNumber = 1;
|
|
106
|
+
while (fromIndex < fromLines.length && toIndex < toLines.length) {
|
|
107
|
+
if (fromLines[fromIndex] === toLines[toIndex]) {
|
|
108
|
+
lines.push({
|
|
109
|
+
type: "context",
|
|
110
|
+
content: fromLines[fromIndex],
|
|
111
|
+
oldLineNumber,
|
|
112
|
+
newLineNumber
|
|
113
|
+
});
|
|
114
|
+
fromIndex += 1;
|
|
115
|
+
toIndex += 1;
|
|
116
|
+
oldLineNumber += 1;
|
|
117
|
+
newLineNumber += 1;
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
if (table[fromIndex + 1][toIndex] >= table[fromIndex][toIndex + 1]) {
|
|
121
|
+
lines.push({
|
|
122
|
+
type: "removed",
|
|
123
|
+
content: fromLines[fromIndex],
|
|
124
|
+
oldLineNumber,
|
|
125
|
+
newLineNumber: null
|
|
126
|
+
});
|
|
127
|
+
fromIndex += 1;
|
|
128
|
+
oldLineNumber += 1;
|
|
129
|
+
} else {
|
|
130
|
+
lines.push({
|
|
131
|
+
type: "added",
|
|
132
|
+
content: toLines[toIndex],
|
|
133
|
+
oldLineNumber: null,
|
|
134
|
+
newLineNumber
|
|
135
|
+
});
|
|
136
|
+
toIndex += 1;
|
|
137
|
+
newLineNumber += 1;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
while (fromIndex < fromLines.length) {
|
|
141
|
+
lines.push({
|
|
142
|
+
type: "removed",
|
|
143
|
+
content: fromLines[fromIndex],
|
|
144
|
+
oldLineNumber,
|
|
145
|
+
newLineNumber: null
|
|
146
|
+
});
|
|
147
|
+
fromIndex += 1;
|
|
148
|
+
oldLineNumber += 1;
|
|
149
|
+
}
|
|
150
|
+
while (toIndex < toLines.length) {
|
|
151
|
+
lines.push({
|
|
152
|
+
type: "added",
|
|
153
|
+
content: toLines[toIndex],
|
|
154
|
+
oldLineNumber: null,
|
|
155
|
+
newLineNumber
|
|
156
|
+
});
|
|
157
|
+
toIndex += 1;
|
|
158
|
+
newLineNumber += 1;
|
|
159
|
+
}
|
|
160
|
+
return lines;
|
|
161
|
+
}
|
|
162
|
+
function splitLines(markdown) {
|
|
163
|
+
if (!markdown) return [];
|
|
164
|
+
return markdown.replace(/\r\n/g, "\n").replace(/\r/g, "\n").split("\n");
|
|
165
|
+
}
|
|
166
|
+
function buildLongestCommonSubsequenceTable(fromLines, toLines) {
|
|
167
|
+
const table = Array.from(
|
|
168
|
+
{ length: fromLines.length + 1 },
|
|
169
|
+
() => Array.from({ length: toLines.length + 1 }, () => 0)
|
|
170
|
+
);
|
|
171
|
+
for (let fromIndex = fromLines.length - 1; fromIndex >= 0; fromIndex -= 1) {
|
|
172
|
+
for (let toIndex = toLines.length - 1; toIndex >= 0; toIndex -= 1) {
|
|
173
|
+
table[fromIndex][toIndex] = fromLines[fromIndex] === toLines[toIndex] ? table[fromIndex + 1][toIndex + 1] + 1 : Math.max(
|
|
174
|
+
table[fromIndex + 1][toIndex],
|
|
175
|
+
table[fromIndex][toIndex + 1]
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
return table;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// src/git/history_reader.ts
|
|
183
|
+
import { execFile } from "node:child_process";
|
|
184
|
+
import { promisify } from "node:util";
|
|
185
|
+
var execFileAsync = promisify(execFile);
|
|
186
|
+
var fieldSeparator = "";
|
|
187
|
+
var recordSeparator = "";
|
|
188
|
+
var GitMarkdownHistoryReader = class {
|
|
189
|
+
#cwd;
|
|
190
|
+
#historyCache = /* @__PURE__ */ new Map();
|
|
191
|
+
#markdownCache = /* @__PURE__ */ new Map();
|
|
192
|
+
#revisionPathCache = /* @__PURE__ */ new Map();
|
|
193
|
+
#repositoryAvailable;
|
|
194
|
+
constructor(options = {}) {
|
|
195
|
+
this.#cwd = options.cwd ?? process.cwd();
|
|
196
|
+
}
|
|
197
|
+
getHistory(filePath) {
|
|
198
|
+
const normalizedPath = normalizeGitPath(filePath);
|
|
199
|
+
const cached = this.#historyCache.get(normalizedPath);
|
|
200
|
+
if (cached) return cached;
|
|
201
|
+
const history = this.#readHistory(normalizedPath);
|
|
202
|
+
this.#historyCache.set(normalizedPath, history);
|
|
203
|
+
return history;
|
|
204
|
+
}
|
|
205
|
+
getRevisionMarkdown(filePath, hash) {
|
|
206
|
+
const normalizedPath = normalizeGitPath(filePath);
|
|
207
|
+
const cacheKey = `${hash}:${normalizedPath}`;
|
|
208
|
+
const cached = this.#markdownCache.get(cacheKey);
|
|
209
|
+
if (cached) return cached;
|
|
210
|
+
const markdown = this.#readRevisionMarkdown(normalizedPath, hash);
|
|
211
|
+
this.#markdownCache.set(cacheKey, markdown);
|
|
212
|
+
return markdown;
|
|
213
|
+
}
|
|
214
|
+
async #readHistory(filePath) {
|
|
215
|
+
if (!await this.#isRepositoryAvailable()) return [];
|
|
216
|
+
const format = ["%H", "%h", "%cI", "%s", "%an"].join(fieldSeparator);
|
|
217
|
+
const result = await runGit(this.#cwd, [
|
|
218
|
+
"log",
|
|
219
|
+
"--follow",
|
|
220
|
+
"--name-status",
|
|
221
|
+
`--format=${recordSeparator}${format}`,
|
|
222
|
+
"--",
|
|
223
|
+
filePath
|
|
224
|
+
]);
|
|
225
|
+
if (!result.ok || !result.stdout.trim()) return [];
|
|
226
|
+
const revisionPaths = /* @__PURE__ */ new Map();
|
|
227
|
+
const revisions = result.stdout.split(recordSeparator).map((record) => record.trim()).filter(Boolean).map((record) => parseRevisionRecord(record, revisionPaths, filePath)).filter((revision) => revision !== null);
|
|
228
|
+
this.#revisionPathCache.set(filePath, revisionPaths);
|
|
229
|
+
return revisions;
|
|
230
|
+
}
|
|
231
|
+
async #readRevisionMarkdown(filePath, hash) {
|
|
232
|
+
if (!await this.#isRepositoryAvailable()) return null;
|
|
233
|
+
await this.getHistory(filePath);
|
|
234
|
+
const revisionPath = this.#revisionPathCache.get(filePath)?.get(hash) ?? filePath;
|
|
235
|
+
const result = await runGit(this.#cwd, ["show", `${hash}:${revisionPath}`]);
|
|
236
|
+
return result.ok ? result.stdout : null;
|
|
237
|
+
}
|
|
238
|
+
#isRepositoryAvailable() {
|
|
239
|
+
this.#repositoryAvailable ??= runGit(this.#cwd, [
|
|
240
|
+
"rev-parse",
|
|
241
|
+
"--is-inside-work-tree"
|
|
242
|
+
]).then((result) => result.ok && result.stdout.trim() === "true");
|
|
243
|
+
return this.#repositoryAvailable;
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
function parseRevisionRecord(record, revisionPaths, fallbackPath) {
|
|
247
|
+
const [metadata = "", ...nameStatusLines] = record.split("\n");
|
|
248
|
+
const [hash, shortHash, date, message, author] = metadata.split(fieldSeparator);
|
|
249
|
+
if (!hash || !shortHash || !date) return null;
|
|
250
|
+
revisionPaths.set(hash, getRevisionPath(nameStatusLines, fallbackPath));
|
|
251
|
+
return {
|
|
252
|
+
hash,
|
|
253
|
+
shortHash,
|
|
254
|
+
date,
|
|
255
|
+
message: message ?? "",
|
|
256
|
+
author: author ?? ""
|
|
257
|
+
};
|
|
258
|
+
}
|
|
259
|
+
function getRevisionPath(nameStatusLines, fallbackPath) {
|
|
260
|
+
const line = nameStatusLines.find((entry) => entry.trim().length > 0);
|
|
261
|
+
if (!line) return fallbackPath;
|
|
262
|
+
const [, ...paths] = line.split(" ");
|
|
263
|
+
return paths.at(-1) ?? fallbackPath;
|
|
264
|
+
}
|
|
265
|
+
function normalizeGitPath(filePath) {
|
|
266
|
+
return filePath.replace(/\\/g, "/").replace(/^\.\//, "");
|
|
267
|
+
}
|
|
268
|
+
async function runGit(cwd, args) {
|
|
269
|
+
try {
|
|
270
|
+
const { stdout } = await execFileAsync("git", args, {
|
|
271
|
+
cwd,
|
|
272
|
+
maxBuffer: 1024 * 1024 * 10
|
|
273
|
+
});
|
|
274
|
+
return { ok: true, stdout };
|
|
275
|
+
} catch (error) {
|
|
276
|
+
const stdout = typeof error === "object" && error !== null && "stdout" in error && typeof error.stdout === "string" ? error.stdout : "";
|
|
277
|
+
return { ok: false, stdout };
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// index.ts
|
|
282
|
+
function diff(options = {}) {
|
|
283
|
+
const api = createPostDiffApi(options);
|
|
284
|
+
const ui = { enabled: true, maxRevisions: 20, ...options.ui };
|
|
285
|
+
return definePlugin({
|
|
286
|
+
name: "diff",
|
|
287
|
+
options,
|
|
288
|
+
assets: ui.enabled === false ? [] : [createStyleAsset("diff")],
|
|
289
|
+
clientEntries: ui.enabled === false ? [] : [createClientEntry("diff", "initDiffHistory")],
|
|
290
|
+
onPostProcessed: async (context) => {
|
|
291
|
+
if (ui.enabled === false) return;
|
|
292
|
+
const filePath = resolvePostFilePath(context);
|
|
293
|
+
const history = (await api.getHistory(filePath)).slice(
|
|
294
|
+
0,
|
|
295
|
+
ui.maxRevisions
|
|
296
|
+
);
|
|
297
|
+
const diffs = await buildRevisionDiffs(api, filePath, history);
|
|
298
|
+
context.content.html += renderDiffHistory({
|
|
299
|
+
history,
|
|
300
|
+
diffs
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
function createPostDiffApi(options = {}) {
|
|
306
|
+
const reader = new GitMarkdownHistoryReader(options);
|
|
307
|
+
return {
|
|
308
|
+
getHistory: (filePath) => reader.getHistory(filePath),
|
|
309
|
+
getRevisionMarkdown: (filePath, hash) => reader.getRevisionMarkdown(filePath, hash),
|
|
310
|
+
getCurrentDiff: async (filePath) => {
|
|
311
|
+
const history = await reader.getHistory(filePath);
|
|
312
|
+
const [current, previous] = history;
|
|
313
|
+
if (!current) return null;
|
|
314
|
+
const toMarkdown = await reader.getRevisionMarkdown(
|
|
315
|
+
filePath,
|
|
316
|
+
current.hash
|
|
317
|
+
);
|
|
318
|
+
if (toMarkdown === null) return null;
|
|
319
|
+
const fromMarkdown = previous ? await reader.getRevisionMarkdown(filePath, previous.hash) : null;
|
|
320
|
+
return buildPostDiff({
|
|
321
|
+
from: previous ?? null,
|
|
322
|
+
fromMarkdown,
|
|
323
|
+
to: current,
|
|
324
|
+
toMarkdown
|
|
325
|
+
});
|
|
326
|
+
},
|
|
327
|
+
compareRevisions: async (input) => {
|
|
328
|
+
const history = await reader.getHistory(input.filePath);
|
|
329
|
+
const from = history.find((revision) => revision.hash === input.fromHash);
|
|
330
|
+
const to = history.find((revision) => revision.hash === input.toHash);
|
|
331
|
+
if (!to) return null;
|
|
332
|
+
const [fromMarkdown, toMarkdown] = await Promise.all([
|
|
333
|
+
input.fromHash ? reader.getRevisionMarkdown(input.filePath, input.fromHash) : Promise.resolve(null),
|
|
334
|
+
reader.getRevisionMarkdown(input.filePath, input.toHash)
|
|
335
|
+
]);
|
|
336
|
+
if (toMarkdown === null) return null;
|
|
337
|
+
return buildPostDiff({
|
|
338
|
+
from: from ?? null,
|
|
339
|
+
fromMarkdown,
|
|
340
|
+
to,
|
|
341
|
+
toMarkdown
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
function getPreviousRevision(history, hash) {
|
|
347
|
+
const index = history.findIndex((revision) => revision.hash === hash);
|
|
348
|
+
return index >= 0 ? history[index + 1] ?? null : null;
|
|
349
|
+
}
|
|
350
|
+
async function buildRevisionDiffs(api, filePath, history) {
|
|
351
|
+
const comparisons = [];
|
|
352
|
+
for (let toIndex = 0; toIndex < history.length; toIndex += 1) {
|
|
353
|
+
const to = history[toIndex];
|
|
354
|
+
const previous = getPreviousRevision(history, to.hash);
|
|
355
|
+
comparisons.push({
|
|
356
|
+
filePath,
|
|
357
|
+
fromHash: previous?.hash ?? null,
|
|
358
|
+
toHash: to.hash
|
|
359
|
+
});
|
|
360
|
+
for (let fromIndex = toIndex + 2; fromIndex < history.length; fromIndex += 1) {
|
|
361
|
+
comparisons.push({
|
|
362
|
+
filePath,
|
|
363
|
+
fromHash: history[fromIndex].hash,
|
|
364
|
+
toHash: to.hash
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
const diffs = await Promise.all(
|
|
369
|
+
comparisons.map((comparison) => api.compareRevisions(comparison))
|
|
370
|
+
);
|
|
371
|
+
return diffs.filter((entry) => entry !== null);
|
|
372
|
+
}
|
|
373
|
+
function resolvePostFilePath(context) {
|
|
374
|
+
const indexedPath = context.contentIndex.get(context.slug.toLowerCase());
|
|
375
|
+
const contentPath = `${indexedPath ?? context.slug}.md`;
|
|
376
|
+
return contentPath;
|
|
377
|
+
}
|
|
378
|
+
function buildPostDiff(input) {
|
|
379
|
+
return {
|
|
380
|
+
from: input.from,
|
|
381
|
+
to: input.to,
|
|
382
|
+
lines: createLineDiff(input.fromMarkdown ?? "", input.toMarkdown)
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
export {
|
|
386
|
+
GitMarkdownHistoryReader,
|
|
387
|
+
createLineDiff,
|
|
388
|
+
createPostDiffApi,
|
|
389
|
+
diff,
|
|
390
|
+
renderDiffHistory,
|
|
391
|
+
renderDiffLine,
|
|
392
|
+
renderDiffPanel,
|
|
393
|
+
renderDiffViewer
|
|
394
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { DiffRevision, PostDiff } from "../types.js";
|
|
2
|
+
export type DiffViewerProps = {
|
|
3
|
+
history: DiffRevision[];
|
|
4
|
+
selected: PostDiff;
|
|
5
|
+
};
|
|
6
|
+
export declare function renderDiffViewer({ history, selected, }: DiffViewerProps): string;
|
|
7
|
+
export declare function renderDiffPanel(diff: PostDiff): string;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { DiffRevision, GitHistoryReaderOptions } from "../types.js";
|
|
2
|
+
export declare class GitMarkdownHistoryReader {
|
|
3
|
+
#private;
|
|
4
|
+
constructor(options?: GitHistoryReaderOptions);
|
|
5
|
+
getHistory(filePath: string): Promise<DiffRevision[]>;
|
|
6
|
+
getRevisionMarkdown(filePath: string, hash: string): Promise<string | null>;
|
|
7
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export type DiffLineType = "context" | "added" | "removed";
|
|
2
|
+
export type DiffLine = {
|
|
3
|
+
type: DiffLineType;
|
|
4
|
+
content: string;
|
|
5
|
+
oldLineNumber: number | null;
|
|
6
|
+
newLineNumber: number | null;
|
|
7
|
+
};
|
|
8
|
+
export type DiffRevision = {
|
|
9
|
+
hash: string;
|
|
10
|
+
shortHash: string;
|
|
11
|
+
date: string;
|
|
12
|
+
message: string;
|
|
13
|
+
author: string;
|
|
14
|
+
};
|
|
15
|
+
export type MarkdownRevision = DiffRevision & {
|
|
16
|
+
markdown: string;
|
|
17
|
+
};
|
|
18
|
+
export type PostDiff = {
|
|
19
|
+
from: DiffRevision | null;
|
|
20
|
+
to: DiffRevision;
|
|
21
|
+
lines: DiffLine[];
|
|
22
|
+
};
|
|
23
|
+
export type RevisionComparisonInput = {
|
|
24
|
+
filePath: string;
|
|
25
|
+
fromHash: string | null;
|
|
26
|
+
toHash: string;
|
|
27
|
+
};
|
|
28
|
+
export type GitHistoryReaderOptions = {
|
|
29
|
+
cwd?: string;
|
|
30
|
+
};
|
|
31
|
+
export type DiffPluginUiOptions = {
|
|
32
|
+
enabled?: boolean;
|
|
33
|
+
maxRevisions?: number;
|
|
34
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@riebeckite/plugin-diff",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"source": "./index.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./client": {
|
|
16
|
+
"types": "./dist/client.d.ts",
|
|
17
|
+
"import": "./dist/client.js",
|
|
18
|
+
"source": "./client.ts",
|
|
19
|
+
"default": "./dist/client.js"
|
|
20
|
+
},
|
|
21
|
+
"./style.css": "./style.css"
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@riebeckite/core": "0.0.1"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/node": "^24.5.2"
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"files": [
|
|
33
|
+
"LICENSE",
|
|
34
|
+
"README.md",
|
|
35
|
+
"README_ja.md",
|
|
36
|
+
"dist",
|
|
37
|
+
"style.css"
|
|
38
|
+
],
|
|
39
|
+
"repository": {
|
|
40
|
+
"type": "git",
|
|
41
|
+
"url": "https://github.com/Rerurate514/riebeckite.git",
|
|
42
|
+
"directory": "packages/plugins/diff"
|
|
43
|
+
},
|
|
44
|
+
"homepage": "https://github.com/Rerurate514/riebeckite#readme",
|
|
45
|
+
"bugs": {
|
|
46
|
+
"url": "https://github.com/Rerurate514/riebeckite/issues"
|
|
47
|
+
},
|
|
48
|
+
"description": "Git-backed note diffs and revision history for Riebeckite.",
|
|
49
|
+
"keywords": [
|
|
50
|
+
"riebeckite",
|
|
51
|
+
"plugin",
|
|
52
|
+
"git",
|
|
53
|
+
"diff"
|
|
54
|
+
],
|
|
55
|
+
"engines": {
|
|
56
|
+
"node": "^20.19.0 || >=22.12.0"
|
|
57
|
+
},
|
|
58
|
+
"sideEffects": [
|
|
59
|
+
"./style.css"
|
|
60
|
+
],
|
|
61
|
+
"scripts": {
|
|
62
|
+
"build": "node ../../../scripts/build_package.mjs"
|
|
63
|
+
}
|
|
64
|
+
}
|
package/style.css
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
.rr-diff-history {
|
|
2
|
+
margin-block: 4rem 2rem;
|
|
3
|
+
border-block-start: 1px solid
|
|
4
|
+
var(--rb-color-border, color-mix(in srgb, currentColor 18%, transparent));
|
|
5
|
+
padding-block-start: 1.5rem;
|
|
6
|
+
color: var(--rb-color-ink, currentColor);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
.rr-diff-history__header {
|
|
10
|
+
display: flex;
|
|
11
|
+
align-items: baseline;
|
|
12
|
+
justify-content: space-between;
|
|
13
|
+
gap: 1rem;
|
|
14
|
+
margin-block-end: 1rem;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
.rr-diff-history__title {
|
|
18
|
+
margin: 0;
|
|
19
|
+
font-size: 0.875rem;
|
|
20
|
+
letter-spacing: 0.08em;
|
|
21
|
+
text-transform: uppercase;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
.rr-diff-history__count,
|
|
25
|
+
.rr-diff-history__date,
|
|
26
|
+
.rr-diff-history__meta {
|
|
27
|
+
color: var(
|
|
28
|
+
--rb-color-muted,
|
|
29
|
+
color-mix(in srgb, currentColor 62%, transparent)
|
|
30
|
+
);
|
|
31
|
+
font-size: 0.8125rem;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
.rr-diff-history__layout {
|
|
35
|
+
display: grid;
|
|
36
|
+
grid-template-columns: minmax(12rem, 0.36fr) minmax(0, 1fr);
|
|
37
|
+
gap: 1.5rem;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
.rr-diff-history__list {
|
|
41
|
+
display: grid;
|
|
42
|
+
gap: 0;
|
|
43
|
+
margin: 0;
|
|
44
|
+
padding: 0;
|
|
45
|
+
list-style: none;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
.rr-diff-history__item {
|
|
49
|
+
position: relative;
|
|
50
|
+
padding-inline-start: 1.25rem;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
.rr-diff-history__item::before {
|
|
54
|
+
position: absolute;
|
|
55
|
+
inset-block-start: 0.7rem;
|
|
56
|
+
inset-inline-start: 0;
|
|
57
|
+
width: 0.5rem;
|
|
58
|
+
height: 0.5rem;
|
|
59
|
+
border: 1px solid var(--rb-color-ink, currentColor);
|
|
60
|
+
border-radius: 999px;
|
|
61
|
+
background: var(--rb-color-paper, Canvas);
|
|
62
|
+
content: "";
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
.rr-diff-history__item:not(:last-child)::after {
|
|
66
|
+
position: absolute;
|
|
67
|
+
inset-block: 1.25rem -0.25rem;
|
|
68
|
+
inset-inline-start: 0.25rem;
|
|
69
|
+
width: 1px;
|
|
70
|
+
background: var(
|
|
71
|
+
--rb-color-border,
|
|
72
|
+
color-mix(in srgb, currentColor 18%, transparent)
|
|
73
|
+
);
|
|
74
|
+
content: "";
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
.rr-diff-history__commit {
|
|
78
|
+
display: grid;
|
|
79
|
+
width: 100%;
|
|
80
|
+
border: 0;
|
|
81
|
+
padding: 0 0 1rem;
|
|
82
|
+
background: transparent;
|
|
83
|
+
color: inherit;
|
|
84
|
+
font: inherit;
|
|
85
|
+
text-align: start;
|
|
86
|
+
cursor: pointer;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
.rr-diff-history__message {
|
|
90
|
+
overflow-wrap: anywhere;
|
|
91
|
+
font-size: 0.9375rem;
|
|
92
|
+
line-height: 1.45;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
.rr-diff-history__commit:hover .rr-diff-history__message,
|
|
96
|
+
.rr-diff-history__commit[aria-pressed="true"] .rr-diff-history__message {
|
|
97
|
+
color: var(--rb-color-accent, #f66620);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
.rr-diff-history__commit:focus-visible,
|
|
101
|
+
.rr-diff-history__controls select:focus-visible {
|
|
102
|
+
outline: 2px solid var(--rb-color-accent, #f66620);
|
|
103
|
+
outline-offset: 2px;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
.rr-diff-history__viewer {
|
|
107
|
+
min-width: 0;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
.rr-diff-history__controls {
|
|
111
|
+
display: flex;
|
|
112
|
+
flex-wrap: wrap;
|
|
113
|
+
gap: 0.75rem;
|
|
114
|
+
margin-block-end: 0.75rem;
|
|
115
|
+
font-size: 0.8125rem;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
.rr-diff-history__controls label {
|
|
119
|
+
display: inline-flex;
|
|
120
|
+
align-items: center;
|
|
121
|
+
gap: 0.5rem;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
.rr-diff-history__controls select {
|
|
125
|
+
border: 1px solid
|
|
126
|
+
var(--rb-color-border, color-mix(in srgb, currentColor 18%, transparent));
|
|
127
|
+
border-radius: 0;
|
|
128
|
+
padding: 0.25rem 0.5rem;
|
|
129
|
+
background: var(--rb-color-paper, Canvas);
|
|
130
|
+
color: inherit;
|
|
131
|
+
font: inherit;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
.rr-diff-history__diff {
|
|
135
|
+
border: 1px solid
|
|
136
|
+
var(--rb-color-border, color-mix(in srgb, currentColor 18%, transparent));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
.rr-diff-history__diff-head {
|
|
140
|
+
border-block-end: 1px solid
|
|
141
|
+
var(--rb-color-border, color-mix(in srgb, currentColor 18%, transparent));
|
|
142
|
+
padding: 0.625rem 0.75rem;
|
|
143
|
+
font-family: var(
|
|
144
|
+
--rb-font-mono,
|
|
145
|
+
ui-monospace,
|
|
146
|
+
SFMono-Regular,
|
|
147
|
+
Consolas,
|
|
148
|
+
monospace
|
|
149
|
+
);
|
|
150
|
+
font-size: 0.8125rem;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
.rr-diff-history__table-wrap {
|
|
154
|
+
overflow-x: auto;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
.rr-diff-history__table {
|
|
158
|
+
width: 100%;
|
|
159
|
+
min-width: 42rem;
|
|
160
|
+
border-collapse: collapse;
|
|
161
|
+
font-family: var(
|
|
162
|
+
--rb-font-mono,
|
|
163
|
+
ui-monospace,
|
|
164
|
+
SFMono-Regular,
|
|
165
|
+
Consolas,
|
|
166
|
+
monospace
|
|
167
|
+
);
|
|
168
|
+
font-size: 0.8125rem;
|
|
169
|
+
line-height: 1.55;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
.rr-diff-history__num {
|
|
173
|
+
width: 3.25rem;
|
|
174
|
+
border-inline-end: 1px solid
|
|
175
|
+
var(--rb-color-border, color-mix(in srgb, currentColor 14%, transparent));
|
|
176
|
+
color: var(
|
|
177
|
+
--rb-color-muted,
|
|
178
|
+
color-mix(in srgb, currentColor 58%, transparent)
|
|
179
|
+
);
|
|
180
|
+
text-align: end;
|
|
181
|
+
user-select: none;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
.rr-diff-history__num,
|
|
185
|
+
.rr-diff-history__code {
|
|
186
|
+
padding: 0.125rem 0.625rem;
|
|
187
|
+
vertical-align: top;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
.rr-diff-history__code code {
|
|
191
|
+
white-space: pre;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
.rr-diff-history__line--added {
|
|
195
|
+
background: color-mix(in srgb, #2f9e44 16%, transparent);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
.rr-diff-history__line--removed {
|
|
199
|
+
background: color-mix(in srgb, #e03131 14%, transparent);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
.rr-diff-history__line--context {
|
|
203
|
+
background: color-mix(in srgb, currentColor 2%, transparent);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
.rr-diff-history__empty {
|
|
207
|
+
margin: 0;
|
|
208
|
+
color: var(
|
|
209
|
+
--rb-color-muted,
|
|
210
|
+
color-mix(in srgb, currentColor 62%, transparent)
|
|
211
|
+
);
|
|
212
|
+
font-size: 0.9375rem;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
@media (max-width: 720px) {
|
|
216
|
+
.rr-diff-history__layout {
|
|
217
|
+
grid-template-columns: 1fr;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
.rr-diff-history__list {
|
|
221
|
+
max-height: 18rem;
|
|
222
|
+
overflow: auto;
|
|
223
|
+
padding-inline-end: 0.25rem;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
.rr-diff-history__header {
|
|
227
|
+
align-items: flex-start;
|
|
228
|
+
}
|
|
229
|
+
}
|