Skip to content

Commit 418779e

Browse files
committed
backend: add width query param
1 parent 770dfcf commit 418779e

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ The easiest way to start using these cards is to visit our website, where you ca
4646
<hr>
4747

4848
## 🔧 Query parameters
49-
None of the fields are required. Every query parameter has a default value displayed below.
49+
None of the fields are required. Every query parameter has a default value displayed below.
5050

51-
| Parameter | Example | Default value | Description|
52-
|---------- |---------|---------------|------------|
51+
> **Note**
52+
> Not every query parameter is changeable in the Demo Website.
53+
54+
| Parameter | Example | Default value | Description |
55+
|---------- |---------|---------------|------------ |
5356
| **title** | `?title=My%20Title` | My Tech Stack | The title of the card. `%20` can be used as a space. |
5457
| **theme** | `?theme=github_dark` | github | The theme of the card. You can browse between the themes [here](docs/THEMES.md). |
5558
| **align** | `?align=center` | left | The alignment of the badges. (`left`, `center`, `right`) |
@@ -58,6 +61,7 @@ None of the fields are required. Every query parameter has a default value displ
5861
| **fontFamily** | `?fontFamily=consolas` | Segoe UI | The font family of the title. If the specified family doesn't exist, the default is used. |
5962
| **fontSize** | `?fontSize=20` | 18 | The size of the title. Accepts a number between 15 and 30. |
6063
| **fontWeight** | `?fontWeight=normal` | semibold | The thickness of the title. (`thin`, `normal`, `semibold`, `bold`) |
64+
| **width** | `?width=500` | 495 | The width of the card. Accepts a valid number. |
6165
| **gap** | `?gap=15` | 10 | The gap between the badges. Accepts a number between 0 and 30. |
6266
| **lineHeight** | `?lineHeight=10` | 7 | The gap between the lines. Accepts a number between 0 and 30. |
6367
| **lineCount** | `?lineCount=2` | 1 | The number of lines you want to add to your card. |

src/controllers/cards-controller.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Request, Response } from "express";
22
import CardBuilder from "../cards/card-builder";
33
import SvgGenerator from "../svg/svg-generator";
4-
import { validateLine } from "../utils/validator";
4+
import { parseWidth, validateLine } from "../utils/validator";
55

66
export const getCard = async (req: Request, res: Response) => {
77
const {
@@ -16,6 +16,7 @@ export const getCard = async (req: Request, res: Response) => {
1616
theme,
1717
gap,
1818
lineHeight,
19+
width,
1920
} = req.query;
2021

2122
const card = new CardBuilder()
@@ -41,5 +42,7 @@ export const getCard = async (req: Request, res: Response) => {
4142
.build();
4243

4344
res.setHeader("Content-Type", "image/svg+xml");
44-
res.send(await new SvgGenerator(card).toString());
45+
res.send(
46+
await new SvgGenerator(card, parseWidth(width?.toString())).toString()
47+
);
4548
};

src/svg/svg-generator.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ export default class SvgGenerator {
1010
private card: Card;
1111
private lineHeight: number;
1212

13-
public constructor(card: Card) {
13+
public constructor(card: Card, width = 495) {
1414
this.card = card;
15-
this.width = 495;
15+
this.width = width;
1616

1717
// the height of a badge is 28
1818
this.lineHeight = this.card.getLineHeight() + 28;
@@ -97,19 +97,19 @@ export default class SvgGenerator {
9797
}
9898

9999
// align: left ==> x = 25
100-
// align: center ==> x = 495/2 - leftPadding/2 + gap/2
101-
// align: right ==> x = 495 - 25 - leftPadding + gap
100+
// align: center ==> x = width/2 - leftPadding/2 + gap/2
101+
// align: right ==> x = width - 25 - leftPadding + gap
102102
let x = 25;
103103

104104
switch (this.card.getBadgeAlign()) {
105105
case "left":
106106
x = 25;
107107
break;
108108
case "center":
109-
x = 495 / 2 - leftPadding / 2 + this.card.getGap() / 2;
109+
x = this.width / 2 - leftPadding / 2 + this.card.getGap() / 2;
110110
break;
111111
case "right":
112-
x = 495 - 25 - leftPadding + this.card.getGap();
112+
x = this.width - 25 - leftPadding + this.card.getGap();
113113
break;
114114
}
115115

src/utils/validator.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
import { Badge } from "../cards/types";
22
import { formatHexColor } from "./hex-color";
33

4+
/**
5+
* Parses the width into a valid number.
6+
*
7+
* @param {string | undefined} width The width.
8+
* @returns {number} The parsed width.
9+
*/
10+
export const parseWidth = (width = "495"): number => {
11+
if (width === "495") {
12+
return 495;
13+
}
14+
15+
const num = Number(width);
16+
17+
if (isNaN(num)) {
18+
return 495;
19+
}
20+
21+
return num;
22+
};
23+
424
/**
525
* Converts the line into a Badge array.
626
* If there's any error in the line,

0 commit comments

Comments
 (0)