Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: return shorter data url if possible #326

Merged
merged 2 commits into from
Aug 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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