Skip to content

Commit 44513fe

Browse files
feat: include license information in SVG output (#348)
1 parent af087a4 commit 44513fe

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/utils/icons.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { BuiltInParserName as PrettierParser } from 'prettier'
22
import { encodeSvgForCss } from '@iconify/utils'
33
import { buildIcon, loadIcon } from 'iconify-icon'
4-
import { getTransformedId } from '../store'
4+
import { getTransformedId, useCurrentCollection } from '../store'
55
import Base64 from './base64'
66
import { HtmlToJSX } from './htmlToJsx'
77
import { prettierCode } from './prettier'
@@ -42,15 +42,19 @@ export const SnippetMap: Record<string, Record<string, Snippet>> = {
4242

4343
const API_ENTRY = 'https://api.iconify.design'
4444

45+
const collectionDetails = useCurrentCollection()
46+
4547
export async function getSvgLocal(icon: string, size = '1em', color = 'currentColor') {
48+
const currentUsedCollection = collectionDetails.value
4649
const data = await loadIcon(icon)
4750
if (!data)
4851
return
4952
const built = buildIcon(data, { height: size })
5053
if (!built)
5154
return
55+
const license = `<!-- Icon from ${currentUsedCollection?.name} by ${currentUsedCollection?.author?.name} - ${currentUsedCollection?.license?.url} -->`
5256
const xlink = built.body.includes('xlink:') ? ' xmlns:xlink="http://www.w3.org/1999/xlink"' : ''
53-
return `<svg xmlns="http://www.w3.org/2000/svg"${xlink} ${Object.entries(built.attributes).map(([k, v]) => `${k}="${v}"`).join(' ')}>${built.body}</svg>`.replaceAll('currentColor', color)
57+
return `<svg xmlns="http://www.w3.org/2000/svg"${xlink} ${Object.entries(built.attributes).map(([k, v]) => `${k}="${v}"`).join(' ')}>${license}${built.body}</svg>`.replaceAll('currentColor', color)
5458
}
5559

5660
export async function getSvg(icon: string, size = '1em', color = 'currentColor') {
@@ -182,7 +186,7 @@ export function SvgToReactNative(svg: string, name: string, snippet: boolean) {
182186
}
183187

184188
function generateImports(usedComponents: string[]): string {
185-
// Separate Svg from the other components
189+
// Separate Svg from the other components
186190
const svgIndex = usedComponents.indexOf('Svg')
187191
if (svgIndex !== -1)
188192
usedComponents.splice(svgIndex, 1)

0 commit comments

Comments
 (0)