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

Anchor the element in the same page not working. #47

Open
aruiplex opened this issue Jan 24, 2023 · 2 comments
Open

Anchor the element in the same page not working. #47

aruiplex opened this issue Jan 24, 2023 · 2 comments

Comments

@aruiplex
Copy link

Hi there.

I would like to use the table of content to anchor the elements in the mdx, which in the same page, but it doesn't work.

I use the gatsby file system to generate pages by the mdx files.

This is the Layout of the mdx template to add the AnchorLink to the h1 tags.

import React from "react";
import { MDXProvider } from "@mdx-js/react";
import { AnchorLink } from "gatsby-plugin-anchor-links";

const MyH1 = (props) => {
  return (
    <h1>
      <AnchorLink to={"#" + props.children}>{props.children}</AnchorLink>
      {/* <a href={"#" + props.children}>{props.children}</a> */}
    </h1>
  );
};

const components = {
  h1: MyH1,
};

export default function Layout({ children }) {
  return <MDXProvider components={components}>{children}</MDXProvider>;
}

And this is a part of my code to generate table of content :

 export const MyToc = ({ url, title, items, level = 0 }) => {
  const hasChildren = items && items.length;

  return (
    <div>
      <AnchorLink to={url} style={{ paddingLeft: level * 20 + "px" }}>
        {title}
      </AnchorLink>

      {hasChildren &&
        items.map((item) => (
          <MyToc key={item.title + item.url} {...item} level={level + 1} />
        ))}
    </div>
  );
};

However, it cannot jump to the element which is in the same page. If I click the anchor element, the URL in the browser will like this: http://localhost:8000/contents/alexnet/#Alexnet. It is the page URL: http://localhost:8000/contents/alexnet/. The hash tag cannot attach behind the page without /.

Thank you for your loveable work.

@AndreaCorda01
Copy link

I have the same problem. Seems a incompatibily with the routing system provided by React and Gatsby. But I haven't achieved to get more information regarding the issue.

@AndreaCorda01
Copy link

I have just solved the problem and it was that the react component was not receiving correct the id="mission" tag and once I have updated the component, it worked 🥳

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

2 participants