Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 35cb6dd976 | |||
| e83b95c6ee |
12
.github/workflows/lint.yml
vendored
12
.github/workflows/lint.yml
vendored
@@ -42,7 +42,11 @@ jobs:
|
||||
uses: actions/setup-node@v6
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
cache: 'npm'
|
||||
- run: npm ci
|
||||
- run: npm run build --if-present
|
||||
- run: npm run lint
|
||||
# Node ships corepack, which selects the pnpm version pinned in
|
||||
# package.json ("packageManager"). If this self-hosted runner has no
|
||||
# Node, drop the setup-node step above and rely on a preinstalled Node.
|
||||
- run: corepack enable
|
||||
- run: pnpm install --frozen-lockfile
|
||||
- run: pnpm run build
|
||||
- run: pnpm run lint
|
||||
- run: pnpm test
|
||||
|
||||
11
.github/workflows/release.yml
vendored
11
.github/workflows/release.yml
vendored
@@ -40,12 +40,17 @@ jobs:
|
||||
uses: actions/setup-node@v6
|
||||
with:
|
||||
node-version: 24
|
||||
cache: 'npm'
|
||||
|
||||
# Node ships corepack, which selects the pnpm version pinned in
|
||||
# package.json ("packageManager"). If this self-hosted runner has no
|
||||
# Node, drop the setup-node step above and rely on a preinstalled Node.
|
||||
- name: Enable pnpm
|
||||
run: corepack enable
|
||||
|
||||
- name: Build plugin
|
||||
run: |
|
||||
npm ci
|
||||
npm run build
|
||||
pnpm install --frozen-lockfile
|
||||
pnpm run build
|
||||
|
||||
- name: Check for optional styles
|
||||
id: styles
|
||||
|
||||
65
README.md
65
README.md
@@ -38,17 +38,18 @@ slug tail follows. Nothing else is touched.
|
||||
## How it works
|
||||
|
||||
- A note is a zettel when its filename starts with a **TIMEID**: a run of 12 to 18 digits
|
||||
(a timestamp), for example `20260728145404-my-note.md`. Files without that prefix are
|
||||
ignored, so plain notes, daily notes, Excalidraw and Kanban files are never renamed.
|
||||
(a timestamp) followed by any non-digit or the end of the name - so `20260728145404-my-note.md`,
|
||||
`202607281454.md`, and even `202607281454 my note.md` all qualify. Files without that prefix
|
||||
are ignored, so plain notes, daily notes, Excalidraw and Kanban files are never renamed.
|
||||
- The TIMEID is minted **once** and never changes. Only the slug tail is regenerated.
|
||||
- When you edit the H1, zettelclean waits about 5 seconds of idle, then renames the file to
|
||||
`TIMEID-slug(H1).md` using Obsidian's own rename (so backlinks are updated). A 10 second
|
||||
- When you edit the H1, zettelclean waits about 4 seconds of idle, then renames the file to
|
||||
`TIMEID-slug(H1).md` using Obsidian's own rename (so backlinks are updated). A 6 second
|
||||
cooldown prevents a just-renamed file from being renamed again.
|
||||
- Sync is strictly one-way, H1 to filename. Renaming a file by hand is never fought.
|
||||
- Delete the H1 and the filename collapses back to the bare `TIMEID.md`.
|
||||
|
||||
The slug is lowercase, ASCII, dash-separated: accents are folded (`Ciências` becomes
|
||||
`ciencias`), runs of punctuation and whitespace collapse to a single dash, and leading and
|
||||
The slug is case-preserving, ASCII, dash-separated: accents are folded (`Ciências` becomes
|
||||
`Ciencias`), runs of punctuation and whitespace collapse to a single dash, and leading and
|
||||
trailing dashes are trimmed.
|
||||
|
||||
## Creating notes
|
||||
@@ -67,9 +68,16 @@ change the setting.
|
||||
### Retrofitting existing notes
|
||||
|
||||
Notes created before you adopted this workflow have no TIMEID. Right-click such a note in the
|
||||
file explorer and choose **"Add zettel TIMEID to filename"**. It mints a fresh timestamp,
|
||||
file explorer and choose **"Prefix timestamp to filename"**. It mints a fresh timestamp,
|
||||
prepends it, and the note is now a zettel that syncs on future H1 edits.
|
||||
|
||||
## Slugging a folder name
|
||||
|
||||
Right-click a folder and choose **"Slug folder name"** to ASCII-clean it in place (this folder
|
||||
only): `Citações` becomes `Citacoes`. The rename goes through Obsidian, so `[[links]]` into the
|
||||
folder are updated. It aborts if a sibling of that name already exists, and never touches the
|
||||
vault root.
|
||||
|
||||
## Pretty sidebar (optional)
|
||||
|
||||
By default the file explorer shows the slugged filename (with dashes). If you want the sidebar,
|
||||
@@ -84,19 +92,48 @@ That reads the first H1 directly and falls back to the filename when there is no
|
||||
|
||||
## Settings
|
||||
|
||||
None. Zettelclean is zero-config in this version. The 5 second settle and 10 second cooldown
|
||||
None. Zettelclean is zero-config in this version. The 4 second settle and 6 second cooldown
|
||||
are fixed.
|
||||
|
||||
## Development
|
||||
|
||||
- `npm install` (or `pnpm install`).
|
||||
- `npm run dev` - compile in watch mode.
|
||||
- `npm run build` - typecheck and produce `main.js`.
|
||||
- `npm test` - run the slug unit tests on Node's built-in test runner.
|
||||
- `npm run lint` - eslint with the Obsidian ruleset.
|
||||
Dependencies are managed with **pnpm** (see "Why pnpm, not npm" below). Node 22+ ships
|
||||
`corepack`, so you do not need to install pnpm globally, and the `packageManager` field in
|
||||
`package.json` pins the exact version.
|
||||
|
||||
```bash
|
||||
corepack pnpm install # first time, or after a dependency change
|
||||
corepack pnpm run dev # compile in watch mode
|
||||
corepack pnpm run build # typecheck and produce main.js
|
||||
corepack pnpm test # run the slug unit tests (Node's built-in runner)
|
||||
corepack pnpm run lint # eslint with the Obsidian ruleset
|
||||
```
|
||||
|
||||
The pure string logic lives in `src/slug.ts` and is unit-tested in isolation; the Obsidian
|
||||
wiring lives in `src/main.ts`.
|
||||
wiring lives in `src/main.ts`. Tests run on Node's built-in test runner (`node --test`) - there
|
||||
is no vitest or jest dependency.
|
||||
|
||||
### Why pnpm, not npm
|
||||
|
||||
**Do not run `npm install` or `npm ci` in this repo.** This project is developed on a **ZFS**
|
||||
filesystem, and npm's installer renames many directories in parallel (to hoist and dedupe
|
||||
packages). That races ZFS's directory-metadata handling and aborts with:
|
||||
|
||||
```
|
||||
npm error code ENOTEMPTY
|
||||
npm error syscall rename
|
||||
```
|
||||
|
||||
pnpm avoids the problem entirely: it hard-links packages from a global content-addressable
|
||||
store instead of renaming directories into place, so it installs cleanly on the same disk.
|
||||
|
||||
Notes for contributors:
|
||||
|
||||
- Only `npm install` / `npm ci` are affected. Running *scripts* through npm (`npm run build`)
|
||||
still works, because that just spawns tsc/esbuild and installs nothing. Still, prefer `pnpm`
|
||||
everywhere for consistency.
|
||||
- The lockfile is `pnpm-lock.yaml` (committed). There is no `package-lock.json`; do not create
|
||||
one.
|
||||
|
||||
## License
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"id": "zettelclean",
|
||||
"name": "Zettelclean",
|
||||
"version": "0.3.0",
|
||||
"version": "0.5.0",
|
||||
"minAppVersion": "1.0.0",
|
||||
"description": "Keep a Zettelkasten filename's slug in sync with the note's H1 heading.",
|
||||
"author": "Ruben Carlo Benante",
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
{
|
||||
"name": "zettelclean",
|
||||
"version": "0.3.0",
|
||||
"version": "0.5.0",
|
||||
"description": "Keep a Zettelkasten filename's slug in sync with the note's H1 heading.",
|
||||
"author": "Ruben Carlo Benante <rcb@beco.cc>",
|
||||
"main": "main.js",
|
||||
"type": "module",
|
||||
"packageManager": "pnpm@9.15.9",
|
||||
"scripts": {
|
||||
"dev": "node esbuild.config.mjs",
|
||||
"build": "tsc -noEmit -skipLibCheck && node esbuild.config.mjs production",
|
||||
|
||||
840
pnpm-lock.yaml
generated
840
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
59
src/main.ts
59
src/main.ts
@@ -19,11 +19,19 @@
|
||||
// * rcb@beco.cc *
|
||||
// *************************************************************************
|
||||
|
||||
import { Plugin, TFile, TAbstractFile, Menu, moment } from 'obsidian';
|
||||
import {
|
||||
Plugin,
|
||||
TFile,
|
||||
TAbstractFile,
|
||||
TFolder,
|
||||
Menu,
|
||||
Notice,
|
||||
moment,
|
||||
} from 'obsidian';
|
||||
import { extractTimeId, generateSlug, buildFilename } from './slug';
|
||||
|
||||
const SETTLE_MS = 5000; // rename ~5s after the H1 stops changing
|
||||
const COOLDOWN_MS = 10000; // do not rename the same path again within 10s
|
||||
const SETTLE_MS = 4000; // rename ~4s after the H1 stops changing
|
||||
const COOLDOWN_MS = 6000; // do not rename the same path again within 6s
|
||||
|
||||
export default class ZettelcleanPlugin extends Plugin {
|
||||
private renameTimers = new Map<string, number>();
|
||||
@@ -37,11 +45,12 @@ export default class ZettelcleanPlugin extends Plugin {
|
||||
this.scheduleSync(file),
|
||||
),
|
||||
);
|
||||
// Retrofit entry in the file-explorer right-click menu, near "Rename".
|
||||
// File-explorer right-click menu: retrofit item for files, slug item for folders.
|
||||
this.registerEvent(
|
||||
this.app.workspace.on('file-menu', (menu, file) =>
|
||||
this.addTimeIdMenu(menu, file),
|
||||
),
|
||||
this.app.workspace.on('file-menu', (menu, file) => {
|
||||
this.addTimeIdMenu(menu, file);
|
||||
this.addFolderSlugMenu(menu, file);
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -56,8 +65,8 @@ export default class ZettelcleanPlugin extends Plugin {
|
||||
return `${dir}${base}.md`;
|
||||
}
|
||||
|
||||
// Guard layer: only the actively edited zettel, honoring the 10s cooldown,
|
||||
// (re)arming the 5s settle timer on every cache change.
|
||||
// Guard layer: only the actively edited zettel, honoring the 6s cooldown,
|
||||
// (re)arming the 4s settle timer on every cache change.
|
||||
private scheduleSync(file: TAbstractFile) {
|
||||
if (this.isRenameInProgress) return; // our own rename, ignore
|
||||
if (!(file instanceof TFile) || file.extension !== 'md') return;
|
||||
@@ -65,7 +74,7 @@ export default class ZettelcleanPlugin extends Plugin {
|
||||
if (this.app.workspace.getActiveFile() !== file) return; // only the edited note
|
||||
|
||||
const last = this.lastRenamedAt.get(file.path);
|
||||
if (last && Date.now() - last < COOLDOWN_MS) return; // 10s cooldown
|
||||
if (last && Date.now() - last < COOLDOWN_MS) return; // 6s cooldown
|
||||
|
||||
const existing = this.renameTimers.get(file.path);
|
||||
if (existing) window.clearTimeout(existing);
|
||||
@@ -106,7 +115,7 @@ export default class ZettelcleanPlugin extends Plugin {
|
||||
if (extractTimeId(file.basename) !== null) return; // already a zettel
|
||||
menu.addItem((item) =>
|
||||
item
|
||||
.setTitle('Add zettel TIMEID to filename')
|
||||
.setTitle('Prefix timestamp to filename')
|
||||
.setIcon('clock')
|
||||
.onClick(() => void this.addTimeId(file)),
|
||||
);
|
||||
@@ -128,4 +137,32 @@ export default class ZettelcleanPlugin extends Plugin {
|
||||
}
|
||||
await this.app.fileManager.renameFile(file, target);
|
||||
}
|
||||
|
||||
// Folder right-click: slug the folder's own name (this folder only, instant).
|
||||
private addFolderSlugMenu(menu: Menu, file: TAbstractFile) {
|
||||
if (!(file instanceof TFolder) || file.isRoot()) return; // never the vault root
|
||||
menu.addItem((item) =>
|
||||
item
|
||||
.setTitle('Slug folder name')
|
||||
.setIcon('folder')
|
||||
.onClick(() => void this.slugFolder(file)),
|
||||
);
|
||||
}
|
||||
|
||||
private async slugFolder(folder: TFolder) {
|
||||
const newName = generateSlug(folder.name);
|
||||
if (!newName || newName === folder.name) {
|
||||
new Notice('Folder name is already clean');
|
||||
return;
|
||||
}
|
||||
const parent = folder.parent;
|
||||
const dir = parent && parent.path !== '/' ? `${parent.path}/` : '';
|
||||
const newPath = `${dir}${newName}`; // folders have no extension
|
||||
if (this.app.vault.getAbstractFileByPath(newPath)) {
|
||||
new Notice(`Cannot rename: "${newName}" already exists`);
|
||||
return;
|
||||
}
|
||||
await this.app.fileManager.renameFile(folder, newPath); // updates [[links]]
|
||||
new Notice(`Renamed folder "${folder.name}" -> "${newName}"`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,15 +23,16 @@ import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { extractTimeId, generateSlug, buildFilename } from './slug.ts';
|
||||
|
||||
test('slug folds accents, lowercases, collapses', () => {
|
||||
test('slug folds accents and collapses, preserving case', () => {
|
||||
assert.equal(
|
||||
generateSlug('Ciências Políticas - Visão Geral'),
|
||||
'ciencias-politicas-visao-geral',
|
||||
'Ciencias-Politicas-Visao-Geral',
|
||||
);
|
||||
assert.equal(generateSlug('Citações'), 'Citacoes');
|
||||
});
|
||||
|
||||
test('slug collapses runs of punctuation and whitespace to one dash', () => {
|
||||
assert.equal(generateSlug(' Hello, World!! '), 'hello-world');
|
||||
assert.equal(generateSlug(' Hello, World!! '), 'Hello-World');
|
||||
assert.equal(generateSlug('a/b:c*d?e'), 'a-b-c-d-e');
|
||||
});
|
||||
|
||||
@@ -40,11 +41,13 @@ test('empty or heading-less input yields empty slug', () => {
|
||||
assert.equal(generateSlug(' --- '), '');
|
||||
});
|
||||
|
||||
test('extractTimeId accepts 12/14/18-digit prefixes, rejects others', () => {
|
||||
assert.equal(extractTimeId('202607281454-x'), '202607281454'); // 12
|
||||
test('extractTimeId accepts 12/14/18-digit prefixes and a non-dash boundary', () => {
|
||||
assert.equal(extractTimeId('202607281454-x'), '202607281454'); // 12, dash
|
||||
assert.equal(extractTimeId('20260728145404-x'), '20260728145404'); // 14
|
||||
assert.equal(extractTimeId('202607281454049999-x'), '202607281454049999'); // 18
|
||||
assert.equal(extractTimeId('20260728145404'), '20260728145404'); // bare id
|
||||
assert.equal(extractTimeId('202607290947 test pkm'), '202607290947'); // space
|
||||
assert.equal(extractTimeId('202607290947test'), '202607290947'); // letters
|
||||
assert.equal(extractTimeId('my-note'), null);
|
||||
assert.equal(extractTimeId('2026-budget'), null); // too short
|
||||
assert.equal(extractTimeId('2026072814540499999-x'), null); // 19, too long
|
||||
|
||||
15
src/slug.ts
15
src/slug.ts
@@ -23,9 +23,10 @@
|
||||
// in isolation. The transform is always one-way (H1 -> filename), which lets us
|
||||
// freely drop characters without worrying about a reverse mapping.
|
||||
|
||||
// A TIMEID is a leading run of 12 to 18 digits, ending at a dash or end-of-name.
|
||||
// A TIMEID is the leading run of 12 to 18 digits, ending at any non-digit or
|
||||
// end-of-name (so "202607290947 test" and "202607290947-slug" both qualify).
|
||||
// 12-18 because collision-avoidance may append extra digits to a 14-digit stamp.
|
||||
const TIMEID_RE = /^(\d{12,18})(?=-|$)/;
|
||||
const TIMEID_RE = /^(\d{12,18})(?=\D|$)/;
|
||||
|
||||
// Return the immutable id prefix, or null if this file is not a zettel.
|
||||
export function extractTimeId(basename: string): string | null {
|
||||
@@ -33,13 +34,13 @@ export function extractTimeId(basename: string): string | null {
|
||||
return m?.[1] ?? null;
|
||||
}
|
||||
|
||||
// H1 text -> kebab slug. sanifize as inspiration, deliberately simpler.
|
||||
export function generateSlug(heading: string): string {
|
||||
return heading
|
||||
// text (an H1 or a folder name) -> ASCII, case-preserving, dash-separated slug.
|
||||
// Shared by the file-rename sync and the folder-slug menu.
|
||||
export function generateSlug(text: string): string {
|
||||
return text
|
||||
.normalize('NFKD') // split accented letters into base + combining mark
|
||||
.replace(/[̀-ͯ]/g, '') // strip the combining marks (c-cedilla -> c)
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, '-') // any run of non-alphanumerics -> one dash
|
||||
.replace(/[^a-zA-Z0-9]+/g, '-') // non-alphanumerics -> one dash (case kept)
|
||||
.replace(/^-+|-+$/g, ''); // trim leading/trailing dashes
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
{
|
||||
"0.3.0": "1.0.0"
|
||||
"0.5.0": "1.0.0"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user