Files
zettelclean/src/slug.test.ts
Ruben Carlo Benante adcb9e5d88
Some checks failed
Zettelclean CI / build (20.x) (push) Has been cancelled
Zettelclean CI / build (22.x) (push) Has been cancelled
Zettelclean CI / build (24.x) (push) Has been cancelled
Release Zettelclean / build (push) Has been cancelled
date-prefix and sticky tail v0.6 for Obsidian v1.12.7 and v1.13.0 (min v1.0.0)
2026-07-30 18:16:52 -03:00

97 lines
4.7 KiB
TypeScript

// *************************************************************************
// * (C)opyright 2026 by Ruben Carlo Benante *
// * *
// * This program is free software; you can redistribute it and/or modify *
// * it under the terms of the GNU General Public License as published by *
// * the Free Software Foundation, either version 3 of the License, or *
// * (at your option) any later version. *
// * *
// * This program is distributed in the hope that it will be useful, *
// * but WITHOUT ANY WARRANTY; without even the implied warranty of *
// * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
// * GNU General Public License for more details. *
// * *
// * You should have received a copy of the GNU General Public License *
// * along with this program. If not, see http://www.gnu.org/licenses/. *
// * *
// * Contact author at: *
// * Ruben Carlo Benante *
// * rcb@beco.cc *
// *************************************************************************
import { test } from 'node:test';
import assert from 'node:assert/strict';
import {
extractTimeId,
extractPrefix,
computeBasename,
generateSlug,
buildFilename,
} from './slug.ts';
test('slug folds accents and collapses, preserving case', () => {
assert.equal(
generateSlug('Ciências Políticas - Visão 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('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 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
});
test('buildFilename collapses empty slug to bare id', () => {
assert.equal(
buildFilename('20260728145404', 'some-title'),
'20260728145404-some-title',
);
assert.equal(buildFilename('20260728145404', ''), '20260728145404');
});
test('extractPrefix: timeid always, date only when allowed, dash does not split', () => {
assert.equal(extractPrefix('202607302233 vai', false), '202607302233'); // timeid
assert.equal(extractPrefix('2026-07-30-Journal', true), '2026-07-30'); // date, dash kept
assert.equal(extractPrefix('2026-07-30 Journal', true), '2026-07-30');
assert.equal(extractPrefix('2026-07-30 Journal', false), null); // date off
assert.equal(extractPrefix('my-note', true), null);
});
test('computeBasename: sticky tail rule', () => {
// H1 sets the tail
assert.equal(computeBasename('202607302233', 'Home', false), '202607302233-Home');
// delete H1 keeps the last slug (sticky)
assert.equal(computeBasename('202607302233-Home', '', false), '202607302233-Home');
// no H1: clean the existing tail
assert.equal(computeBasename('202607291108 vai', '', false), '202607291108-vai');
// punctuation-only H1: keep the existing tail
assert.equal(computeBasename('2026-07-30-Home', '!!!', true), '2026-07-30-Home');
// bare stays bare
assert.equal(computeBasename('202607302233', '', false), '202607302233');
// no usable title -> left untouched
assert.equal(computeBasename('2026-07-30 !!!', '', true), '2026-07-30 !!!');
// date mode gating
assert.equal(computeBasename('2026-07-30 Journal', '', true), '2026-07-30-Journal');
assert.equal(computeBasename('2026-07-30 Journal', '', false), null);
// unrecognized note
assert.equal(computeBasename('my-note', 'Title', true), null);
});