the slugs tests typeScript
Some checks failed
Node.js build / build (20.x) (push) Has been cancelled
Node.js build / build (22.x) (push) Has been cancelled
Node.js build / build (24.x) (push) Has been cancelled
Release Obsidian plugin / build (push) Has been cancelled

This commit is contained in:
2026-07-28 16:52:46 -03:00
parent 1c4aa2a263
commit 1bcd6b6a97
3 changed files with 4055 additions and 0 deletions

28
src/slug.ts Normal file
View File

@@ -0,0 +1,28 @@
// Pure string logic for zettelclean. No Obsidian imports, so it is unit-testable
// 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.
// 12-18 because collision-avoidance may append extra digits to a 14-digit stamp.
const TIMEID_RE = /^(\d{12,18})(?=-|$)/;
// Return the immutable id prefix, or null if this file is not a zettel.
export function extractTimeId(basename: string): string | null {
const m = basename.match(TIMEID_RE);
return m?.[1] ?? null;
}
// H1 text -> kebab slug. sanifize as inspiration, deliberately simpler.
export function generateSlug(heading: string): string {
return heading
.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(/^-+|-+$/g, ''); // trim leading/trailing dashes
}
// Reassemble; an empty slug collapses to the bare id.
export function buildFilename(id: string, slug: string): string {
return slug ? `${id}-${slug}` : id;
}