Skip to content

Conversation

@tomerqodo
Copy link

@tomerqodo tomerqodo commented Dec 4, 2025

User description

Benchmark PR discourse#36240

Type: Clean (correct implementation)

Original PR Title: FIX: maintain extension when quoting images
Original PR Description: Also do not show quote image to anonymous

Original PR URL: discourse#36240


PR Type

Bug fix, Enhancement


Description

  • Maintain file extension when quoting images with upload:// URLs

  • Hide quote image button from anonymous users

  • Refactor canQuoteImage to canBuildImageQuote for clarity

  • Extract extensionFromUrl utility function for reuse

  • Update tests to verify extension preservation in quotes


Diagram Walkthrough

flowchart LR
  A["Image Quote Flow"] --> B["Check User Authentication"]
  B --> C["Extract File Extension"]
  C --> D["Build upload:// URL with Extension"]
  D --> E["Generate Markdown Quote"]
  B --> F["Hide Quote Button for Anonymous"]
Loading

File Walkthrough

Relevant files
Enhancement
lightbox.js
Restrict quote button visibility to authenticated users   

frontend/discourse/app/lib/lightbox.js

  • Replace User.current() with helperContext()?.currentUser for better
    context management
  • Add canQuoteImage flag based on current user authentication status
  • Wrap quote-image UI element registration in conditional check to hide
    from anonymous users
  • Update import to use renamed canBuildImageQuote function
+38/-33 
markdown-image-builder.js
Add extensionFromUrl utility function                                       

frontend/discourse/app/lib/markdown-image-builder.js

  • Add new extensionFromUrl utility function to extract file extensions
    from URLs
  • Function handles query parameters and returns null for URLs without
    extensions
  • Supports alphanumeric extensions with regex pattern matching
+16/-0   
Bug fix
quote-image.js
Add extension preservation to upload URLs                               

frontend/discourse/app/lib/lightbox/quote-image.js

  • Import extensionFromUrl utility function from markdown-image-builder
  • Extract and append file extension to upload:// URLs when base62SHA1 is
    present
  • Rename canQuoteImage export to canBuildImageQuote with added JSDoc
    documentation
  • Preserve original file extension in short upload URL format
+16/-2   
to-markdown.js
Apply extension preservation to markdown conversion           

frontend/discourse/app/lib/to-markdown.js

  • Import extensionFromUrl utility function
  • Apply extension preservation logic to link href conversion in anchor
    tags
  • Apply extension preservation logic to image src conversion
  • Check multiple sources for extension fallback (src, data-orig-src,
    data-download-href)
+19/-1   
Tests
quote-image-test.js
Update tests for authentication and extension preservation

frontend/discourse/tests/unit/lib/lightbox/quote-image-test.js

  • Set up helper context with current user in beforeEach hook
  • Log in test user to enable quote functionality
  • Preserve and restore original helper context in afterEach hook
  • Rename test references from canQuoteImage to canBuildImageQuote
  • Update test assertion to verify extension is included in upload URL
+21/-7   
to-markdown-test.js
Update markdown conversion test expectations                         

frontend/discourse/tests/unit/lib/to-markdown-test.js

  • Update test expectations to include .png extension in upload:// URLs
  • Verify extension is preserved for both image and link conversions
  • Update test description to reflect extension preservation behavior
+2/-2     

@qodo-code-review
Copy link

PR Compliance Guide 🔍

Below is a summary of compliance checks for this PR:

Security Compliance
🟢
No security concerns identified No security vulnerabilities detected by AI analysis. Human verification advised for critical code.
Ticket Compliance
🎫 No ticket provided
  • Create ticket/issue
Codebase Duplication Compliance
Codebase context is not defined

Follow the guide to enable codebase context checks.

Custom Compliance
🟢
Generic: Meaningful Naming and Self-Documenting Code

Objective: Ensure all identifiers clearly express their purpose and intent, making code
self-documenting

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Robust Error Handling and Edge Case Management

Objective: Ensure comprehensive error handling that provides meaningful context and graceful
degradation

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Error Handling

Objective: To prevent the leakage of sensitive system information through error messages while
providing sufficient detail for internal debugging.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Secure Logging Practices

Objective: To ensure logs are useful for debugging and auditing without exposing sensitive
information like PII, PHI, or cardholder data.

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Security-First Input Validation and Data Handling

Objective: Ensure all data inputs are validated, sanitized, and handled securely to prevent
vulnerabilities

Status: Passed

Learn more about managing compliance generic rules or creating your own custom rules

Generic: Comprehensive Audit Trails

Objective: To create a detailed and reliable record of critical system actions for security analysis
and compliance.

Status:
No Audit Logs: The new UI actions (hiding/using quote-image based on authentication) and quoting behavior
introduce user actions without any added audit logging, which may be acceptable for
non-critical actions but requires verification.

Referred Code
if (canQuoteImage) {
  lightboxEl.pswp.ui.registerElement({
    name: "quote-image",
    order: 10,
    isButton: true,
    title: i18n("lightbox.quote"),
    html: {
      isCustomSVG: true,
      inner:
        '<path id="pswp__icn-quote" d="M0 216C0 149.7 53.7 96 120 96l8 0c17.7 0 32 14.3 32 32s-14.3 32-32 32l-8 0c-30.9 0-56 25.1-56 56l0 8 64 0c35.3 0 64 28.7 64 64l0 64c0 35.3-28.7 64-64 64l-64 0c-35.3 0-64-28.7-64-64l0-32 0-32 0-72zm256 0c0-66.3 53.7-120 120-120l8 0c17.7 0 32 14.3 32 32s-14.3 32-32 32l-8 0c-30.9 0-56 25.1-56 56l0 8 64 0c35.3 0 64 28.7 64 64l0 64c0 35.3-28.7 64-64 64l-64 0c-35.3 0-64-28.7-64-64l0-32 0-32 0-72z"/>',
      outlineID: "pswp__icn-quote",
      size: 512,
    },
    onInit: (el, pswp) => {
      pswp.on("change", () => {
        const slideData = pswp.currSlide?.data;
        const slideElement = slideData?.element;
        el.style.display = canBuildImageQuote(slideElement, slideData)
          ? ""
          : "none";
      });


 ... (clipped 12 lines)

Learn more about managing compliance generic rules or creating your own custom rules

Compliance status legend 🟢 - Fully Compliant
🟡 - Partial Compliant
🔴 - Not Compliant
⚪ - Requires Further Human Verification
🏷️ - Compliance label

@qodo-code-review
Copy link

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
Fix regex to correctly extract extension

Modify the regex in extensionFromUrl to correctly extract the file extension by
matching the last dot in the URL, preventing errors with filenames that contain
multiple dots.

frontend/discourse/app/lib/markdown-image-builder.js [28]

-const match = url.match(/\.([a-zA-Z0-9]+)(?:\?|$)/);
+const match = url.match(/.*\.([a-zA-Z0-9]+)(?:\?|$)/);
  • Apply / Chat
Suggestion importance[1-10]: 7

__

Why: The suggestion correctly identifies a bug in the new extensionFromUrl function where the regex would fail to extract the correct extension for filenames containing multiple dots, and the proposed fix is accurate.

Medium
  • More

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants