Rollup merge of #68224 - GuillaumeGomez:prevent-urls-in-headings, r=ollie27

Prevent urls in headings

Fixes #68215.

cc @pietroalbini @ollie27

r? @kinnison
diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs
index c5f88f9..c87964a 100644
--- a/src/librustdoc/html/markdown.rs
+++ b/src/librustdoc/html/markdown.rs
@@ -380,7 +380,10 @@
                     }
                     _ => {}
                 }
-                self.buf.push_back(event);
+                match event {
+                    Event::Start(Tag::Link(_, _, _)) | Event::End(Tag::Link(..)) => {}
+                    event => self.buf.push_back(event),
+                }
             }
             let id = self.id_map.derive(id);
 
@@ -395,7 +398,7 @@
 
             let start_tags = format!(
                 "<h{level} id=\"{id}\" class=\"section-header\">\
-                                      <a href=\"#{id}\">",
+                    <a href=\"#{id}\">",
                 id = id,
                 level = level
             );
diff --git a/src/test/rustdoc/remove-url-from-headings.rs b/src/test/rustdoc/remove-url-from-headings.rs
new file mode 100644
index 0000000..9761c1d
--- /dev/null
+++ b/src/test/rustdoc/remove-url-from-headings.rs
@@ -0,0 +1,17 @@
+#![crate_name = "foo"]
+
+// @has foo/fn.foo.html
+// !@has - '//a[@href="http://a.a"]'
+// @has - '//a[@href="#implementing-stuff-somewhere"]' 'Implementing stuff somewhere'
+// @has - '//a[@href="#another-one-urg"]' 'Another one urg'
+
+/// fooo
+///
+/// # Implementing [stuff](http://a.a "title") somewhere
+///
+/// hello
+///
+/// # Another [one][two] urg
+///
+/// [two]: http://a.a
+pub fn foo() {}