helmet added, box real width/height
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
const express = require('express');
|
||||
const axios = require('axios');
|
||||
|
||||
const helmet = require('helmet');
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3000;
|
||||
|
||||
app.use(helmet({
|
||||
crossOriginResourcePolicy: false,
|
||||
}));
|
||||
|
||||
app.get('/badge', async (req, res) => {
|
||||
const {
|
||||
icon = 'simpleicons',
|
||||
@@ -17,7 +21,7 @@ app.get('/badge', async (req, res) => {
|
||||
} = req.query;
|
||||
|
||||
try {
|
||||
// Ikon letöltése
|
||||
// download icon SVG from SimpleIcons CDN
|
||||
const iconUrl = `https://cdn.simpleicons.org/${icon}?viewbox=auto&size=${size}`;
|
||||
const iconResponse = await axios.get(iconUrl, { responseType: 'text' });
|
||||
if (iconResponse.status !== 200) {
|
||||
@@ -25,7 +29,7 @@ app.get('/badge', async (req, res) => {
|
||||
}
|
||||
const iconSVG = iconResponse.data;
|
||||
|
||||
// Ha nincs label paraméter, akkor az ikon SVG <title> mezőjét használjuk
|
||||
// if there is no label query, use the title from the SVG
|
||||
let effectiveLabel = label;
|
||||
if (!req.query.label || req.query.label === 'Simple Icons') {
|
||||
const titleMatch = iconSVG.match(/<title>([^<]+)<\/title>/i);
|
||||
@@ -34,11 +38,11 @@ app.get('/badge', async (req, res) => {
|
||||
}
|
||||
}
|
||||
|
||||
// Badge méretek
|
||||
// Badge size
|
||||
const padding = 10;
|
||||
const iconSize = parseInt(size);
|
||||
const fontSize = Math.round(iconSize * 0.6); // Font size az ikon méret 60%-a
|
||||
const textPadding = Math.round(iconSize * 0.4); // Dinamikus: size 40%-a
|
||||
const fontSize = Math.round(iconSize * 0.6);
|
||||
const textPadding = Math.round(iconSize * 0.4);
|
||||
const radius = 8;
|
||||
const approxCharWidth = fontSize * 0.6;
|
||||
const textWidth = effectiveLabel.length * approxCharWidth;
|
||||
@@ -58,26 +62,17 @@ app.get('/badge', async (req, res) => {
|
||||
// Ha a color paraméter nincs megadva, próbáljuk kinyerni az ikon színét
|
||||
let effectiveColor = color;
|
||||
if (!req.query.color || req.query.color === '#000000') {
|
||||
// Próbáljuk kinyerni az ikon fő színét a SimpleIcons API-ból
|
||||
try {
|
||||
const iconMetaUrl = `https://cdn.simpleicons.org/${icon}`;
|
||||
const iconMetaResp = await axios.get(iconMetaUrl, { responseType: 'text' });
|
||||
// Kikeressük az első "fill=" attribútumot
|
||||
const match = iconMetaResp.data.match(/fill="([^"]+)"/);
|
||||
if (match && match[1]) {
|
||||
effectiveColor = match[1];
|
||||
} else {
|
||||
effectiveColor = "#222"; // fallback
|
||||
}
|
||||
} catch {
|
||||
effectiveColor = "#222";
|
||||
const match = iconSVG.match(/fill="([^"]+)"/);
|
||||
if (match && match[1]) {
|
||||
effectiveColor = match[1];
|
||||
} else {
|
||||
effectiveColor = "#222"; // fallback
|
||||
}
|
||||
}
|
||||
|
||||
// ÚJ: doboz magasság 5 pixellel nagyobb legyen az ikon méreténél
|
||||
const boxHeight = iconSize + 5;
|
||||
const labelBoxHeight = boxHeight;
|
||||
const totalHeight = Math.max(boxHeight, fontSize + padding * 2);
|
||||
// doboz magasság 5 pixellel nagyobb legyen az ikon méreténél
|
||||
const iconSizeAndPadding = iconSize + 5;
|
||||
const labelRectHeight = fontSize + padding;
|
||||
|
||||
let width, height, iconGroup, textElem;
|
||||
|
||||
@@ -94,15 +89,15 @@ app.get('/badge', async (req, res) => {
|
||||
`;
|
||||
} else if (labelpos === 'above' || labelpos === 'below') {
|
||||
// Felső vagy alsó label: ikon doboz magassága = iconSize + 5, szélessége = szöveg doboz szélessége
|
||||
width = Math.max(iconSize + 5, textWidth);
|
||||
const labelRectHeight = fontSize + padding;
|
||||
width = Math.max(iiconSizeAndPadding, textWidth);
|
||||
|
||||
const iconRectY = labelpos === 'above'
|
||||
? labelRectHeight
|
||||
: 0;
|
||||
const labelRectY = labelpos === 'above'
|
||||
? 0
|
||||
: iconSize + 5;
|
||||
height = iconSize + 5 + textPadding + labelRectHeight;
|
||||
: iiconSizeAndPadding;
|
||||
height = iconSizeAndPadding + textPadding + labelRectHeight;
|
||||
|
||||
// Label háttér
|
||||
const labelRect = `<rect x="0" y="${labelRectY}" width="${width}" height="${labelRectHeight}" rx="${radius}" fill="${bglabel}"/>`;
|
||||
@@ -135,7 +130,7 @@ app.get('/badge', async (req, res) => {
|
||||
|
||||
// SVG badge string összefűzéssel, szöveg árnyékkal
|
||||
const svg = `
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="${width}" height="${height}" style="background:none">
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="${(labelpos === 'left' || labelpos === 'right') ? iconBoxWidth + textWidth : width}" height="${(labelpos === 'left' || labelpos === 'right') ? iconBoxHeight : labelRectHeight+iconSize + 5}" style="background:none">
|
||||
<defs>
|
||||
<filter id="shadow" x="-20%" y="-20%" width="140%" height="140%">
|
||||
<feDropShadow dx="0" dy="1" stdDeviation="1" flood-color="#000" flood-opacity="0.25"/>
|
||||
|
||||
Reference in New Issue
Block a user