Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve heading id slug format #12972

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tiny-parrots-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/markdown-remark': patch
---

Makes generated heading ID slug values more kebab-case friendly
10 changes: 4 additions & 6 deletions packages/markdown/remark/src/rehype-collect-headings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,11 @@ export function rehypeHeadingIds(): ReturnType<RehypePlugin> {
}
});

node.properties = node.properties || {};
node.properties ??= {};
if (typeof node.properties.id !== 'string') {
let slug = slugger.slug(text);

if (slug.endsWith('-')) slug = slug.slice(0, -1);

node.properties.id = slug;
const NBSP = /[\u00A0\u2007\u202F]/g;
const EXTRA_HYPHENS = /^-+|(?<=-)-+|-+$/g;
node.properties.id = slugger.slug(text.replace(NBSP, ' ')).replace(EXTRA_HYPHENS, '');
}

headings.push({ depth, slug: node.properties.id, text });
Expand Down
72 changes: 72 additions & 0 deletions packages/markdown/remark/test/rehype-collect-headings.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import * as assert from 'node:assert/strict';
import { before, describe, it } from 'node:test';
import { createMarkdownProcessor } from '../dist/index.js';

describe('rehypeHeadingIds()', () => {
let processor;

before(async () => {
processor = await createMarkdownProcessor();
});

describe('generated ID slug', () => {
it('treats non-breaking space as space', async () => {
/**
* NO-BREAK SPACE `&nbsp;`, FIGURE SPACE `&numsp;`, NARROW NO-BREAK SPACE
* */
const NON_BREAKING_SPACES = ['&#160;', '&#8199;', '&#8239;'];

const {
metadata: {
headings: [heading],
},
} = await processor.render(markdownHeading(NON_BREAKING_SPACES));

assert.equal(heading.slug, expectedSlug(NON_BREAKING_SPACES));

/**
* Helper function
* @param {string[]} nbsp - list of non-breaking spaces
* @returns {string} markdown heading with given `nbsp` list
* */
function markdownHeading(nbsp = []) {
return `## text${nbsp.join('text')}text`;
}

/**
* Helper function
* @param {string[]} nbsp - list of non-breaking spaces
* @returns {string} expected slug value of `markdownHeading()` with given `nbsp` list
* */
function expectedSlug(nbsp = []) {
return `text-${nbsp.map(() => 'text').join('-')}`;
}
});

it('is in kebab-case', async () => {
const EXPECTED_SLUG = 'lorem-ipsum-dolor-sit-amet';
const MARKDOWN_HEADING = '## 😄 😄 Lorem 😄 ipsum 😄 😄 😄 dolor ✓ sit &nbsp; amet 😄 😄';

const {
metadata: {
headings: [heading],
},
} = await processor.render(MARKDOWN_HEADING);

assert.equal(heading.slug, EXPECTED_SLUG);
});

it('has unique value within given set of headings', async () => {
const repeatedMarkdownHeadings = `${'## Lorem ipsum dolor sit amet\n\n'.repeat(3)}`;

const {
metadata: { headings },
} = await processor.render(repeatedMarkdownHeadings);

const numberOfHeadings = headings.length;
const numberOfUniqueIdValues = new Set(headings.map(({ slug }) => slug)).size;

assert.equal(numberOfHeadings, numberOfUniqueIdValues);
});
});
});
Loading