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

[gatsby-transformer-remark] Add a way to get AST of Table of contents #25152

Closed
iray-tno opened this issue Jun 20, 2020 · 4 comments
Closed

[gatsby-transformer-remark] Add a way to get AST of Table of contents #25152

iray-tno opened this issue Jun 20, 2020 · 4 comments
Assignees
Labels
topic: remark/mdx Related to Markdown, remark & MDX ecosystem type: feature or enhancement Issue that is not a bug and requests the addition of a new feature or enhancement.

Comments

@iray-tno
Copy link

Summary

Adding a way to get AST of Table of contents just like the htmlAst property.

Basic example

{
  allMarkdownRemark {
    edges {
      node {
        html
        htmlAst
        tableOfContents
        tableOfContentsAst // <- add this
      }
    }
  }
}

Motivation

When a user using htmlAst to customize the output DOM, they may want to access the AST of the table of contents too.
In my case, I'm using the htmlAst with rehype-react. When I want to add functionality to the table of contents, like emphasizing the header which a user is reading. It's very convenient if I can get the AST of the table of contents.

@iray-tno iray-tno added the type: feature or enhancement Issue that is not a bug and requests the addition of a new feature or enhancement. label Jun 20, 2020
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Jun 20, 2020
@freiksenet freiksenet added topic: remark/mdx Related to Markdown, remark & MDX ecosystem and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Jun 22, 2020
@PetroPavlenko
Copy link

Hi @freiksenet .
I'm using htmlAst with rehype and can all headers

import rehypeReact from "rehype-react";
const config = {
  createElement: React.createElement,
  Fragment: React.Fragment,
  components: {
    p: P,
    sup: Sup,
    a: StyledLink, 
    ol: Ol,
    ul: Ul,
    li: Li,
    h1: Heading1,
    h2: Heading2,
    h3: Heading3,
    h4: Heading4,
    h5: H5,
    b: Strong,
    i: Em,
    em: Em,
  },
};
const renderAst = new rehypeReact(astConfig).Compiler;

export const AstContent = ({ content, className }: AstContentProps) => (
  <div className={className}>
    {renderAst(content)}
  </div>
);

most tags are custom. And I can add styles how I want.
Not sure that's is answer to your question. But should be helpful )

@iray-tno
Copy link
Author

iray-tno commented Jun 23, 2020

Hi @PetroPavlenko . Maybe wrong mention? XD

Thanks for your comment.

Yeah, I'm doing the same thing with that for article content.
What I meant is I wanted to do that for the table of contents also.

By filtering elements except for headers, I can actually use the htmlAst for rendering the table of contents also.
But the rendering layer (components) looks like the wrong place to filter AST elements.
I think it's more convenient if I'm able to access AST of the table of contents.

@iray-tno
Copy link
Author

This looks like the proper use-case of headings. #23546
I'll look into it a bit more tomorrow.

@iray-tno
Copy link
Author

iray-tno commented Aug 29, 2020

I'll look into it a bit more tomorrow.

"tomorrow"... haha

Sorry for the late reply.

I actually could do everything I wanted to do with the headings.
And now it looks enough simple for me. Maybe some functionalities are difficult to implement with my idea (tableOfContentsAst).
headings were better than it.

This issue can be closed.
But since it's already assigned @pieh, I let this open,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: remark/mdx Related to Markdown, remark & MDX ecosystem type: feature or enhancement Issue that is not a bug and requests the addition of a new feature or enhancement.
Projects
None yet
Development

No branches or pull requests

5 participants