Skip to content

fix: strip XML preamble in devvit create icons#249

Open
mvanhorn wants to merge 2 commits intoreddit:mainfrom
mvanhorn:osc/128-icons-strip-xml-preamble
Open

fix: strip XML preamble in devvit create icons#249
mvanhorn wants to merge 2 commits intoreddit:mainfrom
mvanhorn:osc/128-icons-strip-xml-preamble

Conversation

@mvanhorn
Copy link

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 icons produces 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. The sanitizeSvg() function in @devvit/shared-types checks if (!svg.trim().startsWith('<svg')) and returns undefined for 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 <svg tag 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 <svg at position 0.

Fixes #128

This contribution was developed with AI assistance (Claude Code + Codex CLI).

Fixes reddit#128

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would you mind extracting this to a standalone function and adding a few unit tests?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

devvit create icons doesn't strip xml preamble

2 participants