@@ -17,6 +17,7 @@ import icon from "astro-icon";
1717import sitemap from "@astrojs/sitemap" ;
1818import react from "@astrojs/react" ;
1919import rehypeTitleFigure from "rehype-title-figure" ;
20+ import { visit } from "unist-util-visit" ;
2021
2122const runLinkCheck = process . env . RUN_LINK_CHECK || false ;
2223
@@ -70,6 +71,30 @@ const autolinkConfig: rehypeAutolinkHeadingsOptions = {
7071 content : ( ) => [ AnchorLinkIcon ] ,
7172} ;
7273
74+ // # foo {/*bar*/} = <a id="bar">foo</a>
75+ function rehypeCustomHeadingId ( ) {
76+ return function ( tree : any ) {
77+ visit ( tree , "element" , function ( element : any ) {
78+ if ( / ^ h [ 1 - 6 ] $ / . test ( element . tagName ) ) {
79+ const last = element . children . at ( - 1 ) ;
80+
81+ if (
82+ last . type === "mdxTextExpression" &&
83+ last . value . startsWith ( "/*" ) &&
84+ last . value . endsWith ( "*/" )
85+ ) {
86+ const id = last . value . slice ( 2 , - 2 ) . trim ( ) ;
87+ element . properties . id = id ;
88+
89+ const text = element . children . at ( - 2 ) ;
90+ text . value = text . value . trimEnd ( ) ;
91+ element . children . with ( - 2 , text ) ;
92+ }
93+ }
94+ } ) ;
95+ } ;
96+ }
97+
7398// https://astro.build/config
7499export default defineConfig ( {
75100 site : "https://developers.cloudflare.com" ,
@@ -95,6 +120,7 @@ export default defineConfig({
95120 rel : [ "noopener" ] ,
96121 } ,
97122 ] ,
123+ rehypeCustomHeadingId ,
98124 rehypeSlug ,
99125 [ rehypeAutolinkHeadings , autolinkConfig ] ,
100126 // @ts -expect-error TODO: fix types
0 commit comments