8000 feat: add CodeBlock component for syntax highlighting by hetunandu · Pull Request #40223 · appsmithorg/appsmith · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: add CodeBlock component for syntax highlighting #40223

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

Merged
merged 2 commits into from
Apr 11, 2025

Conversation

hetunandu
Copy link
Member
@hetunandu hetunandu commented Apr 11, 2025

Description

  • Introduced a new CodeBlock component that utilizes react-syntax-highlighter for displaying code snippets with syntax highlighting.
  • Implemented responsive theming based on the current theme context (dark/light).
  • Added Storybook stories for the CodeBlock component to demonstrate its usage and capabilities.

This addition enhances the design system by providing a reusable component for code display.

Automation

/ok-to-test tags="@tag.Anvil"

🔍 Cypress test results

Tip

🟢 🟢 🟢 All cypress tests have passed! 🎉 🎉 🎉
Workflow run: https://github.com/appsmithorg/appsmith/actions/runs/14402871511
Commit: 81bb9cf
Cypress dashboard.
Tags: @tag.Anvil
Spec:


Fri, 11 Apr 2025 12:40:24 UTC

Communication

Should the DevRel and Marketing teams inform users about this change?

  • Yes
  • No

Summary by CodeRabbit

  • New Features
    • Introduced a new CodeBlock component that renders code snippets with syntax highlighting and theme support (light/dark modes).
    • Enabled customizable styling and behavior through property options.
    • Added interactive examples for previewing different code formats and sizes.
    • Consolidated access to the CodeBlock component through new export statements in the design system.

- Introduced a new CodeBlock component that utilizes react-syntax-highlighter for displaying code snippets with syntax highlighting.
- Implemented responsive theming based on the current theme context (dark/light).
- Added Storybook stories for the CodeBlock component to demonstrate its usage and capabilities.

This addition enhances the design system by providing a reusable component for code display.
@hetunandu hetunandu requested a review from a team as a code owner April 11, 2025 11:17
Copy link
Contributor
coderabbitai bot commented Apr 11, 2025

Walkthrough

This pull request introduces the CodeBlock component to the design system, which renders syntax-highlighted code using the react-syntax-highlighter library. It includes updated export statements for easier access to the CodeBlock component and its related entities. Additionally, a new Storybook configuration has been created to provide interactive examples of the component through defined stories.

Changes

File(s) Change Summary
.../components/CodeBlock/index.ts
.../components/CodeBlock/src/index.ts
Added export statements to re-export CodeBlock entities for consolidated access.
.../components/CodeBlock/src/CodeBlock.tsx Introduced new React component CodeBlock; utilizes react-syntax-highlighter, theme context, and typography classes.
.../components/CodeBlock/stories/CodeBlock.stories.tsx Added new Storybook file with metadata and two stories (Main and Sizes) showcasing the CodeBlock component.
.../components/CodeBlock/src/index.ts
.../components/index.ts
Added export statements to facilitate access to the CodeBlock component from the main entry point.

Sequence Diagram(s)

sequenceDiagram
    participant App
    participant CodeBlock
    participant ThemeContext
    participant SyntaxHighlighter

    App->>CodeBlock: Render CodeBlock with code snippet
    CodeBlock->>ThemeContext: Retrieve current theme
    ThemeContext-->>CodeBlock: Return dark/light theme info
    CodeBlock->>SyntaxHighlighter: Render code with props and theme
    SyntaxHighlighter-->>CodeBlock: Return highlighted code
    CodeBlock-->>App: Display highlighted code snippet
Loading

Poem

A code block blossoms on our screen,
With themes that dance in light and dark sheen.
Exports aligned, stories in play,
Highlighting syntax in a refined display.
Cheers to clean code and creative sway!
🚀 Happy coding!


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro (Legacy)

📥 Commits

Reviewing files that changed from the base of the PR and between 2897614 and 81bb9cf.

📒 Files selected for processing (1)
  • app/client/packages/design-system/widgets/src/index.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (8)
  • GitHub Check: perform-test / rts-build / build
  • GitHub Check: perform-test / server-build / server-unit-tests
  • GitHub Check: client-lint / client-lint
  • GitHub Check: client-build / client-build
  • GitHub Check: client-prettier / prettier-check
  • GitHub Check: chromatic-deployment
  • GitHub Check: chromatic-deployment
  • GitHub Check: storybook-tests
🔇 Additional comments (1)
app/client/packages/design-system/widgets/src/index.ts (1)

39-39: Export for the new CodeBlock component looks good.

The export statement follows the established pattern in the file and makes the new CodeBlock component accessible to consumers of the design system.


