Add fontSize querystring param

This commit is contained in:
Steven 2019-01-21 16:10:56 -05:00
parent fc9236ef63
commit 75c6bd797c
3 changed files with 12 additions and 7 deletions

View File

@ -6,8 +6,8 @@ import { writeTempFile, pathToFileURL } from './file';
async function handler(req: IncomingMessage, res: ServerResponse) {
try {
const { type, text, fontWeight, images } = parseRequest(req);
const html = getHtml(text, fontWeight, images);
const { type, text, fontWeight, fontSize, images } = parseRequest(req);
const html = getHtml(text, fontWeight, fontSize, images);
const filePath = await writeTempFile(text, html);
const fileUrl = pathToFileURL(filePath);
const file = await getScreenshot(fileUrl, type);

View File

@ -5,16 +5,20 @@ interface ParsedRequest {
type: ScreenshotType;
text: string;
fontWeight: FontWeight;
fontSize: string;
images: string[];
};
export function parseRequest(req: IncomingMessage) {
console.log('HTTP ' + req.url);
const { pathname = '/', query = {} } = parse(req.url || '', true);
const { fontWeight, images } = query;
const { fontWeight, fontSize, images } = query;
if (Array.isArray(fontWeight)) {
throw new Error('Expected a single fontWeight');
}
if (Array.isArray(fontSize)) {
throw new Error('Expected a single fontSize');
}
const arr = pathname.slice(1).split('.');
const type = arr.pop();
const text = arr.join('.');
@ -22,6 +26,7 @@ export function parseRequest(req: IncomingMessage) {
type: type as ScreenshotType,
text: decodeURIComponent(text),
fontWeight: fontWeight as FontWeight,
fontSize: fontSize || '75px',
images: Array.isArray(images) && images.length > 0
? images
: ['https://assets.zeit.co/image/upload/front/assets/design/now-black.svg'],

View File

@ -1,7 +1,7 @@
import { readFileSync } from 'fs';
function getCss(fontWeight: FontWeight) {
function getCss(fontWeight: FontWeight, fontSize: string) {
const regular = `${__dirname}/fonts/Inter-UI-Regular.woff2`;
const bold = `${__dirname}/fonts/Inter-UI-Bold.woff2`;
const buffer = readFileSync(fontWeight === 'normal' ? regular : bold);
@ -36,16 +36,16 @@ function getCss(fontWeight: FontWeight) {
.heading {
font-family: 'Inter UI', sans-serif;
font-size: 75px;
font-size: ${fontSize};
font-style: normal;
font-weight: ${fontWeight};
}`;
}
export function getHtml(text: string, fontWeight: FontWeight, images: string[]) {
export function getHtml(text: string, fontWeight: FontWeight, fontSize: string, images: string[]) {
return `<html>
<style>
${getCss(fontWeight)}
${getCss(fontWeight, fontSize)}
</style>
<body>
<div>