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

Add & use BitMarkdownService class to BlazorUI (#10221) #10223

Merged
merged 5 commits into from
Mar 10, 2025

Conversation

msynk
Copy link
Member

@msynk msynk commented Mar 10, 2025

closes #10221

Summary by CodeRabbit

  • New Features

    • The Markdown viewer now delivers a smoother and quicker rendering experience with enhanced parsing logic.
  • Refactor

    • Streamlined the markdown processing workflow to simplify initialization and improve overall performance.

Copy link

coderabbitai bot commented Mar 10, 2025

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The changes refactor the markdown processing logic in the BitMarkdownViewer component. The previous implementation using the RunJint and ReadMarkedScriptText methods along with associated variables has been removed. Instead, the viewer now calls the new BitMarked.Parse method during initialization to process markdown content. A new BitMarked class is introduced to handle script loading, concurrency via a semaphore, and executing the JavaScript parsing logic using the Jint engine. Error handling for missing script files is maintained.

Changes

File Change Summary
src/BlazorUI/.../Components/MarkdownViewer/BitMarkdownViewer.razor.cs Removed methods: RunJint, ReadMarkedScriptText and associated variables (_markedScriptText, _markedScriptReadTextSemaphore). Updated initialization logic to call BitMarked.Parse directly.
src/BlazorUI/.../Services/BitMarked.cs Added new class BitMarked with static method Parse for markdown parsing. Added private method ReadMarkedScriptText to manage script loading and concurrency control using a semaphore.

Sequence Diagram(s)

sequenceDiagram
    participant Viewer as BitMarkdownViewer
    participant Parser as BitMarked
    participant FS as File System (Script Loader)

    Viewer->>Parser: OnInitializedAsync calls Parse(markdown)
    Parser->>FS: Acquire semaphore & read script (ReadMarkedScriptText)
    FS-->>Parser: Return script text (or error)
    Parser->>Parser: Configure Jint Engine & evaluate script
    Parser-->>Viewer: Return parsed HTML
Loading

Poem

I hopped through tangled code with glee,
Clearing paths for markdown to be free.
No more scripts hiding in the mist,
With BitMarked's help, they now co-exist.
A coding dance, light and bright—
Hoppy days and bugs taking flight!
🐰✨


🪧 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.
  • @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.

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: 1

🧹 Nitpick comments (1)
src/BlazorUI/Bit.BlazorUI.Extras/Services/BitMarked.cs (1)

34-63: Consider adding disposal of resources.

The semaphore is created but never disposed, which could potentially lead to resource leaks if the application is reloaded multiple times in the same process.

Consider making the BitMarked class implement IDisposable to properly clean up resources:

-public class BitMarked
+public class BitMarked : IDisposable
 {
     private static string? _markedScriptText;
     private static readonly SemaphoreSlim _markedScriptReadTextSemaphore = new(1, 1);
     
     // Existing code...
     
+    public void Dispose()
+    {
+        _markedScriptReadTextSemaphore.Dispose();
+    }
 }

Alternatively, if you prefer to keep it as a static class, consider adding a static Dispose method that can be called during application shutdown.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 10e1826 and 361958b.

📒 Files selected for processing (2)
  • src/BlazorUI/Bit.BlazorUI.Extras/Components/MarkdownViewer/BitMarkdownViewer.razor.cs (2 hunks)
  • src/BlazorUI/Bit.BlazorUI.Extras/Services/BitMarked.cs (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build and test
🔇 Additional comments (3)
src/BlazorUI/Bit.BlazorUI.Extras/Components/MarkdownViewer/BitMarkdownViewer.razor.cs (1)

34-38: Good refactoring to use BitMarked service.

The code has been simplified by delegating the markdown parsing logic to the new BitMarked service. The Task.Run ensures the potentially heavy parsing operation doesn't block the UI thread, and invoking StateHasChanged afterward ensures the UI is properly updated once parsing completes.

src/BlazorUI/Bit.BlazorUI.Extras/Services/BitMarked.cs (2)

6-10: Good structure for the new BitMarked service class.

The static fields and semaphore implementation provide an efficient way to load and cache the script text, ensuring it's only loaded once and thread-safety is maintained when accessing it.


13-30: Effective implementation of the Parse method.

The Parse method correctly handles null/empty input, uses the Jint engine with appropriate options, and properly executes the marked.parse function. The use of cancellation tokens is a good practice for long-running operations.

@msynk msynk changed the title Add BitMarked service class to BlazorUI (#10221) Add & use BitMarkdownService class to BlazorUI (#10221) Mar 10, 2025
@msynk msynk merged commit e7d2f5c into bitfoundation:develop Mar 10, 2025
3 checks passed
@msynk msynk deleted the 10221-blazorui-markdownservice branch March 10, 2025 09:12
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.

The BlazorUI project needs a service for converting .md files to html
1 participant