1chooo / 1chooo.com

👨🏻‍💻 $ ls -al Hugo -- A monorepo of my personal website and blog built using React and Next.js Turbo, responsive for all devices
https://1chooo.com
Creative Commons Attribution 4.0 International
12 stars 2 forks source link

[Bug] markdown-alert-title is not contained #442

Open 1chooo opened 1 day ago

1chooo commented 1 day ago

https://github.com/chrisweb/rehype-github-alerts/issues/8

related: #440 , #441

1chooo commented 1 hour ago
p: (props) => {
    const { node, children, ...rest } = props;
    const hasImage = node && node.children && node.children.some(isImageNode);
    if (hasImage) {
        return <>{children}</>;
    }
    return <p {...rest}>{children}</p>;
},
p: (props) => {
  const { node, children } = props;
  const hasImage = node && node.children && node.children.some(isImageNode);
  if (hasImage) {
    return <>{children}</>;
  }
  return <p>{children}</p>;
},

what is the different between this two logic?


The two code snippets have minor differences in handling props and rendering logic. Let’s analyze them in detail:

First Snippet

p: (props) => {
    const { node, children, ...rest } = props; // Destructures `node`, `children`, and `rest`
    const hasImage = node && node.children && node.children.some(isImageNode);
    if (hasImage) {
        return <>{children}</>; // Renders only children if there's an image
    }
    return <p {...rest}>{children}</p>; // Spreads remaining props (`rest`) onto <p>
}

Key Details:

  1. Destructuring with ...rest:

    • Any extra props passed to this component (besides node and children) are captured in rest.
    • These props are then spread into the <p> tag.
  2. Flexibility:

    • Allows additional attributes, like className, style, etc., to be applied to the <p> tag.
  3. Use Case:

    • Suitable when the <p> tag needs to support extra props.

Second Snippet

p: (props) => {
    const { node, children } = props; // Only destructures `node` and `children`
    const hasImage = node && node.children && node.children.some(isImageNode);
    if (hasImage) {
        return <>{children}</>; // Renders only children if there's an image
    }
    return <p>{children}</p>; // Doesn't pass any additional props to <p>
}

Key Details:

  1. No ...rest:

    • Ignores any extra props passed to the component.
    • The <p> tag will only have children as its content, without any additional attributes.
  2. Simpler Logic:

    • Less flexibility since extra props like className or style are ignored.
  3. Use Case:

    • Ideal when no additional props are expected or required.

Key Differences

  1. Handling Extra Props:

    • First snippet: Supports additional props (rest).
    • Second snippet: Ignores any extra props.
  2. Flexibility vs. Simplicity:

    • First snippet: More flexible but slightly more complex.
    • Second snippet: Simpler but less capable.

Recommendation