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

How to use NODE_HEADING? #36

Open
bnbon opened this issue Apr 11, 2023 · 7 comments
Open

How to use NODE_HEADING? #36

bnbon opened this issue Apr 11, 2023 · 7 comments

Comments

@bnbon
Copy link

bnbon commented Apr 11, 2023

[NODE_HEADING]: (children, { attrs: { "level": 2, }}) => <H2>{children}</H2>,

I am trying this, but it doesn't seem to work, and I found the example a little terse... could you clarify for me (the H2 is a new custom component).

Also, how to remove the <p> tag which is inside a <li>, as <li><p></p></li> adds an extra space and looks rather awful when rendered!

@claus
Copy link
Owner

claus commented Apr 12, 2023

To return a custom component for level 2 headings you need this:

[NODE_HEADING]: (children, { level }) => {
  if (level === 2) {
    return <H2>{children}</H2>;
  } else {
    const Heading = `h${level}`;
    return <Heading>{children}</Heading>;
  }
}

This library renders the data it gets from Storyblok as is, it doesn't have any built in functionality to remove elements. That said, it also doesn't add any spaces where there are none. What you probably want to do is style those <p> inside those <li> as needed (like setting margin: 0, or display: inline)

@claus
Copy link
Owner

claus commented Apr 12, 2023

Regarding your second question, you could also try this:

[NODE_LI]: (children) => {
  if (Children.count(children) === 1) {
    const child = Children.toArray(children)[0];
    if (child.type === "p") {
      return <li>{child.props.children}</li>;
    }
  }
  return <li>{children}</li>;
},

(code above is untested)

@bnbon
Copy link
Author

bnbon commented Apr 12, 2023

To return a custom component for level 2 headings you need this:

[NODE_HEADING]: (children, { level }) => {
  if (level === 2) {
    return <H2>{children}</H2>;
  } else {
    const Heading = `h${level}`;
    return <Heading>{children}</Heading>;
  }
}

Hello, I tried this, but there was no transformation occurring... the transform for the paragraph and code nodes is working... am I doing something absurd?

        nodeResolvers: {
          [NODE_HEADING]: (children, { level }) => {
            if (level === 2) {
              return <H2>{children}</H2>;
            } else if (level === 3) {
              return <H3>{children}</H3>;
            } else if (level === 4) {
              return <H4>{children}</H4>;
            } else if (level === 5) {
              return <H5>{children}</H5>;
            } else if (level === 6) {
              return <H6>{children}</H6>;
            } else {
              const Heading = `h${level}`;
              return <Heading>{children}</Heading>;
            }
          },
          [NODE_PARAGRAPH]: (children) => <P>{children}</P>,
          [NODE_CODEBLOCK]: (children) => <CodeBlock>{children}</CodeBlock>,
        },

@claus
Copy link
Owner

claus commented Apr 13, 2023

Hmm, that code looks good to me. Is that heading resolver executed at all?

@bnbon
Copy link
Author

bnbon commented Apr 15, 2023

The headings aren't changed, the other elements are...

@bnbon
Copy link
Author

bnbon commented Apr 23, 2023

Any ideas @claus I wasn't able to spot the issue.

@claus
Copy link
Owner

claus commented Apr 24, 2023

As i said, your code looks good, and it would be helpful if you could find out if that NODE_HEADING resolver is actually executed, for example by placing some console.log's in there and watching the browser console.

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