🪧 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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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.

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.

@hetunandu hetunandu requested review from ichik and removed request for a team April 11, 2025 11:17
@github-actions github-actions bot added the Enhancement New feature or request label Apr 11, 2025
@hetunandu hetunandu added the ok-to-test Required label for CI label Apr 11, 2025
Copy link
Contributor
@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)
app/client/packages/design-system/widgets/src/components/CodeBlock/src/CodeBlock.tsx (1)

10-23: Consider some improvements to the CodeBlock implementation.

The CodeBlock component correctly implements syntax highlighting with theming support, but there are some potential enhancements:

  1. The spreading of props and explicit children usage is redundant
  2. No error handling for invalid/empty code content
  3. No documentation for the useInlineStyles prop

Consider these improvements:

 export const CodeBlock = (props: SyntaxHighlighterProps) => {
   const theme = useThemeContext();
+  // Ensure children content exists or provide fallback
+  const content = props.children || '';
 
   return (
     <SyntaxHighlighter
       className={getTypographyClassName("caption")}
       {...props}
       style={theme.colorMode === "dark" ? darkTheme : lightTheme}
       useInlineStyles
+      // useInlineStyles is required to apply the theme styling correctly
     >
-      {props.children}
+      {content}
     </SyntaxHighlighter>
   );
 };
app/client/packages/design-system/widgets/src/components/CodeBlock/stories/CodeBlock.stories.tsx (2)

16-21: Consider API consistency between stories.

The Main story passes code via the code prop while the Sizes story passes it as children.

For consistency, consider using the same approach in both stories:

 export const Main: Story = {
   args: {
-    code: "{ test: 123 }",
     language: "json",
+    children: "{ test: 123 }",
   },
 };

23-33: Rename story to better reflect its purpose.

The name "Sizes" doesn't accurately describe what this story demonstrates, which is the component's ability to render different languages.

- export const Sizes: Story = {
+ export const DifferentLanguages: Story = {
   render: (props) => (
     <CodeBlock language={props.language} {...props}>
       {props.code}
     </CodeBlock>
   ),
 };

Also, consider adding examples with different languages to better showcase the component's capabilities.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between abc9c71 and 2897614.

📒 Files selected for processing (4)
  • app/client/packages/design-system/widgets/src/components/CodeBlock/index.ts (1 hunks)
  • app/client/packages/design-system/widgets/src/components/CodeBlock/src/CodeBlock.tsx (1 hunks)
  • app/client/packages/design-system/widgets/src/components/CodeBlock/src/index.ts (1 hunks)
  • app/client/packages/design-system/widgets/src/components/CodeBlock/stories/CodeBlock.stories.tsx (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
app/client/packages/design-system/widgets/src/components/CodeBlock/src/CodeBlock.tsx (1)
app/client/src/constants/DefaultTheme.tsx (1)
  • theme (2749-3016)
⏰ Context from checks skipped due to timeout of 90000ms (10)
  • GitHub Check: perform-test / client-build / client-build
  • GitHub Check: perform-test / server-build / server-unit-tests
  • GitHub Check: perform-test / rts-build / build
  • GitHub Check: client-unit-tests / client-unit-tests
  • GitHub Check: client-lint / client-lint
  • GitHub Check: client-build / client-build
  • GitHub Check: client-prettier / prettier-check
  • GitHub Check: chromatic-deployment
  • GitHub Check: storybook-tests
  • GitHub Check: chromatic-deployment
🔇 Additional comments (4)
app/client/packages/design-system/widgets/src/components/CodeBlock/index.ts (1)

1-1: Export implementation looks good.

The index file correctly re-exports all entities from the src/index module, following standard module pattern practices.

app/client/packages/design-system/widgets/src/components/CodeBlock/src/index.ts (1)

1-1: Export implementation looks good.

The src/index.ts file correctly re-exports all entities from the CodeBlock module, maintaining a clean export structure.

app/client/packages/design-system/widgets/src/components/CodeBlock/src/CodeBlock.tsx (1)

1-9: Import statements look good.

All necessary dependencies are properly imported, including React, SyntaxHighlighter component, types, themes, and utility functions.

app/client/packages/design-system/widgets/src/components/CodeBlock/stories/CodeBlock.stories.tsx (1)

1-14: Storybook setup looks good.

The metadata and Story type definition are properly set up for the component.

ichik
ichik previously approved these changes Apr 11, 2025
@hetunandu hetunandu merged commit 7cdee43 into release Apr 11, 2025
52 checks passed
@hetunandu hetunandu deleted the chore/wds-code-block branch April 11, 2025 13:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement New feature or request ok-to-test Required label for CI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0