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

✨ Render markdown with mermaid graph #4

Merged
merged 9 commits into from
Oct 7, 2024

Conversation

zhumeisongsong
Copy link
Owner

@zhumeisongsong zhumeisongsong commented Oct 7, 2024

image

Summary by CodeRabbit

  • New Features

    • Introduced new components: BodyMarkdown for rendering Markdown content and BodyMermaid for displaying Mermaid diagrams.
    • Enhanced document on Domain-Driven Design (DDD) with expanded content and a new section discussing its applicability.
  • Bug Fixes

    • Improved rendering of Markdown and Mermaid diagrams, simplifying the logic in the PostBody component.
  • Style

    • Added styles for unordered lists in Markdown and centered text alignment for Mermaid diagrams.

Copy link

coderabbitai bot commented Oct 7, 2024

Warning

Rate limit exceeded

@zhumeisongsong has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 2 minutes and 50 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Files that changed from the base of the PR and between a0bc7d9 and 5ea6399.

Walkthrough

The changes introduced in this pull request include the enhancement of a document on Domain-Driven Design (DDD) with a new title and expanded content, as well as the addition of two new React components: BodyMarkdown and BodyMermaid. These components facilitate the rendering of Markdown and Mermaid diagrams, respectively. Additionally, CSS styles have been updated to improve the presentation of Markdown content and Mermaid diagrams. The PostBody component has been refactored to utilize these new components, simplifying its logic.

Changes

File Path Change Summary
_posts/2024-08-29-DDD-1.md Title updated to "Domain-Driven Design 1"; content expanded with sections on software design approaches, DDD, and domain models.
src/app/_components/body-markdown.tsx New component BodyMarkdown added; converts Markdown to HTML and renders it.
src/app/_components/body-mermaid.tsx New component BodyMermaid added; renders Mermaid diagrams based on provided graph prop.
src/app/_components/markdown-styles.module.css New style rule for unordered lists added to enhance visual presentation.
src/app/_components/mermaid-styles.module.css New CSS rule added to center text in elements with the .mermaid class.
src/app/_components/post-body.tsx Refactored to use BodyMarkdown and BodyMermaid; removed previous rendering logic.
src/lib/markdown-to-html.ts Updated markdownToHtml function to include remarkGfm and remarkHtml plugins for enhanced Markdown processing.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant PostBody
    participant BodyMarkdown
    participant BodyMermaid

    User->>PostBody: Provide content
    PostBody->>BodyMarkdown: Render markdown content
    PostBody->>BodyMermaid: Render mermaid diagrams
    BodyMarkdown-->>PostBody: Return HTML
    BodyMermaid-->>PostBody: Return rendered diagram
    PostBody-->>User: Display combined content
Loading

🐰 "In the meadow, changes bloom,
New designs lift every room.
Markdown flows, diagrams gleam,
In this code, we weave a dream!
With styles that dance, and logic so clear,
Celebrate the changes, let’s give a cheer!" 🐇


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

🧹 Outside diff range and nitpick comments (10)
src/lib/markdown-to-html.ts (1)

7-8: LGTM! Consider adding error handling.

The changes to enable remarkGfm and remarkHtml plugins are appropriate and align with the PR objectives. These additions will enhance the markdown parsing capabilities, particularly for GitHub Flavored Markdown features, which is beneficial for rendering mermaid graphs.

Consider adding error handling to make the function more robust:

 export default async function markdownToHtml(markdown: string) {
-  const result = await remark()
-    .use(remarkGfm)
-    .use(remarkHtml)
-    .process(markdown);
-
-  return result.toString();
+  try {
+    const result = await remark()
+      .use(remarkGfm)
+      .use(remarkHtml)
+      .process(markdown);
+
+    return result.toString();
+  } catch (error) {
+    console.error('Error processing markdown:', error);
+    throw new Error('Failed to process markdown');
+  }
 }

This change will help catch and log any errors that occur during markdown processing, making it easier to debug issues in the future.

src/app/_components/markdown-styles.module.css (2)

30-33: LGTM! Consider using Tailwind classes for consistency.

The addition of styles for unordered lists is a good improvement. It enhances the readability and visual structure of the markdown content. However, to maintain consistency with the rest of the file, consider using Tailwind classes instead of raw CSS properties.

Here's a suggested refactor using Tailwind classes:

 .markdown ul {
-  list-style: disc;
-  margin-left: 32px;
+  @apply list-disc ml-8;
 }

