Files
dashboard/utils/markdownUtils.ts
T
Alberto-Audrix 32a36cceff
CI / lint-and-test (push) Canceled after 0s
first commit
2026-07-28 08:55:05 +07:00

26 lines
781 B
TypeScript

// Custom Slugger class to replace the one from 'marked' due to import issues.
// This mimics the behavior of creating unique slugs for duplicate headings.
export class CustomSlugger {
private seen: { [key: string]: number } = {};
slug(value: any) {
// Accept any type to be robust
// Coerce value to a string to prevent errors if it's not a string type, like in some edge cases with marked's parser.
const stringValue = String(value || '');
let slug = stringValue
.toLowerCase()
.trim()
.replace(/[\s_]+/g, '-')
.replace(/[^\w-]+/g, '')
.replace(/--+/g, '-');
if (this.seen[slug]) {
this.seen[slug]++;
slug = `${slug}-${this.seen[slug] - 1}`;
} else {
this.seen[slug] = 1;
}
return slug;
}
}