Replies: 1 comment
-
Hey @neet! Sorry for the late reply. I really enjoyed your article! It does a great job explaining the heading and componentization problem. The embedded HTML is an interesting case. To get more benefits from the library, one could use a solution similar to this: import { H, Section } from 'react-headings';
import { MDXProvider } from '@mdx-js/react';
import Article from "./article.mdx";
const RelativeH = ({ level, ...props }) => {
if (level === 1) {
return <H {...props} />;
}
return (
<Section component={<></>}>
<RelativeH level={level - 1} {...props} />
</Section>
);
};
export const Cms = () => {
return (
<div>
<H>My blog</H>
<Section component={<H>My article</H>}>
<MDXProvider
components={{
h1: (props) => <RelativeH level={1} {...props} />,
h2: (props) => <RelativeH level={2} {...props} />,
h3: (props) => <RelativeH level={3} {...props} />,
h4: (props) => <RelativeH level={4} {...props} />,
h5: (props) => <RelativeH level={5} {...props} />,
h6: (props) => <RelativeH level={6} {...props} />,
}}
>
<Article />
</MDXProvider>
</Section>
</div>
);
}; And like you said, introducing a Cheers! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Thank you for maintaining this awesome library! This is a kind of essential tool that I can't make websites without this in the world that lost the Document Outline Algorithm 😭
I recently wrote an article in Japanese that introduces how to mark up headings properly in React and introduced react-headings.
https://zenn.dev/neet/articles/f25abb616ec105
I also tried to mention how to use react-headings with CMS or an embedded HTML / Markdown in that article, but I couldn't find a pretty way to achieve it.
This is basically what I've come up with (Using mdx-js):
But it directly accesses
useLevel
, does incrementation, callsMath.min
, and concatenates with"h"
which feels like I'm not getting the benefits of using a library.My simple idea to avoid this is adding a new prop
relativeLevel=X
toH
which makes the final heading level be(the current section's depth + X)
so you can also useH
in a flat structure.However, I then realised that this would also lead to users skipping the heading level. So I kept this idea to a Discussion post.
Do you have any idea?
Beta Was this translation helpful? Give feedback.
All reactions