Skip to content

Commit

Permalink
Rename plugin to rehypeHeadingIds as per @bholmesdev suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
delucis committed Dec 20, 2022
1 parent a579458 commit 13d0c0b
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 15 deletions.
6 changes: 3 additions & 3 deletions .changeset/fast-baboons-prove.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ Run heading ID injection after user plugins

If you are using a rehype plugin that depends on heading IDs injected by Astro, the IDs will no longer be available when your plugin runs by default.

To inject IDs before your plugins run, import and add the `rehypeHeadingSlugs` plugin to your `rehypePlugins` config:
To inject IDs before your plugins run, import and add the `rehypeHeadingIds` plugin to your `rehypePlugins` config:

```diff
// astro.config.mjs
+ import { rehypeHeadingSlugs } from '@astrojs/markdown-remark';
+ import { rehypeHeadingIds } from '@astrojs/markdown-remark';
import mdx from '@astrojs/mdx';

export default {
integrations: [mdx()],
markdown: {
rehypePlugins: [
+ rehypeHeadingSlugs,
+ rehypeHeadingIds,
otherPluginThatReliesOnHeadingIDs,
],
},
Expand Down
4 changes: 2 additions & 2 deletions .changeset/violet-mice-push.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
'@astrojs/markdown-remark': minor
---

Refactor and export `rehypeHeadingSlugs` plugin
Refactor and export `rehypeHeadingIds` plugin

The `rehypeHeadingSlugs` plugin injects IDs for all headings in a Markdown document and can now also handle MDX inputs if needed. You can import and use this plugin if you need heading IDs to be injected _before_ other rehype plugins run.
The `rehypeHeadingIds` plugin injects IDs for all headings in a Markdown document and can now also handle MDX inputs if needed. You can import and use this plugin if you need heading IDs to be injected _before_ other rehype plugins run.
6 changes: 3 additions & 3 deletions packages/integrations/mdx/src/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { rehypeHeadingSlugs } from '@astrojs/markdown-remark';
import { rehypeHeadingIds } from '@astrojs/markdown-remark';
import { nodeTypes } from '@mdx-js/mdx';
import type { PluggableList } from '@mdx-js/mdx/lib/core.js';
import type { Options as MdxRollupPluginOptions } from '@mdx-js/rollup';
Expand Down Expand Up @@ -178,8 +178,8 @@ export function getRehypePlugins(
...rehypePlugins,
...(mdxOptions.rehypePlugins ?? []),
// getHeadings() is guaranteed by TS, so this must be included.
// We run `rehypeHeadingSlugs` _last_ to respect any custom IDs set by user plugins.
rehypeHeadingSlugs,
// We run `rehypeHeadingIds` _last_ to respect any custom IDs set by user plugins.
rehypeHeadingIds,
rehypeInjectHeadingsExport,
];
return rehypePlugins;
Expand Down
4 changes: 2 additions & 2 deletions packages/integrations/mdx/test/mdx-get-headings.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { rehypeHeadingSlugs } from '@astrojs/markdown-remark';
import { rehypeHeadingIds } from '@astrojs/markdown-remark';
import mdx from '@astrojs/mdx';
import { visit } from 'unist-util-visit';

Expand Down Expand Up @@ -123,7 +123,7 @@ describe('MDX heading IDs can be injected before user plugins', () => {
integrations: [
mdx({
rehypePlugins: [
rehypeHeadingSlugs,
rehypeHeadingIds,
() => (tree) => {
visit(tree, 'element', (node, index, parent) => {
if (!/^h\d$/.test(node.tagName)) return;
Expand Down
8 changes: 4 additions & 4 deletions packages/markdown/remark/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { MarkdownRenderingOptions, MarkdownRenderingResult, MarkdownVFile } from './types';

import { loadPlugins } from './load-plugins.js';
import { rehypeHeadingSlugs } from './rehype-collect-headings.js';
import { rehypeHeadingIds } from './rehype-collect-headings.js';
import rehypeEscape from './rehype-escape.js';
import rehypeExpressions from './rehype-expressions.js';
import rehypeIslands from './rehype-islands.js';
Expand All @@ -22,7 +22,7 @@ import markdownToHtml from 'remark-rehype';
import { unified } from 'unified';
import { VFile } from 'vfile';

export { rehypeHeadingSlugs } from './rehype-collect-headings.js';
export { rehypeHeadingIds } from './rehype-collect-headings.js';
export * from './types.js';

export const DEFAULT_REMARK_PLUGINS = ['remark-gfm', 'remark-smartypants'];
Expand Down Expand Up @@ -99,8 +99,8 @@ export async function renderMarkdown(
parser
.use(
isAstroFlavoredMd
? [rehypeJsx, rehypeExpressions, rehypeEscape, rehypeIslands, rehypeHeadingSlugs]
: [rehypeHeadingSlugs, rehypeRaw]
? [rehypeJsx, rehypeExpressions, rehypeEscape, rehypeIslands, rehypeHeadingIds]
: [rehypeHeadingIds, rehypeRaw]
)
.use(rehypeStringify, { allowDangerousHtml: true });

Expand Down
2 changes: 1 addition & 1 deletion packages/markdown/remark/src/rehype-collect-headings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { MarkdownHeading, MarkdownVFile, RehypePlugin } from './types.js';
const rawNodeTypes = new Set(['text', 'raw', 'mdxTextExpression']);
const codeTagNames = new Set(['code', 'pre']);

export function rehypeHeadingSlugs(): ReturnType<RehypePlugin> {
export function rehypeHeadingIds(): ReturnType<RehypePlugin> {
return function (tree, file: MarkdownVFile) {
const headings: MarkdownHeading[] = [];
const slugger = new Slugger();
Expand Down

0 comments on commit 13d0c0b

Please sign in to comment.