fix: strip XML preamble in devvit create icons#249
Open
mvanhorn wants to merge 2 commits intoreddit:mainfrom
Open
fix: strip XML preamble in devvit create icons#249mvanhorn wants to merge 2 commits intoreddit:mainfrom
mvanhorn wants to merge 2 commits intoreddit:mainfrom
Conversation
Fixes reddit#128 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
niedzielski
reviewed
Mar 19, 2026
| const contents = await fsp.readFile(asset, 'utf-8'); | ||
| return { name, contents }; | ||
| // Strip XML preamble (<?xml?>, <!DOCTYPE>, comments) before <svg> tag. | ||
| const stripped = contents.replace(/^[\s\S]*?(<svg)/i, '$1'); |
Contributor
There was a problem hiding this comment.
would you mind extracting this to a standalone function and adding a few unit tests?
Author
There was a problem hiding this comment.
Extracted to stripXmlPreamble() in 3b156af and added 6 unit tests covering: declaration, <!DOCTYPE>, comments, combined preamble, no-op when already clean, and leading whitespace.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Strips XML declarations and DOCTYPE tags from SVG files before inserting them into the generated icons map.
Why this matters
#128 reports that
devvit create iconsproduces broken icons when SVG files contain XML preambles. SVG editors like Illustrator and Inkscape commonly export files with<?xml version="1.0" encoding="UTF-8"?>before the<svg>tag. ThesanitizeSvg()function in@devvit/shared-typeschecksif (!svg.trim().startsWith('<svg'))and returnsundefinedfor these files, resulting in empty icon entries.Changes
In
packages/cli/src/commands/create/icons.ts, added a regex strip in#getSvgAssets()that removes everything before the first<svgtag when reading each SVG file. This handles<?xml?>declarations,<!DOCTYPE>tags, and XML comments that precede the SVG element.Testing
The regex
contents.replace(/^[\s\S]*?(<svg)/i, '$1')preserves the full<svg>element and everything inside it while stripping only content before the opening tag. SVG files without preambles are unaffected since the regex matches the<svgat position 0.Fixes #128
This contribution was developed with AI assistance (Claude Code + Codex CLI).