diff --git a/astro.config.mjs b/astro.config.mjs index ddff644..9f4acc0 100644 --- a/astro.config.mjs +++ b/astro.config.mjs @@ -6,6 +6,14 @@ import { defineConfig } from "astro/config"; // https://astro.build/config export default defineConfig({ site: "https://svgforge.github.io", + image: { + layout: "constrained", + responsiveStyles: true, + breakpoints: [480, 640, 768], + service: { + entrypoint: "./src/lib/avif-image-service.ts", + }, + }, integrations: [sitemap()], markdown: { shikiConfig: { diff --git a/package.json b/package.json index 1f1f1fb..0e918a4 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,8 @@ }, "dependencies": { "@astrojs/sitemap": "^3.7.4", - "astro": "^7.3.2" + "astro": "^7.3.2", + "sharp": "^0.35.4" }, "allowScripts": { "esbuild": true diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7f3e0e4..f8d26e6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14,6 +14,9 @@ importers: astro: specifier: ^7.3.2 version: 7.3.2(@emnapi/core@1.11.1)(@emnapi/runtime@1.11.3)(@types/node@24.13.4)(yaml@2.9.0) + sharp: + specifier: ^0.35.4 + version: 0.35.4(@types/node@24.13.4) devDependencies: '@astrojs/check': specifier: ^0.9.10 @@ -2146,8 +2149,7 @@ snapshots: '@esbuild/win32-x64@0.28.2': optional: true - '@img/colour@1.1.0': - optional: true + '@img/colour@1.1.0': {} '@img/sharp-darwin-arm64@0.35.4': optionalDependencies: @@ -3098,7 +3100,6 @@ snapshots: '@img/sharp-win32-ia32': 0.35.4 '@img/sharp-win32-x64': 0.35.4 '@types/node': 24.13.4 - optional: true shiki@4.4.3: dependencies: diff --git a/src/content/blog/custom-icon-sets/example-preview.png b/src/content/blog/custom-icon-sets/example-preview.png new file mode 100644 index 0000000..4b3b36f Binary files /dev/null and b/src/content/blog/custom-icon-sets/example-preview.png differ diff --git a/src/content/blog/custom-icon-sets/icon-modal.png b/src/content/blog/custom-icon-sets/icon-modal.png new file mode 100644 index 0000000..d1b2a6c Binary files /dev/null and b/src/content/blog/custom-icon-sets/icon-modal.png differ diff --git a/src/content/blog/custom-icon-sets/index.md b/src/content/blog/custom-icon-sets/index.md new file mode 100644 index 0000000..1c5fdb5 --- /dev/null +++ b/src/content/blog/custom-icon-sets/index.md @@ -0,0 +1,149 @@ +--- +title: Create Your Own Icon Sets for the SVG Forge Icon Manager +pubDate: 2026-09-15 +tags: + - tips + - svg +--- + +The **SVG Forge Icon Manager** is a Gutenberg plugin for WordPress: an "SVG Icon" block inserts icons via `` from a central SVG sprite file into your content. The plugin is deliberately *not* an icon collection with an import UI — every icon must exist as a `` element in a sprite file that you build, maintain, and version yourself. + +This article shows you how to create your own icon set: create a folder full of SVG files, build them with [svgforge-cli](https://github.com/svgforge/svgforge-cli/) into a sprite file in one line, and hook it into the plugin. Including the **directory-marker trick**, which turns your subfolders into filter groups in the icon picker automatically. + +As a reference, we use the example repo [svgforge/default-icons](https://github.com/svgforge/default-icons): it contains two ready-made icon sets with a build configuration and shows the structure your own set should have. + +## How an Icon Set Is Structured + +In its simplest form, an icon set is a folder with SVG files; subfolders form categories: + +```text +assets/ +├── actions/ +│ ├── add_circle.svg +│ └── delete.svg +└── navigation/ + ├── arrow_back.svg + └── home.svg +``` + +`svgforge-cli` converts each SVG file into a `` inside a central `sprite.svg`. The sprite structure is simple — exactly what the plugin expects: + +```xml + + + + + + + + +``` + +The editor loads this sprite, shows each `` as a clickable icon, and later references the selected icon as ``. The `--` notation in the IDs is the link to the picker: the first part (`actions`) becomes a filter group there. + +## Step 1: Install svgforge-cli + +```bash +npm install --global @svgforge/svgforge-cli +``` + +`svgforge-cli` sanitizes the SVGs (removes scripts, event handlers, `javascript:` links) and optimizes them with SVGO — the very sprite that the plugin additionally re-checks on upload as a safety net. + +## Step 2: Create Your Icon Folder + +Create a folder with your SVG files and sort them into subfolders as you like. The folder name later becomes the group name in the picker — so choose a descriptive name: + +```text +assets/ +├── actions/ +│ └── add_circle.svg +└── communication/ + └── mail.svg +``` + +## Step 3: The Directory-Marker Trick + +The build command looks like this: + +```bash +svgforge --symbol --dest=out 'assets/./**/*.svg' +``` + +The trick is the `./` right in the middle of the glob: it is the **directory marker**. Everything before it is only the base from which the icon ID is calculated. Without the marker, the base folder name would bleed into every ID: + +| Glob | Icon ID | Group | +|---|---|---| +| `assets/**/*.svg` | `assets--actions--add_circle` | `assets` (single, useless group) | +| `assets/./**/*.svg` | `actions--add_circle` | `actions` | + +With `./`, you get compact IDs like `actions--add_circle` from `assets/actions/add_circle.svg`. The base name `assets` disappears, and each subfolder level becomes a `--` segment — the picker turns it into the group filter. Without the marker, all icons would share the identical `assets--` prefix and the filter would be useless. + +## Step 4: Generate the Sprite + +```bash +svgforge --symbol --dest=out 'assets/./**/*.svg' +``` + +After the run, the finished sprite lives at `out/symbol/sprite.svg`. If you prefer working with configuration files instead (for example, to remove the `fill` attribute), take a look at the `material.json` in the [default-icons](https://github.com/svgforge/default-icons) repository: + +```json +{ + "dest": "./dist/material", + "shape": { + "transform": [ + { + "svgo": { + "plugins": [ + { "name": "removeAttrs", "params": { "attrs": "fill" } } + ] + } + } + ] + }, + "mode": { + "symbol": { "sprite": "sprite.svg", "render": { "css": false } } + } +} +``` + +This lets you run set-wide transformations — in the example, the `fill` attribute of all Material icons is removed so they stay monochrome and can be colored via the block's color controller. + +![Screenshot — generated sprite.svg + HTML preview](./example-preview.png) + +## Step 5: Hand the Sprite to the Plugin + +There are two sensible ways. The **filter** is recommended because the file then lives in the theme repo and is versioned along with it (if you use Git) — in `functions.php`: + +```php +add_filter( 'sfim_sprite_url', fn () => get_theme_file_uri( 'assets/ico.svg' ) ); +``` + +The filter wins against everything else, including an upload. Alternatively, you can upload the file via **Settings → SVG Forge Icon Manager** in the backend (the plugin settings page calls it "SVG fragment library"). The plugin detects the `` elements immediately. + +![Plugin settings page (upload + active sprite file)](./sprite-upload.png) + +## Step 6: Use Icons in the Editor + +Add the **SVG Icon** block, click "Replace", and pick an icon from the picker. The picker shows all `` elements with a live preview, offers a group filter (if subdirectories were used) and a grid/list view. You configure color (Fill/Stroke), size (standard dimensions panel with preset slider), and links per block. + +![SVG Icon block in the editor with a selected icon and settings](./icon-modal.png) + +## Reference: svgforge/default-icons + +The [svgforge/default-icons](https://github.com/svgforge/default-icons) repo is a ready-made example for exactly this pipeline: + +- **`material/`** — 34 monochrome Material icons (Apache 2.0, via Google Fonts) in six categories (`actions`, `communication`, `content`, `navigation`, `people`, `status`). The trick (removing `fill` via config) makes them colorable through `currentColor`. +- **`tango/`** — 213 multicolored Tango icons (Public Domain) in nine categories (`actions`, `apps`, `categories`, `devices`, `emblems`, `emotes`, `mimetypes`, `places`, `status`). +- **Build**: `build.sh` calls `svgforge -C material.json 'material/./**/*.svg'` (and analogously `tango.json`) and then starts a local preview server. The finished sprites live at `dist//symbol/sprite.svg`; the `example: true` flag additionally generates an HTML preview of the whole group. + +So you can also download a set directly and hook it into the plugin without building it yourself. + +## Monochrome or Multicolored? + +The plugin distinguishes automatically: multicolored sets (like Tango) keep their built-in colors — the color controller has no visible effect there. Monochrome icons (`fill: currentColor` or no fixed fill) follow the block's chosen color — that is the basis of the Material configuration above, which consistently removes `fill`. + +## Conclusion + +A custom icon set is therefore: a folder with SVG files, a build command with the `./` directory marker, and an upload (or a filter) in the plugin. The subfolders automatically become filter groups in the picker. The structure and configs from [svgforge/default-icons](https://github.com/svgforge/default-icons) serve as a starting point. + +**Tip:** If you work with a CI/CD pipeline, you can add your icons as individual SVGs to your theme. A GitHub workflow generates the `sprite.svg` during the build — the installable ZIP then contains the theme including the generated `sprite.svg`. \ No newline at end of file diff --git a/src/content/blog/custom-icon-sets/sprite-upload.png b/src/content/blog/custom-icon-sets/sprite-upload.png new file mode 100644 index 0000000..aa9694e Binary files /dev/null and b/src/content/blog/custom-icon-sets/sprite-upload.png differ diff --git a/src/layouts/SiteLayout.astro b/src/layouts/SiteLayout.astro index 532d8ed..6461453 100644 --- a/src/layouts/SiteLayout.astro +++ b/src/layouts/SiteLayout.astro @@ -275,6 +275,10 @@ const siteUrl = new URL(Astro.url.pathname, Astro.site); .prose > :last-child { margin-bottom: 0; } + .prose img { + width: 100%; + height: auto; + } .prose :is(h2, h3) { margin-top: 2.2rem; } diff --git a/src/lib/avif-image-service.ts b/src/lib/avif-image-service.ts new file mode 100644 index 0000000..fde855e --- /dev/null +++ b/src/lib/avif-image-service.ts @@ -0,0 +1,29 @@ +import type { LocalImageService } from "astro"; +import { baseService } from "astro/assets"; +import sharpService from "astro/assets/services/sharp"; + +/** + * Wraps the built-in sharp service with two project-specific defaults: + * - AVIF as the output format unless the caller explicitly set one (or the + * source is SVG, which is passed through unchanged). + * - A capped `sizes` attribute: the article container constrains images to + * roughly 672px, so there is no point telling the browser to load the full + * source width on desktop. + */ +const service: LocalImageService = { + ...sharpService, + validateOptions(options, imageConfig, logger) { + const src = options.src; + if (!options.format && typeof src !== "string" && src.format !== "svg") { + options.format = "avif"; + } + if (options.sizes) { + options.sizes = "(min-width: 768px) 768px, 100vw"; + } + return ( + baseService.validateOptions?.(options, imageConfig, logger) ?? options + ); + }, +}; + +export default service; diff --git a/src/pages/releases.astro b/src/pages/releases.astro index 851a389..0af32d6 100644 --- a/src/pages/releases.astro +++ b/src/pages/releases.astro @@ -2,22 +2,31 @@ import GitHubRelease from "../components/GitHubRelease.astro"; import SiteLayout from "../layouts/SiteLayout.astro"; -const releases = [ - { - repo: "svgforge/svgforge", - description: - "Low-level Node.js module — takes a bunch of SVG files, optimizes and bakes them into SVG sprites.", - }, - { - repo: "svgforge/svgforge-cli", - description: "Command line interface for svgforge.", - }, - { - repo: "svgforge/icon-library", - description: - "A Wordpress plugin for managing SVG icons. You can upload your generated SVG sprite and use it in Gutenberg editor.", - }, +const repos = [ + "svgforge/svgforge", + "svgforge/svgforge-cli", + "svgforge/sf-icon-manager", ]; + +async function getRepoDescription(repo: string): Promise { + try { + const res = await fetch(`https://api.github.com/repos/${repo}`, { + headers: { Accept: "application/vnd.github.v3+json" }, + }); + if (!res.ok) return ""; + const data: { description?: string | null } = await res.json(); + return data.description ?? ""; + } catch { + return ""; + } +} + +const releases = await Promise.all( + repos.map(async (repo) => ({ + repo, + description: await getRepoDescription(repo), + })), +); --- @@ -29,7 +38,7 @@ const releases = [

{name}

-

{item.description}

+ {item.description &&

{item.description}

}
);