astro
Version:
Astro is a modern site builder with web best practices, performance, and DX front-of-mind.
64 lines (63 loc) • 2.11 kB
JavaScript
import {
createComponent,
render,
spreadAttributes,
unescapeHTML
} from "../runtime/server/index.js";
const countersByPage = /* @__PURE__ */ new WeakMap();
function createSvgComponent({ meta, attributes, children }) {
const renderedIds = /* @__PURE__ */ new WeakMap();
const Component = createComponent((result, props) => {
let counter = countersByPage.get(result) ?? 0;
const {
title: titleProp,
viewBox,
mode,
...normalizedProps
} = normalizeProps(attributes, props);
const title = titleProp ? unescapeHTML(`<title>${titleProp}</title>`) : "";
if (mode === "sprite") {
let symbol = "";
let id = renderedIds.get(result);
if (!id) {
countersByPage.set(result, ++counter);
id = `a:${counter}`;
symbol = unescapeHTML(`<symbol${spreadAttributes({ viewBox, id })}>${children}</symbol>`);
renderedIds.set(result, id);
}
return render`<svg${spreadAttributes(normalizedProps)}>${title}${symbol}<use href="#${id}" /></svg>`;
}
return render`<svg${spreadAttributes({ viewBox, ...normalizedProps })}>${title}${unescapeHTML(children)}</svg>`;
});
if (import.meta.env.DEV) {
makeNonEnumerable(Component);
Object.defineProperty(Component, Symbol.for("nodejs.util.inspect.custom"), {
value: (_, opts, inspect) => inspect(meta, opts)
});
}
return Object.assign(Component, meta);
}
const ATTRS_TO_DROP = ["xmlns", "xmlns:xlink", "version"];
const DEFAULT_ATTRS = { role: "img" };
function dropAttributes(attributes) {
for (const attr of ATTRS_TO_DROP) {
delete attributes[attr];
}
return attributes;
}
function normalizeProps(attributes, { size, ...props }) {
if (size !== void 0 && props.width === void 0 && props.height === void 0) {
props.height = size;
props.width = size;
}
return dropAttributes({ ...DEFAULT_ATTRS, ...attributes, ...props });
}
function makeNonEnumerable(object) {
for (const property in object) {
Object.defineProperty(object, property, { enumerable: false });
}
}
export {
createSvgComponent,
dropAttributes
};