Stable heading ids

This commit is contained in:
Tom Moor
2018-01-27 00:47:48 -08:00
parent 2b861ea3d5
commit 7647d23804
3 changed files with 21 additions and 7 deletions

View File

@@ -3,7 +3,8 @@ import { escape } from 'lodash';
import { Node } from 'slate';
import slug from 'slug';
export default function headingToSlug(node: Node) {
const level = node.type.replace('heading', 'h');
return escape(`${level}-${slug(node.text)}-${node.key}`);
export default function headingToSlug(node: Node, index: number = 0) {
const slugified = escape(slug(node.text));
if (index === 0) return slugified;
return `${index}-${slugified}`;
}