From d9260e447b426e2b92dbe0b17eb0f81a9c80aa8a Mon Sep 17 00:00:00 2001 From: Stan Lo Date: Thu, 1 Jan 2026 23:00:28 +0000 Subject: [PATCH 1/2] Use GitHub-style anchors for headings and cross-references Anchors Generated: | Context | Before | After | |---------|--------|-------| | Heading `== Hello` (standalone) | `id="label-Hello"` | `id="hello"` | | Heading `== Hello` (in class Foo) | `id="class-Foo-label-Hello"` | `id="class-foo-hello"` | | Heading `== Hello` (in method #bar) | `id="method-i-bar-label-Hello"` | `id="method-i-bar-hello"` | | Class/Module anchor | `id="class-Foo::Bar"` | `id="class-foo-bar"` | Legacy anchors preserved as hidden `` elements. Links Generated: | Syntax | Before | After | |--------|--------|-------| | `rdoc-ref:@foo` | `href="#label-foo"` | `href="#foo"` | | `rdoc-ref:Class@foo` | `href="#class-Class-label-foo"` | `href="#class-class-foo"` | | `rdoc-ref:Class#method@foo` | `href="#method-i-method-label-foo"` | `href="#method-i-method-foo"` | | `rdoc-ref:Class@Section Title` | `href="#Section Title"` | `href="#section-title"` | These changes now enable Markdown links to work with GitHub-style anchors. Markdown `[link](#foo)` passes through unchanged as `href="#foo"`. - Before: Anchor was `id="label-Foo"` -> link broken - After: Anchor is `id="foo"` -> link works Supportive Changes: - ToC sidebar links updated to use new anchor format --- lib/rdoc/code_object/class_module.rb | 20 ++++- lib/rdoc/code_object/context/section.rb | 21 ++++- lib/rdoc/generator/template/aliki/class.rhtml | 2 + .../generator/template/aliki/css/rdoc.css | 20 +++++ .../generator/template/darkfish/class.rhtml | 2 + .../generator/template/darkfish/css/rdoc.css | 19 +++++ lib/rdoc/markup/heading.rb | 77 +++++++++++++++++-- lib/rdoc/markup/to_html.rb | 27 +++++-- lib/rdoc/markup/to_html_crossref.rb | 29 +++++-- lib/rdoc/markup/to_label.rb | 12 ++- lib/rdoc/parser/changelog.rb | 8 ++ lib/rdoc/text.rb | 15 ++++ test/rdoc/code_object/normal_class_test.rb | 4 +- test/rdoc/code_object/normal_module_test.rb | 4 +- test/rdoc/generator/darkfish_test.rb | 10 ++- test/rdoc/generator/markup_test.rb | 3 +- test/rdoc/markup/heading_test.rb | 33 +++++++- test/rdoc/markup/to_html_crossref_test.rb | 35 +++++---- test/rdoc/markup/to_html_test.rb | 33 +++++--- test/rdoc/markup/to_label_test.rb | 18 +++-- test/rdoc/rdoc_context_section_test.rb | 4 +- 21 files changed, 324 insertions(+), 72 deletions(-) diff --git a/lib/rdoc/code_object/class_module.rb b/lib/rdoc/code_object/class_module.rb index d7ee36f950..00e406b1a6 100644 --- a/lib/rdoc/code_object/class_module.rb +++ b/lib/rdoc/code_object/class_module.rb @@ -188,10 +188,26 @@ def aref_prefix # :nodoc: end ## - # HTML fragment reference for this module or class. See - # RDoc::NormalClass#aref and RDoc::NormalModule#aref + # HTML fragment reference for this module or class using GitHub-style + # anchor format (lowercase, :: replaced with -). + # + # Examples: + # Foo -> class-foo + # Foo::Bar -> class-foo-bar def aref + "#{aref_prefix}-#{full_name.downcase.gsub('::', '-')}" + end + + ## + # Legacy HTML fragment reference for backward compatibility. + # Returns the old RDoc-style anchor format. + # + # Examples: + # Foo -> class-Foo + # Foo::Bar -> class-Foo::Bar + + def legacy_aref "#{aref_prefix}-#{full_name}" end diff --git a/lib/rdoc/code_object/context/section.rb b/lib/rdoc/code_object/context/section.rb index 16b778174f..7600a16935 100644 --- a/lib/rdoc/code_object/context/section.rb +++ b/lib/rdoc/code_object/context/section.rb @@ -70,11 +70,30 @@ def add_comment(comment) end ## - # Anchor reference for linking to this section + # Anchor reference for linking to this section using GitHub-style format. + # + # Examples: + # "Section" -> "section" + # "One Two" -> "one-two" + # "[untitled]" -> "untitled" def aref title = @title || '[untitled]' + RDoc::Text.to_anchor(title) + end + + ## + # Legacy anchor reference for backward compatibility. + # + # Examples: + # "Section" -> "section" + # "One Two" -> "one+two" + # "[untitled]" -> "5Buntitled-5D" + + def legacy_aref + title = @title || '[untitled]' + CGI.escape(title).gsub('%', '-').sub(/^-/, '') end diff --git a/lib/rdoc/generator/template/aliki/class.rhtml b/lib/rdoc/generator/template/aliki/class.rhtml index 69adfab1ad..5a6fa35b18 100644 --- a/lib/rdoc/generator/template/aliki/class.rhtml +++ b/lib/rdoc/generator/template/aliki/class.rhtml @@ -29,6 +29,7 @@ <% end %> +

<%= klass.type %> <%= klass.full_name %>

@@ -38,6 +39,7 @@ <%- klass.each_section do |section, constants, attributes| %> + <%- klass.each_section do |section, constants, attributes| %> +