|
| 1 | +import { describe, it, expect } from 'vitest'; |
| 2 | +import type { Root, Element, Text, Properties } from 'hast'; |
| 3 | +import { rehypeSlug } from './rehype-slug.js'; |
| 4 | + |
| 5 | +function el(tag: string, text: string, properties: Properties = {}): Element { |
| 6 | + const child: Text = { type: 'text', value: text }; |
| 7 | + return { type: 'element', tagName: tag, properties, children: [child] }; |
| 8 | +} |
| 9 | + |
| 10 | +function tree(...children: Element[]): Root { |
| 11 | + return { type: 'root', children }; |
| 12 | +} |
| 13 | + |
| 14 | +function getEl(root: Root, index: number): Element { |
| 15 | + const child = root.children[index]; |
| 16 | + if (child.type !== 'element') { |
| 17 | + throw new Error(`expected element at index ${index}, got ${child.type}`); |
| 18 | + } |
| 19 | + return child; |
| 20 | +} |
| 21 | + |
| 22 | +describe('rehypeSlug', () => { |
| 23 | + const transform = rehypeSlug(); |
| 24 | + |
| 25 | + it('should add id attributes to headings', () => { |
| 26 | + const root = tree(el('h2', 'Getting Started'), el('h3', 'Prerequisites')); |
| 27 | + transform(root); |
| 28 | + |
| 29 | + expect(getEl(root, 0).properties).toMatchObject({ id: 'getting-started' }); |
| 30 | + expect(getEl(root, 1).properties).toMatchObject({ id: 'prerequisites' }); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should lowercase and strip punctuation when slugifying', () => { |
| 34 | + const root = tree(el('h2', "What's New?")); |
| 35 | + transform(root); |
| 36 | + |
| 37 | + expect(getEl(root, 0).properties).toMatchObject({ id: 'whats-new' }); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should suffix duplicate headings with -1, -2, ...', () => { |
| 41 | + const root = tree(el('h2', 'Setup'), el('h2', 'Setup'), el('h2', 'Setup')); |
| 42 | + transform(root); |
| 43 | + |
| 44 | + expect(getEl(root, 0).properties).toMatchObject({ id: 'setup' }); |
| 45 | + expect(getEl(root, 1).properties).toMatchObject({ id: 'setup-1' }); |
| 46 | + expect(getEl(root, 2).properties).toMatchObject({ id: 'setup-2' }); |
| 47 | + }); |
| 48 | + |
| 49 | + it('should leave pre-existing ids untouched', () => { |
| 50 | + const root = tree(el('h2', 'Setup', { id: 'custom-anchor' })); |
| 51 | + transform(root); |
| 52 | + |
| 53 | + expect(getEl(root, 0).properties).toMatchObject({ id: 'custom-anchor' }); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should not count pre-existing ids toward dedupe', () => { |
| 57 | + const root = tree(el('h2', 'Setup', { id: 'setup' }), el('h2', 'Setup')); |
| 58 | + transform(root); |
| 59 | + |
| 60 | + // the second heading still gets the unsuffixed slug because the visitor |
| 61 | + // only tracks ids it has assigned itself |
| 62 | + expect(getEl(root, 1).properties).toMatchObject({ id: 'setup' }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should skip headings that slugify to empty (emoji or punctuation only)', () => { |
| 66 | + const root = tree(el('h2', '🎉'), el('h2', '!!!')); |
| 67 | + transform(root); |
| 68 | + |
| 69 | + expect(getEl(root, 0).properties).not.toHaveProperty('id'); |
| 70 | + expect(getEl(root, 1).properties).not.toHaveProperty('id'); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should slugify across all h1-h6 levels', () => { |
| 74 | + const root = tree(el('h1', 'Title'), el('h4', 'Deep'), el('h6', 'Deeper')); |
| 75 | + transform(root); |
| 76 | + |
| 77 | + expect(getEl(root, 0).properties).toMatchObject({ id: 'title' }); |
| 78 | + expect(getEl(root, 1).properties).toMatchObject({ id: 'deep' }); |
| 79 | + expect(getEl(root, 2).properties).toMatchObject({ id: 'deeper' }); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should ignore non-heading elements', () => { |
| 83 | + const root = tree(el('p', 'A paragraph'), el('div', 'A div')); |
| 84 | + transform(root); |
| 85 | + |
| 86 | + expect(getEl(root, 0).properties).not.toHaveProperty('id'); |
| 87 | + expect(getEl(root, 1).properties).not.toHaveProperty('id'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('should concatenate text from nested children', () => { |
| 91 | + const inner: Element = { |
| 92 | + type: 'element', |
| 93 | + tagName: 'code', |
| 94 | + properties: {}, |
| 95 | + children: [{ type: 'text', value: 'parseMarkdown' } as Text], |
| 96 | + }; |
| 97 | + const heading: Element = { |
| 98 | + type: 'element', |
| 99 | + tagName: 'h2', |
| 100 | + properties: {}, |
| 101 | + children: [{ type: 'text', value: 'The ' } as Text, inner, { type: 'text', value: ' function' } as Text], |
| 102 | + }; |
| 103 | + const root = tree(heading); |
| 104 | + transform(root); |
| 105 | + |
| 106 | + expect(getEl(root, 0).properties).toMatchObject({ id: 'the-parsemarkdown-function' }); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should handle an empty tree', () => { |
| 110 | + const root = tree(); |
| 111 | + transform(root); |
| 112 | + |
| 113 | + expect(root.children).toHaveLength(0); |
| 114 | + }); |
| 115 | +}); |
0 commit comments