Add header_slug_prefix render option

Why

For Heading anchors in a snippet always scroll to f... (gitlab-org/gitlab#604237 - closed). Non-Markdown markup is already covered (Fix snippet heading anchors always scrolling to... (gitlab-org/gitlab!242817 - merged)).

Since Markdown is rendered by this gem, the same approach requires gem-side support. This was suggested by the gem maintainer @kivikakk in that MR (gitlab-org/gitlab!242817 (comment 3506268420)).

What this MR does

Adds header_slug_prefix option as the entry point. A follow-up GitLab MR will pass a prefix derived from blob.path to it.

Dependency on `header_accessibility` option

header_slug_prefix only takes effect when header_accessibility is true. This is because render_heading function handles both in the same code path. In practice, GitLab always sets both options, so this is not an issue in production.

How to set up and validate locally

$ cargo run --bin gitlab-glfm-markdown --features="cli" -- \
    --header-accessibility --header-ids=user-content- --header-slug-prefix=readme- \
    <<< $'# Foo\n\n## Bar'
<h1 id="user-content-readme-foo">Foo<a href="#readme-foo" aria-label=... class="anchor"></a></h1>
<h2 id="user-content-readme-bar">Bar<a href="#readme-bar" aria-label=... class="anchor"></a></h2>

Notes

  • First time writing Rust — feedback of any kind welcome.

Merge request reports

Loading