devvit icon indicating copy to clipboard operation
devvit copied to clipboard

devvit create icons doesn't strip xml preamble

Open PitchforkAssistant opened this issue 1 year ago • 0 comments

devvit create icons is supposed to convert svg files in the assets folder into usable image strings. That process results in an invalid svg if the file contains anything ahead of the <svg> tag.

The average svgfile will look something like this:

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" >
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" viewBox="0 0 300 300">
    <rect x="0" y="0" width="300" height="300" fill="red" />
</svg>

or this:

<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 300 300">
    <rect x="0" y="0" width="300" height="300" fill="red" />
</svg>

In both examples, the data before the actual svg is retained in the output, resulting in something like this:

import {svg} from "@devvit/public-api";

export const Icons = {
  "exampleOne.svg": svg`<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" ><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" viewBox="0 0 300 300">    <rect x="0" y="0" width="300" height="300" fill="red" /></svg>`,
  "exampleTwo.svg": svg`<?xml version="1.0" encoding="UTF-8"?><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 300 300">    <rect x="0" y="0" width="300" height="300" fill="red" /></svg>`,
} as const;

That results in the svg helper function logging "The provided string is not a valid SVG." and returning an empty string. Either the devvit create icons command or svg function needs to account for this, otherwise there is a lot of manual work required each time you regenerate the icons.

PitchforkAssistant avatar Dec 14 '24 00:12 PitchforkAssistant