This change uses the list-disc utility for the bullet style and ml-8 for the left margin (which is equivalent to 32px in Tailwind's default scale).


35-35: Remove unnecessary empty lines at the end of the file.

There are unnecessary empty lines at the end of the file. It's a good practice to end files with a single newline character.

Consider removing the extra empty lines to keep the file clean and consistent.

src/app/_components/body-mermaid.tsx (1)

7-9: Add a comment to explain the 'graph' prop.

While the type definition is correct, it would be helpful to add a comment explaining the expected format of the 'graph' string. This can improve code readability and make it easier for other developers to use the component correctly.

Consider adding a comment like this:

type Props = {
  /** A string containing the Mermaid diagram definition */
  graph: string;
};
src/app/_components/body-markdown.tsx (1)

13-17: LGTM with a minor optimization suggestion.

The useEffect hook is implemented correctly, ensuring that the Markdown is converted to HTML whenever the content changes.

Consider memoizing the markdownToHtml function call to optimize performance, especially if the content doesn't change frequently:

const memoizedHtml = useMemo(() => markdownToHtml(content), [content]);

useEffect(() => {
  memoizedHtml.then(setHtml);
}, [memoizedHtml]);

This approach can help reduce unnecessary re-renders and improve performance.

_posts/2024-08-29-DDD-1.md (5)

Line range hint 2-9: Consider adding an excerpt for better SEO and preview.

The title update to "Domain-Driven Design 1" is good, as it suggests this might be part of a series. However, the excerpt is currently empty. Consider adding a brief description (1-2 sentences) of the article's content in the excerpt field to improve SEO and provide a preview for readers.

Example:

excerpt: "An introduction to Domain-Driven Design (DDD) and its place among software design approaches. Learn about DDD's core concepts and when to apply them."

Line range hint 11-72: Great structure, consider completing placeholder sections.

The content is well-organized with a logical flow from general software design approaches to specific DDD concepts. The inclusion of the "You may not need DDD" section provides a balanced perspective.

However, some sections appear to be placeholders or incomplete:

  • "DDD 's value"
  • "Key Concepts"
  • "How DDD domain-driven design can be engineered to the ground?"
  • "How I practice in my time-travel-quiz-game product?"

Consider expanding these sections to provide more value to the reader and complete the article.


Line range hint 11-72: Consider enhancing explanations and providing examples.

The content provides a good overview of different software design approaches and DDD. To further improve clarity and depth:

  1. In the "Data-driven design" section, consider providing a brief example of a "simple small system" where this approach is sufficient.

  2. In the "Domain-driven design" section, the statement "Makes that domain model decoupled from the data model" could benefit from a brief explanation of why this decoupling is important.

  3. In the "You may not need DDD" section, consider providing specific criteria or examples of when software might be considered "simple and small" enough to not require DDD.

  4. The question "What's the mean of software complexity?" contains a grammatical error. Consider rephrasing to "What does software complexity mean?" or "How is software complexity defined?"

These enhancements would provide readers with a more comprehensive understanding of the concepts presented.


Line range hint 11-72: Good use of Markdown, with room for minor enhancements.

The Markdown syntax is well-applied throughout the document. Here are some suggestions for further improvement:

  1. Consider using consistent emphasis for key terms. For example, simple small systems and the design of complex systems use backticks, while other terms like "business domain" and "domain model" could also benefit from this emphasis.

  2. The alt text for the image ("/blog/assets/ddd-complexity-of-domain-logic.png") could be more descriptive. Instead of "alt text", consider describing what the image shows, e.g., "Graph showing the relationship between domain logic complexity and the need for a domain model".

  3. For the Mermaid diagram, consider adding a caption or description below it to explain its significance in the context of the article.

These small changes can enhance readability and accessibility of the document.


Line range hint 13-17: Enhance context and explanation for diagrams.

The inclusion of visual aids (Mermaid graph and image) is excellent for illustrating concepts. To maximize their effectiveness:

  1. For the Mermaid graph:

    • Consider adding a brief explanation or caption below the graph to explain its significance.
    • You might want to expand on why there's a progression from Deadline-driven to Data-driven to Domain-driven design.
  2. For the image "/blog/assets/ddd-complexity-of-domain-logic.png":

    • Add a caption or brief explanation of what the image represents and how it relates to the concept of domain models in DDD.
    • Ensure the image is properly referenced or discussed in the surrounding text.

These additions will help readers better understand the purpose and relevance of these visual elements in the context of your article.

Also applies to: 70-72

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 5d8b112 and a0bc7d9.

📒 Files selected for processing (7)
  • _posts/2024-08-29-DDD-1.md (1 hunks)
  • src/app/_components/body-markdown.tsx (1 hunks)
  • src/app/_components/body-mermaid.tsx (1 hunks)
  • src/app/_components/markdown-styles.module.css (1 hunks)
  • src/app/_components/mermaid-styles.module.css (1 hunks)
  • src/app/_components/post-body.tsx (1 hunks)
  • src/lib/markdown-to-html.ts (1 hunks)
✅ Files skipped from review due to trivial changes (1)
  • src/app/_components/mermaid-styles.module.css
🧰 Additional context used
🪛 Biome
src/app/_components/body-markdown.tsx

[error] 22-22: Avoid passing content using the dangerouslySetInnerHTML prop.

Setting content using code can expose users to cross-site scripting (XSS) attacks

(lint/security/noDangerouslySetInnerHtml)

🔇 Additional comments (6)
src/app/_components/post-body.tsx (2)

2-3: Excellent separation of concerns with new component imports.

The introduction of BodyMarkdown and BodyMermaid components demonstrates a good separation of concerns. This approach enhances modularity and makes the codebase more maintainable. The use of path aliases (starting with "@") in the import statements is also a good practice for keeping imports clean and readable.


Line range hint 1-22: Overall positive changes with room for refinement.

The refactoring of the PostBody component represents a significant improvement in code organization and separation of concerns. The introduction of specialized components for Markdown and Mermaid content enhances modularity and maintainability.

While the current implementation is functional, there are opportunities for further refinement:

  1. Improving the accuracy of Mermaid graph detection.
  2. Implementing a more robust parsing strategy for mixed content.
  3. Addressing potential issues with nested code blocks and preserving formatting.

These refinements would elevate the component's reliability and flexibility in handling various content scenarios.

Despite the suggested improvements, the overall direction of these changes is positive and aligns well with React best practices.

src/app/_components/body-mermaid.tsx (1)

11-25: Overall implementation looks good.

The component logic is well-structured and uses React hooks appropriately. The use of useRef and useEffect to manage the mermaid diagram rendering is a good approach.

src/app/_components/body-markdown.tsx (2)

1-9: LGTM: Imports and type definition are well-structured.

The "use client" directive, imports, and Props type definition are correctly implemented and appropriate for the component's functionality.


11-12: LGTM: Component declaration and state initialization are correct.

The component is properly exported, and the state initialization using useState is appropriate for managing the HTML content.

_posts/2024-08-29-DDD-1.md (1)

Line range hint 1-72: Overall, excellent improvements with minor suggestions for enhancement.

This update to the Domain-Driven Design article represents a significant improvement. The content now provides a comprehensive overview of different software design approaches, leading into a focused discussion on DDD. The logical structure and flow of information make the complex topic more accessible to readers.

Key strengths:

  1. Clear progression from general software design approaches to DDD specifics.
  2. Inclusion of balanced perspectives (e.g., "You may not need DDD" section).
  3. Use of visual aids (Mermaid graph and image) to support the text.

Areas for potential enhancement:

  1. Completing placeholder sections to provide a fully comprehensive guide.
  2. Adding more examples or specific criteria to clarify certain concepts.
  3. Enhancing the context and explanation for the included diagrams.
  4. Minor improvements in Markdown formatting for consistency and accessibility.

These suggested improvements are relatively minor and can easily be addressed to further elevate the quality of this already valuable content. Great work on this update!

src/app/_components/post-body.tsx Outdated Show resolved Hide resolved
src/app/_components/body-mermaid.tsx Show resolved Hide resolved
src/app/_components/body-mermaid.tsx Outdated Show resolved Hide resolved
src/app/_components/body-markdown.tsx Outdated Show resolved Hide resolved
@zhumeisongsong zhumeisongsong merged commit a85aacb into main Oct 7, 2024
4 checks passed
@zhumeisongsong zhumeisongsong deleted the feature/render-markdown branch October 7, 2024 14:58
@zhumeisongsong
Copy link
Owner Author

🎉 This PR is included in version 1.6.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant