Skip to content

Commit 2ebf7fa

Browse files
author
Krystle Salazar
committed
Add font family dropdown list
Fixes #11.
1 parent 1e6fcee commit 2ebf7fa

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

api/_lib/parser.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ export function parseRequest(req: IncomingMessage) {
77
const { pathname, query } = parse(req.url || '/', true)
88
// @todo: Switch to param stuctucture here: https://github.com/spinks/og-image/commit/45439b73b3a5156faf9f6757fc4f208b230f5d9a
99
// to bypass facebook issue (facebook strips query params that appear to be urls)
10-
const { fontSize, images, widths, heights, theme, md } = query || {}
10+
const { fontFamily, fontSize, images, widths, heights, theme, md } = query || {}
1111

12+
if (Array.isArray(fontFamily)) {
13+
throw new Error('Expected a single fontFamily')
14+
}
1215
if (Array.isArray(fontSize)) {
1316
throw new Error('Expected a single fontSize')
1417
}
@@ -33,6 +36,7 @@ export function parseRequest(req: IncomingMessage) {
3336
text: decodeURIComponent(text),
3437
theme: theme === 'dark' ? 'dark' : 'light',
3538
md: md === '1' || md === 'true',
39+
fontFamily: fontFamily === 'roboto-condensed' ? 'Roboto Condensed' : 'Source Sans Pro',
3640
fontSize: fontSize || '96px',
3741
images: getArray(images),
3842
widths: getArray(widths),

api/_lib/template.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const sourceBold = readFileSync(`${__dirname}/../_fonts/SourceSansPro-Bold.woff2
1111
const robotoRglr = readFileSync(`${__dirname}/../_fonts/RobotoCondensed-Regular.woff2`).toString('base64');
1212
const robotoBold = readFileSync(`${__dirname}/../_fonts/RobotoCondensed-Bold.woff2`).toString('base64');
1313

14-
function getCss(theme: string, fontSize: string) {
14+
function getCss(theme: string, fontFamily: string, fontSize: string) {
1515
let background = 'white'
1616
let foreground = 'black'
1717
let radial = 'lightgray'
@@ -107,7 +107,7 @@ function getCss(theme: string, fontSize: string) {
107107
}
108108
109109
.heading {
110-
font-family: 'Inter', sans-serif;
110+
font-family: '${sanitizeHtml(fontFamily)}', sans-serif;
111111
font-size: ${sanitizeHtml(fontSize)};
112112
font-style: normal;
113113
color: ${foreground};
@@ -116,14 +116,14 @@ function getCss(theme: string, fontSize: string) {
116116
}
117117

118118
export function getHtml(parsedReq: ParsedRequest) {
119-
const { text, theme, md, fontSize, images, widths, heights } = parsedReq
119+
const { text, theme, md, fontFamily, fontSize, images, widths, heights } = parsedReq
120120
return `<!DOCTYPE html>
121121
<html>
122122
<meta charset="utf-8">
123123
<title>Generated Image</title>
124124
<meta name="viewport" content="width=device-width, initial-scale=1">
125125
<style>
126-
${getCss(theme, fontSize)}
126+
${getCss(theme, fontFamily, fontSize)}
127127
</style>
128128
<body>
129129
<div>

api/_lib/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface ParsedRequest {
66
text: string;
77
theme: Theme;
88
md: boolean;
9+
fontFamily: string;
910
fontSize: string;
1011
images: string[];
1112
widths: string[];

web/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ const fileTypeOptions: DropdownOption[] = [
159159
{ text: 'JPEG', value: 'jpeg' },
160160
]
161161

162+
const fontFamilyOptions: DropdownOption[] = [
163+
{ text: 'Source Sans Pro', value: 'source-sans-pro'},
164+
{ text: 'Roboto Condensed', value: 'roboto-condensed'},
165+
]
166+
162167
const fontSizeOptions: DropdownOption[] = Array.from({ length: 10 })
163168
.map((_, i) => i * 25)
164169
.filter((n) => n > 0)
@@ -308,6 +313,7 @@ const App = (_: any, state: AppState, setState: SetState) => {
308313

309314
const {
310315
fileType = 'png',
316+
fontFamily = 'source-sans-pro',
311317
fontSize = '100px',
312318
theme = 'light',
313319
md = true,
@@ -328,6 +334,7 @@ const App = (_: any, state: AppState, setState: SetState) => {
328334
url.pathname = `${encodeURIComponent(text)}.${fileType}`
329335
url.searchParams.append('theme', theme)
330336
url.searchParams.append('md', mdValue)
337+
url.searchParams.append('fontFamily', fontFamily)
331338
url.searchParams.append('fontSize', fontSize)
332339

333340
for (let image of images) {
@@ -369,6 +376,14 @@ const App = (_: any, state: AppState, setState: SetState) => {
369376
onchange: (val: FileType) => setLoadingState({ fileType: val }),
370377
}),
371378
}),
379+
H(Field, {
380+
label: 'Font Family',
381+
input: H(Dropdown, {
382+
options: fontFamilyOptions,
383+
value: fontFamily,
384+
onchange: (val: string) => setLoadingState({ fontFamily: val }),
385+
}),
386+
}),
372387
H(Field, {
373388
label: 'Font Size',
374389
input: H(Dropdown, {

0 commit comments

Comments
 (0)