the slugs tests typeScript
This commit is contained in:
3989
pnpm-lock.yaml
generated
Normal file
3989
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
38
src/slug.test.ts
Normal file
38
src/slug.test.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { extractTimeId, generateSlug, buildFilename } from './slug.ts';
|
||||
|
||||
test('slug folds accents, lowercases, collapses', () => {
|
||||
assert.equal(
|
||||
generateSlug('Ciências Políticas - Visão Geral'),
|
||||
'ciencias-politicas-visao-geral',
|
||||
);
|
||||
});
|
||||
|
||||
test('slug collapses runs of punctuation and whitespace to one dash', () => {
|
||||
assert.equal(generateSlug(' Hello, World!! '), 'hello-world');
|
||||
assert.equal(generateSlug('a/b:c*d?e'), 'a-b-c-d-e');
|
||||
});
|
||||
|
||||
test('empty or heading-less input yields empty slug', () => {
|
||||
assert.equal(generateSlug(''), '');
|
||||
assert.equal(generateSlug(' --- '), '');
|
||||
});
|
||||
|
||||
test('extractTimeId accepts 12/14/18-digit prefixes, rejects others', () => {
|
||||
assert.equal(extractTimeId('202607281454-x'), '202607281454'); // 12
|
||||
assert.equal(extractTimeId('20260728145404-x'), '20260728145404'); // 14
|
||||
assert.equal(extractTimeId('202607281454049999-x'), '202607281454049999'); // 18
|
||||
assert.equal(extractTimeId('20260728145404'), '20260728145404'); // bare id
|
||||
assert.equal(extractTimeId('my-note'), null);
|
||||
assert.equal(extractTimeId('2026-budget'), null); // too short
|
||||
assert.equal(extractTimeId('2026072814540499999-x'), null); // 19, too long
|
||||
});
|
||||
|
||||
test('buildFilename collapses empty slug to bare id', () => {
|
||||
assert.equal(
|
||||
buildFilename('20260728145404', 'some-title'),
|
||||
'20260728145404-some-title',
|
||||
);
|
||||
assert.equal(buildFilename('20260728145404', ''), '20260728145404');
|
||||
});
|
||||
28
src/slug.ts
Normal file
28
src/slug.ts
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user