gettalong / kramdown

kramdown is a fast, pure Ruby Markdown superset converter, using a strict syntax definition and supporting several common extensions.
http://kramdown.gettalong.org
Other
1.72k stars 271 forks source link

Add links to headers 🔗 #765

Closed mctaylorpants closed 1 year ago

mctaylorpants commented 2 years ago

Hello! I've implemented a feature that I hope would be a good fit for kramdown: the ability to add links to headers.

How it works

When the header_links option is set to true and auto_ids is also true, headers will receive an anchor tag - just like the ones on a Github README page

## Example header

... becomes:

<h2 id="example-header"><a href="#example-header">Example header</a></h2>

Having a link in the header is handy for generating a permalink to share when there's no table of contents on the page.

mctaylorpants commented 2 years ago

I've currently got two test failures in the suite - I can look into them further if the overall feature looks good:

TestFiles#test_/usr/src/app/test/testcases/block/04_header/with_header_links_text_to_html [/usr/src/app/test/test_files.rb:60]:
--- expected
+++ actual
@@ -16,7 +16,7 @@

 <h2 id=\"hallo-2\"><a href=\"#hallo-2\">hallO</a></h2>

-<h1>Header without ID</h1>
+<h1><a href=\"#\">Header without ID</a></h1>

 <h1 id=\"transliterated-day-la-vi-du\"><a href=\"#transliterated-day-la-vi-du\">Transliterated: Đây-là-ví-dụ</a></h1>
TestFiles#test_whether_Html_modifies_tree_with_file_/usr/src/app/test/testcases/block/04_header/with_header_links_text [/usr/src/app/test/test_files.rb:284]:
type mismatch.
Expected: :text
  Actual: :a
mctaylorpants commented 2 years ago

Thanks for the comments and suggestions, @gettalong! I've made some updates:

gettalong commented 1 year ago

@mctaylorpants Thanks! I have squashed the commits and the changes will be in the next release.