docusaurus icon indicating copy to clipboard operation
docusaurus copied to clipboard

Theme Classic inconsistently wraps the doc page title into <header>

Open cmfcmf opened this issue 2 years ago • 9 comments
trafficstars

Have you read the Contributing Guidelines on issues?

Prerequisites

  • [X] I'm using the latest version of Docusaurus.
  • [ ] I have tried the npm run clear or yarn clear command.
  • [ ] I have tried rm -rf node_modules yarn.lock package-lock.json and re-installing packages.
  • [ ] I have tried creating a repro with https://new.docusaurus.io.
  • [ ] I have read the console error message carefully (if applicable).

Description

The theme inconsistently wraps the title of a doc page into <header>: Only when a synthetic title is rendered, it is being wrapped into <header>. Non-synthetic titles are not wrapped into <header>.

Relevant code: https://github.com/facebook/docusaurus/blob/024474a7b9924f206fa22a91b0c39832354f8e0d/packages/docusaurus-theme-classic/src/theme/DocItem/Content/index.tsx#L41-L43

Reproducible demo

https://codesandbox.io/s/dazzling-panini-566sqf

Steps to reproduce

Go to "Tutorial Intro" page: Title not wrapped in <header> Go to "Tutorial - Basics" -> "Create a Page": Title is wrapped in <header>

Expected behavior

The doc title should either always, or never, be wrapped in <header>.

Actual behavior

The doc page title is wrapped in <header> depending on whether it is a synthetic title or not.

Your environment

  • Docusaurus version used: 2.2.0

Self-service

  • [ ] I'd be willing to fix this bug myself.

cmfcmf avatar Dec 22 '22 17:12 cmfcmf

Thanks

I also noticed that. Historically the synthetic/frontMatter title was wrapped in a header.

When introducing the # Title title as it's part of MDX it's not really possible to "extract it" apart (although it should be possible in MDX 2).

I'm not an expert in semantic HTML and accessibility, does this violate any existing rule, or is it just more convenient for you to crawl the doc and create a search index if things are consistent?

slorber avatar Dec 22 '22 18:12 slorber

I'm not an expert in semantic HTML and accessibility, does this violate any existing rule, or is it just more convenient for you to crawl the doc and create a search index if things are consistent?

It was a bit surprising to me that the <header> is sometimes there and sometimes not. The fix for parsing it wasn't difficult or anything special. I guess this issue is more of a feature request to make it consistent, which could be as simple as never rendering a <header>, but I am unsure about the implications.

cmfcmf avatar Dec 22 '22 19:12 cmfcmf

I am not sure either 😅

So I'd prefer if someone can back up with semantic HTML links what we should do exactly before we change anything

slorber avatar Dec 23 '22 15:12 slorber

Is this related to an issue I'm seeing. In certain pages, the file name is appearing (sort of) above the H1 header. For example, for a file called: 1-quick-start.md with the following at the top of the file:

---
sidebar_position: 1
---

import DocsCard from '/src/components/global/DocsCard';
import DocsCards from '/src/components/global/DocsCards';
import TOCInline from '@theme/TOCInline';


# Quick Start

<div class="indent-1">
  <TOCInline toc={toc} minHeadingLevel={2} maxHeadingLevel = {2} />
</div>
<br/>

In the output file, I can see the H1 Header for "Quick Start" as expected, but there is an additional H1 header with the text "quick-start". I.e.

::before
<header>
  <h1>quick-start</h1>
</header>
<header>
  <h1>Quick Start</h1>
</header>

The odd thing is that I have 2 almost identical files. In one case the undesirable first header tag shows, but in the other it does not... Any idea what the underlying issue is?

monkeytronics avatar May 24 '23 06:05 monkeytronics

@monkeytronics Your issue is because Docusaurus did not pick up the H1 heading in the content, so we (1) could not remove it from the content (2) inferred another title based on the file name. The reason why parsing failed for your case is unknown; I can try to reproduce this later.

Josh-Cena avatar May 24 '23 08:05 Josh-Cena

I moved the lines of code around and it now works?!

---
sidebar_position: 1
---

# Quick Start

import DocsCard from '/src/components/global/DocsCard';
import DocsCards from '/src/components/global/DocsCards';
import TOCInline from '@theme/TOCInline';


<div class="indent-1">
  <TOCInline toc={toc} minHeadingLevel={2} maxHeadingLevel = {2} />
</div>
<br/>

monkeytronics avatar May 24 '23 21:05 monkeytronics

@monkeytronics I could not reproduce your issue. Could you submit a new issue with a reproduction? FYI, your second one is more likely to work because Docusaurus simply extracts the first H1 heading before any content, and the less text there is before the H1, the less likely it is to think that there's no title.

Josh-Cena avatar May 26 '23 03:05 Josh-Cena

In my perspective, it would be beneficial for us to consistently include the <header> tag. It helps individuals, who rely on accessibility features, identify the introductory content. Moreover, it aligns with the usage outlined in MDN.

sierikov avatar May 16 '24 08:05 sierikov

We already have a contentTitle remark plugin where we could do this wrapping, we'll look into it.

We could probably wrap the MDX heading in a <heading> tag thanks to that remark plugin. Pseudo code to test:

CleanShot 2024-05-16 at 12 02 47

slorber avatar May 16 '24 08:05 slorber