Skip to content

Commit

Permalink
feat: return shorter data url if possible (#326)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <[email protected]>
  • Loading branch information
hyrious and antfu committed Aug 19, 2024
1 parent 84c29f2 commit ed9d3c1
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/utils/icons.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { buildIcon, loadIcon } from 'iconify-icon'
import { encodeSvgForCss } from '@iconify/utils'
import { getTransformedId } from '../store'
import Base64 from './base64'
import { HtmlToJSX } from './htmlToJsx'
Expand Down Expand Up @@ -211,6 +212,13 @@ export function ${name}(props) {
return prettierCode(code, 'babel-ts')
}

export function SvgToDataURL(svg: string) {
const base64 = `data:image/svg+xml;base64,${Base64.encode(svg)}`
const plain = `data:image/svg+xml,${encodeSvgForCss(svg)}`
// Return the shorter of the two data URLs
return base64.length < plain.length ? base64 : plain
}

export async function getIconSnippet(icon: string, type: string, snippet = true, color = 'currentColor'): Promise<string | undefined> {
if (!icon)
return
Expand All @@ -235,7 +243,7 @@ export async function getIconSnippet(icon: string, type: string, snippet = true,
case 'svg-symbol':
return await getSvgSymbol(icon, '32', color)
case 'data_url':
return `data:image/svg+xml;base64,${Base64.encode(await getSvg(icon, undefined, color))}`
return SvgToDataURL(await getSvg(icon, undefined, color))
case 'pure-jsx':
return ClearSvg(await getSvg(icon, undefined, color))
case 'jsx':
Expand Down

0 comments on commit ed9d3c1

Please sign in to comment.