gjtorikian / commonmarker

Ruby wrapper for the comrak (CommonMark parser) Rust crate
MIT License
431 stars 83 forks source link

Anchors are automatically generated for headings #318

Open tysongach opened 1 week ago

tysongach commented 1 week ago

Hello,

I’ve just installed the gem into a Rails app and without passing in any options, Commonmarker is generating HTML headings with an anchor link inside of them:

% bin/rails c
Loading development environment (Rails 7.2.1.1)
app(dev)> Commonmarker.to_html("# Foo")
=> "<h1><a href=\"#foo\" aria-hidden=\"true\" class=\"anchor\" id=\"foo\"></a>Foo</h1>\n"

I was expecting this:

<h1>Foo</h1>

The output is surprising to me because there is no mention of these anchors in the CommonMark spec or in the GitHub Flavored Markdown spec.

If you put the example above into the playground/interactive dingus on the CommonMark website, it outputs what I was expecting:

<h1>Foo</h1>

If you disable the autolink extension, the anchor is not generated:

app(dev)> Commonmarker.to_html("# Foo", options: { extension: { autolink: false } })
=> "<h1>Foo</h1>\n"

However, autolinking is something I do want. Is there a way to disable these heading anchor from being generated, while retaining the standard autolink functionality?

Thanks in advance!

gjtorikian commented 3 days ago

Could you try options: { extension: { header_ids: nil } }?

tysongach commented 3 days ago

@gjtorikian Yep, that removed the anchors for me. Thank you! I wonder if that should be the default?

gjtorikian commented 3 days ago

Yes, I’m having a big think about defaults at the moment. Actually #315 is a more general bug around the issue you faced with autolink: false. Apparently, the defaults aren’t getting merged correctly, and no one has noticed until now!