remcohaszing / rehype-mdx-title

A rehype MDX plugin for exposing the page title
MIT License
14 stars 1 forks source link

Suport secondary or third level heading if no top level title found #6

Closed guoyunhe closed 7 months ago

guoyunhe commented 9 months ago

In my cases, users may write some document without first level heading:

## Page Title

...

Can it be supported to parse the first heading? (even if not top level?)

remcohaszing commented 9 months ago

Do you mean something like the following? This would mean the plugin would search for level 1, 2, and 4 headers, but not 3, 5, or 6. It will use the first level 1 header if one is present. If not, it will use the first level 2 header found. If that’s not present either, it will use the first level 4 header found.

await compile(content, {
  rehypePlugins: [
    [rehypeMdxTitle, { levels: [1, 2, 4] }]
  ]
})

By default it will only look for a level 1 header.

remcohaszing commented 9 months ago

Or perhaps the configuration should only define one level, which implies all levels up to the one specified. So a level of 4 would mean to look for header levels 1-4.

guoyunhe commented 9 months ago

@remcohaszing it should not be a config option, but a fallback logic.

  1. The plugin looks for first top level heading (h1 or #).
  2. If not found, the plugin looks for first heading, no matter h2/h3/h4/h5/h6.

This makes it more likely to return a title.

remcohaszing commented 9 months ago

I read your proposal as:

This code yields Level 1:

### Level 3

## Level 2

# Level 1

This code yields Level 3:

### Level 3

## Level 2

IMO this is weird. Why not yield Level 2 in the latter example?

wooorm commented 9 months ago

What if you find the first heading of the highest rank?

@guoyunhe do you have an actual example of what your users do?

guoyunhe commented 9 months ago

I read your proposal as:

This code yields Level 1:

### Level 3

## Level 2

# Level 1

This code yields Level 3:

### Level 3

## Level 2

IMO this is weird. Why not yield Level 2 in the latter example?

Yield Level 2 is also fine for me, better than return nothing.

In my case, I made an internal doc tool to generate websites from markdown. When creating the site navigation menu, here are cases when no title are parsed. Some people prefer writting level 2 or level 3 headings, simply because they think level 1 headings is too big in font size... You cannot really control it.

remcohaszing commented 7 months ago

Released in version 3.1.0 :tada: