Skip to content

Commit 8f99dcb

Browse files
committed
backend: add a new titleAlign query parameter
1 parent 6ec08f7 commit 8f99dcb

File tree

6 files changed

+48
-9
lines changed

6 files changed

+48
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ None of the fields are required. Every query parameter has a default value displ
6363
| **title** | `?title=My%20Title` | My Tech Stack | The title of the card. `%20` can be used as a space. |
6464
| **theme** | `?theme=github_dark` | github | The theme of the card. You can browse between the themes [here](docs/THEMES.md). |
6565
| **align** | `?align=center` | left | The alignment of the badges. (`left`, `center`, `right`) |
66+
| **titleAlign** | `?align=center` | left | The alignment of the title. (`left`, `center`, `right`) |
6667
| **showBorder** | `?showBorder=false` | true | Display the border around the card or not. (`true`, `false`) |
6768
| **hideTitle** | `?hideTitle=true` | false | Display the title of the card or not. (`true`, `false`) |
6869
| **hideBg** | `?hideBg=true` | false | If true, sets the background to transparent. (`true`, `false`) |

src/cards/card-builder.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ export default class CardBuilder {
5858
return this;
5959
}
6060

61+
public titleAlign(_titleAlign = "left"): CardBuilder {
62+
this.card.setTitleAlign(align.parse(_titleAlign));
63+
return this;
64+
}
65+
6166
public border(_showBorder = "true"): CardBuilder {
6267
this.card.setShowBorder(
6368
showBorder.parse(_showBorder === "false" ? false : true)

src/cards/card.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { getThemeByName } from "./themes";
2-
import { Badge, BadgeAlign, FontWeight, Theme } from "./types";
2+
import { Badge, Align, FontWeight, Theme } from "./types";
33

44
export default class Card {
55
private title: string;
66
private lineCount: number;
77
private theme: Theme;
8-
private badgeAlign: BadgeAlign;
8+
private badgeAlign: Align;
9+
private titleAlign: Align;
910
private showBorder: boolean;
1011
private borderRadius: number;
1112
private fontWeight: FontWeight;
@@ -21,6 +22,7 @@ export default class Card {
2122
this.title = "My Tech Stack";
2223
this.theme = getThemeByName("github");
2324
this.badgeAlign = "left";
25+
this.titleAlign = "left";
2426
this.showBorder = true;
2527
this.borderRadius = 4.5;
2628
this.fontWeight = FontWeight.SEMIBOLD;
@@ -71,12 +73,18 @@ export default class Card {
7173
this.lines.set(lineCount, lineValue);
7274
};
7375

74-
public getBadgeAlign = (): BadgeAlign => this.badgeAlign;
76+
public getBadgeAlign = (): Align => this.badgeAlign;
7577

76-
public setBadgeAlign = (badgeAlign: BadgeAlign): void => {
78+
public setBadgeAlign = (badgeAlign: Align): void => {
7779
this.badgeAlign = badgeAlign;
7880
};
7981

82+
public getTitleAlign = (): Align => this.titleAlign;
83+
84+
public setTitleAlign = (titleAlign: Align): void => {
85+
this.titleAlign = titleAlign;
86+
};
87+
8088
public getShowBorder = (): boolean => this.showBorder;
8189

8290
public setShowBorder = (showBorder: boolean): void => {

src/cards/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ export interface Badge {
1111
logoColor: string;
1212
}
1313

14-
export type BadgeAlign = "left" | "center" | "right";
14+
export type Align = "left" | "center" | "right";
15+
export type TextAnchor = "start" | "middle" | "end";
1516

1617
export const enum FontWeight {
1718
THIN = 200,

src/controllers/cards-controller.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ export const getCard = async (req: Request, res: Response) => {
2323
titleColor,
2424
hideBg,
2525
hideTitle,
26+
titleAlign,
2627
} = req.query;
2728

2829
const card = new CardBuilder()
2930
.title(title?.toString())
3031
.lineCount(lineCount?.toString())
3132
.align(align?.toString())
33+
.titleAlign(titleAlign?.toString())
3234
.border(showBorder?.toString())
3335
.hideTitle(hideTitle?.toString())
3436
.borderRadius(borderRadius?.toString())

src/svg/svg-generator.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Card from "../cards/card";
2-
import { Badge } from "../cards/types";
2+
import { Badge, TextAnchor } from "../cards/types";
33
import { badgeWidth } from "../utils/badge-width";
44
import { fetchBadge } from "../utils/fetch-badge";
55
import { formatHexColor } from "../utils/hex-color";
@@ -34,6 +34,24 @@ export default class SvgGenerator {
3434
* @returns {string} The generated raw SVG.
3535
*/
3636
public toString = async (): Promise<string> => {
37+
// default values: left title align
38+
let textTranslateX = 25;
39+
let textXPercent = 0;
40+
let textAnchor: TextAnchor = "start";
41+
42+
switch (this.card.getTitleAlign()) {
43+
case "center":
44+
textTranslateX = 0;
45+
textXPercent = 50;
46+
textAnchor = "middle";
47+
break;
48+
case "right":
49+
textTranslateX = -25;
50+
textXPercent = 100;
51+
textAnchor = "end";
52+
break;
53+
}
54+
3755
return `
3856
<svg
3957
width="${this.width}"
@@ -60,9 +78,13 @@ export default class SvgGenerator {
6078
${
6179
this.card.getHideTitle()
6280
? ""
63-
: `<g transform="translate(25, ${17 + this.card.getFontSize()})">
64-
<text x="0" y="0" class="header">${this.card.getTitle()}</text>
65-
</g>`
81+
: `<g transform="translate(${textTranslateX}, ${
82+
17 + this.card.getFontSize()
83+
})">
84+
<text x="${textXPercent}%" y="0" text-anchor="${textAnchor}" class="header">
85+
${this.card.getTitle()}
86+
</text>
87+
</g>`
6688
}
6789
6890
${await this.generateLines()}

0 commit comments

Comments
 (0)