@@ -117,17 +117,44 @@ pub struct Extension<'c> {
117117 #[ cfg_attr( feature = "bon" , builder( default ) ) ]
118118 pub superscript : bool ,
119119
120- /// Enables the header IDs Comrak extension.
120+ /// Enables the header IDs Comrak extension, with the given ID prefix.
121+ ///
122+ /// When set, each heading gains an anchor element with an `id` attribute
123+ /// formed by prefixing the slugified heading text. This is useful for
124+ /// namespacing heading anchors to avoid collisions when rendered Markdown
125+ /// is embedded alongside other content on a page (e.g. GitHub uses the
126+ /// prefix `"user-content-"` for this purpose).
121127 ///
122128 /// ```rust
123129 /// # use comrak::{markdown_to_html, Options};
124130 /// let mut options = Options::default();
125- /// options.extension.header_ids = Some("user-content-".to_string());
131+ /// options.extension.header_id_prefix = Some("user-content-".to_string());
126132 /// assert_eq!(markdown_to_html("# README\n", &options),
127133 /// "<h1><a href=\"#readme\" aria-hidden=\"true\" class=\"anchor\" id=\"user-content-readme\"></a>README</h1>\n");
128134 /// ```
135+ pub header_id_prefix : Option < String > ,
136+
137+ #[ deprecated( since = "0.52.0" , note = "renamed to `header_id_prefix`" ) ]
138+ /// Deprecated: use [`header_id_prefix`](#structfield.header_id_prefix) instead.
139+ #[ cfg_attr( feature = "bon" , builder( skip) ) ]
129140 pub header_ids : Option < String > ,
130141
142+ /// When enabled alongside [`header_id_prefix`](#structfield.header_id_prefix), the header ID
143+ /// prefix is also applied to the `href` anchor in the generated link.
144+ ///
145+ /// Has no effect if `header_id_prefix` is `None`.
146+ ///
147+ /// ```rust
148+ /// # use comrak::{markdown_to_html, Options};
149+ /// let mut options = Options::default();
150+ /// options.extension.header_id_prefix = Some("user-content-".to_string());
151+ /// options.extension.header_id_prefix_in_href = true;
152+ /// assert_eq!(markdown_to_html("# README\n", &options),
153+ /// "<h1><a href=\"#user-content-readme\" aria-hidden=\"true\" class=\"anchor\" id=\"user-content-readme\"></a>README</h1>\n");
154+ /// ```
155+ #[ cfg_attr( feature = "bon" , builder( default ) ) ]
156+ pub header_id_prefix_in_href : bool ,
157+
131158 /// Enables the footnotes extension per `cmark-gfm`.
132159 ///
133160 /// For usage, see `src/tests.rs`. The extension is modelled after
@@ -578,6 +605,15 @@ pub struct Extension<'c> {
578605}
579606
580607impl Extension < ' _ > {
608+ /// Returns the effective header ID prefix, preferring [`header_id_prefix`] over the
609+ /// deprecated [`header_ids`].
610+ ///
611+ /// TODO: Remove this method when `header_ids` is removed.
612+ #[ allow( deprecated) ]
613+ pub ( crate ) fn effective_header_id_prefix ( & self ) -> Option < & String > {
614+ self . header_id_prefix . as_ref ( ) . or ( self . header_ids . as_ref ( ) )
615+ }
616+
581617 pub ( crate ) fn wikilinks ( & self ) -> Option < WikiLinksMode > {
582618 match (
583619 self . wikilinks_title_before_pipe ,
0 commit comments