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

"Shift" option doesn't accept zero #2

Closed
4 tasks done
noltron000 opened this issue Feb 8, 2023 · 6 comments
Closed
4 tasks done

"Shift" option doesn't accept zero #2

noltron000 opened this issue Feb 8, 2023 · 6 comments
Labels
💪 phase/solved Post is done

Comments

@noltron000
Copy link

Initial checklist

Affected packages and versions

v1.02

Link to runnable example

No response

Steps to reproduce

Use {shift: 0} as an option in the this module.
IE,

const vFile = await rehype( )
  .use(rehypeParse, {fragment: true})
  .use(rehypeShiftHeading, {shift: 0})
  .use(rehypeSanitize, rehypeSanitizerSchema)
  .use(rehypeReact, rehypeReactOptions)
  .process(html)

Expected behavior

It shouldn't throw an error, even though technically the function shouldn't do anything to the tree either, with a shift value of zero.

Actual behavior

There is an error thrown: Error: Missing required `shift` in options.

You can see why this behavior is happening:

if (!options || !options.shift) {

Zero is falsey, so it isn't an accepted value.

Runtime

No response

Package manager

No response

OS

No response

Build and bundle tools

No response

@github-actions github-actions bot added 👋 phase/new Post is being triaged automatically 🤞 phase/open Post is being triaged manually and removed 👋 phase/new Post is being triaged automatically labels Feb 8, 2023
@noltron000
Copy link
Author

noltron000 commented Feb 8, 2023

As to why we would want this, there are times our HTML headings might be on the right level already, and other times where it is not. We could pass in a variable shift value instead of setting it at zero as I did in the ticket above. (IE (html, shift) => rehype( ).use(rehypeShiftHeading, {shift}) or something)

@wooorm
Copy link
Member

wooorm commented Feb 8, 2023

Yeah that was my next question lol. Open to a PR for this, I understand the use case :)

@noltron000
Copy link
Author

Yeah! No problem and thanks for the fast response. Its not a big deal and can be worked around by setting the options object to false to turn the plugin off, instead of {shift: 0} or whatever, for example

const processHTML = async (
  html: string,
  options?: Options,
) => {

  // ...

  // Bypass zeroes not being accepted in this method.
  let shiftHeadingOptions
  if (options?.shift == null || options.shift === 0) shiftHeadingOptions = false
  else shiftHeadingOptions = {shift: options.shift}

  // ...

  const vFile = await rehype( )
    .use(rehypeShiftHeading, shiftHeadingOptions)
    .use( // ...etc
}

@wooorm
Copy link
Member

wooorm commented Feb 8, 2023

For the PR: you should add a test, and then: don't walk the tree on 0, that would be useless!

@wooorm wooorm closed this as completed in bb2fef5 Aug 26, 2023
@github-actions

This comment has been minimized.

@wooorm wooorm added the 💪 phase/solved Post is done label Aug 26, 2023
@wooorm
Copy link
Member

wooorm commented Aug 26, 2023

done!

@github-actions github-actions bot removed the 🤞 phase/open Post is being triaged manually label Aug 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💪 phase/solved Post is done
Development

No branches or pull requests

2 participants