Color mode

Diff

Git-backed diff and revision history for Markdown notes: read commit history, retrieve past revisions, and compute line-level diffs between them.

日本語

Overview

createPostDiffApi() wraps a local Git repository and exposes revision history plus line diffs for any Markdown file, even when the note was renamed or moved (git log --follow). createLineDiff() is the pure diff engine used by the API and is also exported for standalone use.

The diff() plugin registers a diff entry in the plugin list; the programmatic API is the primary interface.

Usage (plugin)

ts
import { defineConfig } from "@riebeckite/core";
import { diff } from "@riebeckite/plugin-diff";
 
export default defineConfig({
  // ...
  plugins: [diff({ cwd: "./content" })],
});

diff(options?) accepts the same GitHistoryReaderOptions as the API. The revision panel renders the initial comparison during the build and computes other selected comparisons in the browser. It embeds each available revision once rather than every pairwise diff, keeping generated pages compact.

Programmatic API

ts
import { createPostDiffApi } from "@riebeckite/plugin-diff";
 
const api = createPostDiffApi({ cwd: "./content" });
 
const history = await api.getHistory("notes/hello.md");
const previous = await api.getRevisionMarkdown("notes/hello.md", history[1].hash);
const current = await api.getCurrentDiff("notes/hello.md");
const compare = await api.compareRevisions({
  filePath: "notes/hello.md",
  fromHash: history[1].hash,
  toHash: history[0].hash,
});
  • getHistory(filePath) — commit history for the file (newest first) as DiffRevision[]
  • getRevisionMarkdown(filePath, hash) — Markdown source at a given revision, or null
  • getCurrentDiff(filePath) — line diff between the latest revision and its predecessor (or from an empty source when there is no predecessor)
  • compareRevisions({ filePath, fromHash, toHash }) — line diff between two revisions; fromHash: null diffs from an empty source

Options

createPostDiffApi(options?) accepts GitHistoryReaderOptions:

Option Type Default Description
cwd string config.content.directory, else process.cwd() Content root used to locate the Git work tree

When cwd is not inside a Git repository, API calls resolve to empty results ([] / null) instead of throwing.

Types

Type Description
DiffRevision Commit metadata: hash, shortHash, date, message, author
MarkdownRevision DiffRevision with the Markdown source
PostDiff from, to, and lines
DiffLine A single diff line: { type, content }
DiffLineType "context" | "added" | "removed"
RevisionComparisonInput { filePath, fromHash: string | null, toHash }

Exports

  • diff(options?) — plugin factory
  • createPostDiffApi(options?) — programmatic API factory
  • createLineDiff(from, to) — LCS-based line diff
  • GitMarkdownHistoryReader — Git-backed history reader class
  • Types listed above

See also

History

1 changesCollapseExpand
1 + <!-- Generated from packages/plugins/diff/README.md. Do not edit this page directly; edit the package README and run `pnpm docs:sync`. -->
2 +
3 + # Diff
4 +
5 + Git-backed diff and revision history for Markdown notes: read commit history,
6 + retrieve past revisions, and compute line-level diffs between them.
7 +
8 + [日本語](./diff.md)
9 +
10 + ## Overview
11 +
12 + `createPostDiffApi()` wraps a local Git repository and exposes revision
13 + history plus line diffs for any Markdown file, even when the note was renamed
14 + or moved (`git log --follow`). `createLineDiff()` is the pure diff engine used
15 + by the API and is also exported for standalone use.
16 +
17 + The `diff()` plugin registers a `diff` entry in the plugin list; the
18 + programmatic API is the primary interface.
19 +
20 + ## Usage (plugin)
21 +
22 + ```ts
23 + import { defineConfig } from "@riebeckite/core";
24 + import { diff } from "@riebeckite/plugin-diff";
25 +
26 + export default defineConfig({
27 + // ...
28 + plugins: [diff({ cwd: "./content" })],
29 + });
30 + ```
31 +
32 + `diff(options?)` accepts the same `GitHistoryReaderOptions` as the API.
33 + The revision panel renders the initial comparison during the build and computes
34 + other selected comparisons in the browser. It embeds each available revision
35 + once rather than every pairwise diff, keeping generated pages compact.
36 +
37 + ## Programmatic API
38 +
39 + ```ts
40 + import { createPostDiffApi } from "@riebeckite/plugin-diff";
41 +
42 + const api = createPostDiffApi({ cwd: "./content" });
43 +
44 + const history = await api.getHistory("notes/hello.md");
45 + const previous = await api.getRevisionMarkdown("notes/hello.md", history[1].hash);
46 + const current = await api.getCurrentDiff("notes/hello.md");
47 + const compare = await api.compareRevisions({
48 + filePath: "notes/hello.md",
49 + fromHash: history[1].hash,
50 + toHash: history[0].hash,
51 + });
52 + ```
53 +
54 + - `getHistory(filePath)` — commit history for the file (newest first) as
55 + `DiffRevision[]`
56 + - `getRevisionMarkdown(filePath, hash)` — Markdown source at a given revision,
57 + or `null`
58 + - `getCurrentDiff(filePath)` — line diff between the latest revision and its
59 + predecessor (or from an empty source when there is no predecessor)
60 + - `compareRevisions({ filePath, fromHash, toHash })` — line diff between two
61 + revisions; `fromHash: null` diffs from an empty source
62 +
63 + ## Options
64 +
65 + `createPostDiffApi(options?)` accepts `GitHistoryReaderOptions`:
66 +
67 + | Option | Type | Default | Description |
68 + | ------ | ---- | ------- | ----------- |
69 + | `cwd` | `string` | `config.content.directory`, else `process.cwd()` | Content root used to locate the Git work tree |
70 +
71 + When `cwd` is not inside a Git repository, API calls resolve to empty results
72 + (`[]` / `null`) instead of throwing.
73 +
74 + ## Types
75 +
76 + | Type | Description |
77 + | ---- | ----------- |
78 + | `DiffRevision` | Commit metadata: `hash`, `shortHash`, `date`, `message`, `author` |
79 + | `MarkdownRevision` | `DiffRevision` with the Markdown source |
80 + | `PostDiff` | `from`, `to`, and `lines` |
81 + | `DiffLine` | A single diff line: `{ type, content }` |
82 + | `DiffLineType` | `"context" \| "added" \| "removed"` |
83 + | `RevisionComparisonInput` | `{ filePath, fromHash: string \| null, toHash }` |
84 +
85 + ## Exports
86 +
87 + - `diff(options?)` — plugin factory
88 + - `createPostDiffApi(options?)` — programmatic API factory
89 + - `createLineDiff(from, to)` — LCS-based line diff
90 + - `GitMarkdownHistoryReader` — Git-backed history reader class
91 + - Types listed above
92 +
93 + ## See also
94 +
95 + - [Plugin guide](../reference/plugin-api.en.md)
96 +