I'm writing Markdown content in Nuxt 3 & Nuxt Content 2.1 and I am facing a problem as I cannot write h2-h6 headers without it rendering them as links.
h1 works fine with one octothorpe symbol but as soon as I add 1 or more of them to render smaller headers, the application automatically transforms them to URLs.
Content is rendered with the default [...slug].vue and <ContentDoc /> configuration as seen in the documentation.
What's written in Markdown:
# header 1
## header 2
... and what's actually being rendered in HTML:
<h1 id="header-1">
  <!--[-->
  header 1
  <!--]-->
</h1>
<h2 id="header-2">
  <a href="#header-2">
    <!--[-->
    header 2
    <!--]-->
  </a>
</h2>
Is there any way to solve this?
EDIT:
Nuxt is also transforming simple HTML <h2> tags to links, but now with an undefined href:
<h2>header 2</h2>
to
<h2>
  <a href="#undefined">
    header 2
  </a>
</h2>
                You can also change this behaviour in your nuxt.config file
content: {
  markdown: {
    anchorLinks: false,
  }
},
https://content.nuxtjs.org/api/configuration/#anchorlinks
Checkout the Nuxt Content doc here:
In Nuxt Content, Prose represents the HTML tags output from the Markdown syntax, for example title levels, links... A Vue component corresponds to each tag, allowing you to override them if needed.
By default, h2 becomes <a> tag in <h2> tag, it is defined in this file. These files are listed in components/prose section.
You may overwrite it by:
<template>
  <h2 :id="id">
    <slot />
  </h2>
</template>
Restart server, it should changes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With