Files
outline/shared/editor/lib/headingToSlug.ts
T
Tom Moor 9884e0379f perf: Memory usage fixes (#13101)
* fix: Module level cache in headingToSlug

* fix: Remove retained full User and Team model on every ws connection

* feedback
2026-07-23 08:14:35 -04:00

26 lines
822 B
TypeScript

import { escape } from "es-toolkit/compat";
import type { Node } from "prosemirror-model";
import slugify from "slugify";
// Slugify, escape, and remove periods from headings so that they are
// compatible with both url hashes AND dom ID's (querySelector does not like
// ID's that begin with a number or a period, for example).
function safeSlugify(text: string) {
return `h-${escape(
slugify(text, {
remove: /[!"#$%&'.()*+,/:;<=>?@[\]\\^_`{|}~]/g,
lower: true,
})
)}`;
}
// calculates a unique slug for this heading based on it's text and position
// in the document that is as stable as possible
export default function headingToSlug(node: Node, index = 0) {
const slugified = safeSlugify(node.textContent);
if (index === 0) {
return slugified;
}
return `${slugified}-${index}`;
}