Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
guoyunhe opened this issue Feb 5, 2024 · 7 comments
Closed

Comments

@guoyunhe
Copy link
Contributor

guoyunhe commented Feb 5, 2024

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
Copy link
Owner

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
Copy link
Owner

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
Copy link
Contributor Author

guoyunhe commented Feb 5, 2024

@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
Copy link
Owner

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
Copy link

wooorm commented Feb 5, 2024

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

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

@guoyunhe
Copy link
Contributor Author

guoyunhe commented Feb 6, 2024

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
Copy link
Owner

Released in version 3.1.0 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants