Skip to content

Comments

fix: prevent HTML tag duplication in ZIP export#286

Open
martinezharo wants to merge 1 commit intomidudev:mainfrom
martinezharo:improved-download
Open

fix: prevent HTML tag duplication in ZIP export#286
martinezharo wants to merge 1 commit intomidudev:mainfrom
martinezharo:improved-download

Conversation

@martinezharo
Copy link

Currently, when exporting code as a ZIP file, the index.html is generated by wrapping the user's input within a static template. This causes structural issues and tag duplication (e.g., double <html> or <body> tags) if the user provides a complete HTML boilerplate.

This PR introduces a smart wrapping logic that detects existing structural elements and only injects the missing parts, ensuring a valid and clean HTML document.

Changes

  • Refactored src/download.js: Replaced the hardcoded template literal in createZipWithMultipleFiles with a dedicated buildIndexHtml helper function.
  • Implemented buildIndexHtml: Added logic to conditionally inject the following elements only if they are missing:
    • <!DOCTYPE html>
    • <html lang="en">
    • <head> (including the <link> to style.css)
    • <body>
    • <script type="module" src="script.js">

Benefits

  • Zero Redundancy: Users can provide either a full HTML document or a simple fragment; the output remains valid and non-redundant.
  • Robust Detection: The implementation uses case-insensitive Regex to account for attributes (e.g., <body class="dark"> or <html lang="es">).
  • Improved DX: Prevents broken layouts in downloaded projects.

Testing Performed

  • Fragment Test: Verified that simple HTML (e.g., <h1>Hello</h1>) is correctly wrapped in a full template.
  • Boilerplate Test: Verified that full HTML documents are exported without duplicate injections.
  • Linting: Validated code style compliance with neostandard.

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.

1 participant