Skip to content

Conversation

@kevinansfield
Copy link
Member

ref https://linear.app/ghost/issue/PLG-332

  • extracted visibility rendering logic and adapted to be more generalised for use in other cards

@coderabbitai
Copy link

coderabbitai bot commented Feb 3, 2025

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

packages/kg-default-nodes/lib/nodes/html/html-renderer.js

Oops! Something went wrong! :(

ESLint: 8.57.0

Error: Failed to load parser '@babel/eslint-parser' declared in 'packages/kg-default-nodes/.eslintrc.js': Cannot find module '@babel/eslint-parser'
Require stack:

  • /packages/kg-default-nodes/.eslintrc.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1248:15)
    at Function.resolve (node:internal/modules/helpers:145:19)
    at Object.resolve (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2346:46)
    at ConfigArrayFactory._loadParser (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3325:39)
    at ConfigArrayFactory._normalizeObjectConfigDataBody (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3099:43)
    at _normalizeObjectConfigDataBody.next ()
    at ConfigArrayFactory._normalizeObjectConfigData (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3040:20)
    at _normalizeObjectConfigData.next ()
    at ConfigArrayFactory.loadInDirectory (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2886:28)
    at CascadingConfigArrayFactory._loadConfigInAncestors (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3871:46)
packages/kg-default-nodes/lib/utils/visibility.js

Oops! Something went wrong! :(

ESLint: 8.57.0

Error: Failed to load parser '@babel/eslint-parser' declared in 'packages/kg-default-nodes/.eslintrc.js': Cannot find module '@babel/eslint-parser'
Require stack:

  • /packages/kg-default-nodes/.eslintrc.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1248:15)
    at Function.resolve (node:internal/modules/helpers:145:19)
    at Object.resolve (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2346:46)
    at ConfigArrayFactory._loadParser (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3325:39)
    at ConfigArrayFactory._normalizeObjectConfigDataBody (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3099:43)
    at _normalizeObjectConfigDataBody.next ()
    at ConfigArrayFactory._normalizeObjectConfigData (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3040:20)
    at _normalizeObjectConfigData.next ()
    at ConfigArrayFactory.loadInDirectory (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2886:28)
    at CascadingConfigArrayFactory._loadConfigInAncestors (/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3871:46)

Walkthrough

This pull request refactors visibility rendering in HTML node processing. The renderHtmlNode function now delegates visibility handling to a new renderWithVisibility function, reducing complexity. The new function, along with its helper routines, manages visibility for both email and web contexts, including migration for outdated formats. Additionally, the test suite has been updated to include scenarios for the new rendering logic.

Changes

Files Change Summary
packages/kg-default-nodes/lib/nodes/.../html-renderer.js
packages/kg-default-nodes/lib/utils/visibility.js
Refactored renderHtmlNode to use renderWithVisibility. Added new logic and helper functions for handling email and web visibility, and deprecated the older visibility checks.
packages/kg-default-nodes/test/.../visibility.test.js Enhanced tests to cover the new renderWithVisibility and buildDefaultVisibility functions, ensuring correct behavior across various visibility scenarios.

Sequence Diagram(s)

sequenceDiagram
    participant H as renderHtmlNode
    participant V as renderWithVisibility
    participant E as EmailVisibilityHelper
    participant W as WebVisibilityHelper

    H->>V: Call renderWithVisibility(renderOutput, node.visibility, options)
    alt Visibility condition active
        V->>E: Process email-specific visibility
        V->>W: Process web-specific visibility
    else No visibility condition
        V-->>H: Return original renderOutput
    end
    H-->>Caller: Return final HTML element
Loading

Possibly related PRs

Suggested reviewers

  • ronaldlangeveld

Poem

In my warren of code I hop with delight,
Simplifying logic and making it bright.
Visibility now dances with a nifty new flair,
Delegating tasks with precision and care.
Hop along paths where simplicity reigns true,
Cheers from this rabbit to developers like you! 🐇

Tip

🌐 Web search-backed reviews and chat
  • We have enabled web search-based reviews and chat for all users. This feature allows CodeRabbit to access the latest documentation and information on the web.
  • You can disable this feature by setting web_search: false in the knowledge_base settings.
  • Please share any feedback in the Discord discussion.
✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@ronaldlangeveld ronaldlangeveld self-requested a review February 4, 2025 04:34
ref https://linear.app/ghost/issue/PLG-332

- extracted visibility rendering logic and adapted to be more generalised for use in other cards
@kevinansfield kevinansfield force-pushed the extract-visibility-rendering branch from 7232267 to 8cbc28f Compare February 4, 2025 21:03
@kevinansfield kevinansfield marked this pull request as ready for review February 4, 2025 21:22
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (3)
packages/kg-default-nodes/lib/utils/visibility.js (1)

40-77: Promote fallback handling for unknown rendering targets.

The function currently handles 'email' and defaults to 'web' for any other value without explicit checks or error messages. Consider either throwing an error for unknown targets or defining a documented fallback to prevent unintentional behavior if targets like 'pdf' are used.

packages/kg-default-nodes/lib/nodes/html/html-renderer.js (1)

25-25: Keep inline comments consistent.

The inline comment clarifies intent. For consistency, consider adding a short explanation whenever returning an object with {element, type} for newly introduced blocks.

packages/kg-default-nodes/test/utils/visibility.test.js (1)

62-200: Impressive coverage for various visibility scenarios.

These tests comprehensively cover old vs. new formats, multiple render types, and membership segments. One extension could be testing unexpected targets to ensure graceful handling or errors, mirroring any fallback logic in renderWithVisibility.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 222a942 and 8cbc28f.

📒 Files selected for processing (3)
  • packages/kg-default-nodes/lib/nodes/html/html-renderer.js (2 hunks)
  • packages/kg-default-nodes/lib/utils/visibility.js (2 hunks)
  • packages/kg-default-nodes/test/utils/visibility.test.js (2 hunks)
🔇 Additional comments (4)
packages/kg-default-nodes/lib/utils/visibility.js (3)

81-92: Validate the DOM element before extracting content.

If element is unexpectedly null/undefined, this block will throw an error. Consider adding a small guard to ensure element is valid, or at least document that a valid DOM element is required.


94-101: Watch for potential HTML injection in container.innerHTML.

When assigning content to innerHTML, it's possible that untrusted markup could be injected. If the input is user-generated, confirm that sanitization is performed upstream to avoid XSS risks.


102-108: Consider sanitizing or validating wrapped comments.

Wrapping content inside HTML comments is effective for gating, but if content is user-supplied, ensure you’ve properly handled or sanitized any malicious scripts injected into the comment block.

packages/kg-default-nodes/lib/nodes/html/html-renderer.js (1)

3-3: Delegate to new renderWithVisibility logic looks good.

Replacing the inline visibility handling with renderWithVisibility centralizes the gating logic and simplifies maintainability. This is a solid refactor.

Also applies to: 20-22

@kevinansfield kevinansfield merged commit 9ab1269 into TryGhost:main Feb 5, 2025
2 checks passed
@kevinansfield kevinansfield deleted the extract-visibility-rendering branch February 5, 2025 08:03
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

Successfully merging this pull request may close these issues.

2 participants