Set up a custom OpenGraph image generator for social media previews (#20)
* Copy in og-image code * Add "yarn start" command in lieu of vercel * Don't require that images be sourced from vercel * Load in Major Mono and Readex fonts * Fix vercel config (?) * Replace default image with Manifold's * Add some brief instructions on getting started * In the UI, use the default image * Fix typescript errors * More typescript fixing
This commit is contained in:
parent
b1b371e708
commit
4b2412c49c
1
og-image/.github/CODEOWNERS
vendored
Normal file
1
og-image/.github/CODEOWNERS
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
* @styfle
|
6
og-image/.gitignore
vendored
Normal file
6
og-image/.gitignore
vendored
Normal file
|
@ -0,0 +1,6 @@
|
|||
.now
|
||||
.vercel
|
||||
node_modules
|
||||
api/dist
|
||||
public/dist
|
||||
|
6
og-image/.vercelignore
Normal file
6
og-image/.vercelignore
Normal file
|
@ -0,0 +1,6 @@
|
|||
.github
|
||||
node_modules
|
||||
api/dist
|
||||
public/dist
|
||||
CONTRIBUTING.md
|
||||
README.md
|
1
og-image/.yarnrc
Normal file
1
og-image/.yarnrc
Normal file
|
@ -0,0 +1 @@
|
|||
save-prefix ""
|
21
og-image/CONTRIBUTING.md
Normal file
21
og-image/CONTRIBUTING.md
Normal file
|
@ -0,0 +1,21 @@
|
|||
# Contributing
|
||||
|
||||
There are two pieces to `og-image` that are worth noting before you begin development.
|
||||
|
||||
1. The backend image generator located in [/api/index.ts](https://github.com/vercel/og-image/blob/main/api/index.ts)
|
||||
2. The html/css template used to generate the image is located in [/\_lib/template.ts](https://github.com/vercel/og-image/blob/main/api/_lib/template.ts)
|
||||
3. The frontend inputs located in [/web/index.ts](https://github.com/vercel/og-image/blob/main/web/index.ts)
|
||||
|
||||
Vercel handles [routing](https://github.com/vercel/og-image/blob/main/vercel.json#L6) in an elegant way for us so deployment is easy.
|
||||
|
||||
To start hacking, do the following:
|
||||
|
||||
1. Clone this repo with `git clone https://github.com/vercel/og-image`
|
||||
2. Change directory with `cd og-image`
|
||||
3. Run `yarn` or `npm install` to install all dependencies
|
||||
4. Run locally with `vercel dev` and visit [localhost:3000](http://localhost:3000) (if nothing happens, run `npm install -g vercel`)
|
||||
5. If necessary, edit the `exePath` in [options.ts](https://github.com/vercel/og-image/blob/main/api/_lib/options.ts) to point to your local Chrome executable
|
||||
|
||||
Now you're ready to start local development!
|
||||
|
||||
You can set an environment variable to assist with debugging `export OG_HTML_DEBUG=1`. This will render the image as HTML so you can play around with your browser's dev tools before committing changes to the template.
|
21
og-image/LICENSE
Normal file
21
og-image/LICENSE
Normal file
|
@ -0,0 +1,21 @@
|
|||
MIT License
|
||||
|
||||
Copyright (c) 2019-2020 Vercel, Inc.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
81
og-image/README.md
Normal file
81
og-image/README.md
Normal file
|
@ -0,0 +1,81 @@
|
|||
# Quickstart
|
||||
|
||||
1. To get started: `yarn install`
|
||||
2. To test locally: `yarn start`
|
||||
The local image preview is broken for some reason; but the service works.
|
||||
E.g. try `http://localhost:3000/manifold.png`
|
||||
3. To deploy: push to Github
|
||||
|
||||
For more info, see Contributing.md
|
||||
|
||||
nb2: (Not `dev` because that's reserved for Vercel)
|
||||
nb3: (Or `cd .. && vercel --prod`, I think)
|
||||
|
||||
(Everything below is from the original repo)
|
||||
|
||||
# [Open Graph Image as a Service](https://og-image.vercel.app)
|
||||
|
||||
<a href="https://twitter.com/vercel">
|
||||
<img align="right" src="https://og-image.vercel.app/tweet.png" height="300" />
|
||||
</a>
|
||||
|
||||
Serverless service that generates dynamic Open Graph images that you can embed in your `<meta>` tags.
|
||||
|
||||
For each keystroke, headless chromium is used to render an HTML page and take a screenshot of the result which gets cached.
|
||||
|
||||
See the image embedded in the tweet for a real use case.
|
||||
|
||||
## What is an Open Graph Image?
|
||||
|
||||
Have you ever posted a hyperlink to Twitter, Facebook, or Slack and seen an image popup?
|
||||
How did your social network know how to "unfurl" the URL and get an image?
|
||||
The answer is in your `<head>`.
|
||||
|
||||
The [Open Graph protocol](http://ogp.me) says you can put a `<meta>` tag in the `<head>` of a webpage to define this image.
|
||||
|
||||
It looks like the following:
|
||||
|
||||
```html
|
||||
<head>
|
||||
<title>Title</title>
|
||||
<meta property="og:image" content="http://example.com/logo.jpg" />
|
||||
</head>
|
||||
```
|
||||
|
||||
## Why use this service?
|
||||
|
||||
The short answer is that it would take a long time to painstakingly design an image for every single blog post and every single documentation page. And we don't want the exact same image for every blog post because that wouldn't make the article stand out when it was shared to Twitter.
|
||||
|
||||
That's where `og-image.vercel.app` comes in. We can simply pass the title of our blog post to our generator service and it will generate the image for us on the fly!
|
||||
|
||||
It looks like the following:
|
||||
|
||||
```html
|
||||
<head>
|
||||
<title>Hello World</title>
|
||||
<meta
|
||||
property="og:image"
|
||||
content="https://og-image.vercel.app/Hello%20World.png"
|
||||
/>
|
||||
</head>
|
||||
```
|
||||
|
||||
Now try changing the text `Hello%20World` to the title of your choosing and watch the magic happen ✨
|
||||
|
||||
## Deploy your own
|
||||
|
||||
You'll want to fork this repository and deploy your own image generator.
|
||||
|
||||
1. Click the fork button at the top right of GitHub
|
||||
2. Clone the repo to your local machine with `git clone URL_OF_FORKED_REPO_HERE`
|
||||
3. Change directory with `cd og-image`
|
||||
4. Make changes by swapping out images, changing colors, etc (see [contributing](https://github.com/vercel/og-image/blob/main/CONTRIBUTING.md) for more info)
|
||||
5. Remove all configuration inside `vercel.json` besides `rewrites`
|
||||
6. Run locally with `vercel dev` and visit [localhost:3000](http://localhost:3000) (if nothing happens, run `npm install -g vercel`)
|
||||
7. Deploy to the cloud by running `vercel` and you'll get a unique URL
|
||||
8. Connect [Vercel for GitHub](https://vercel.com/github) to automatically deploy each time you `git push` 🚀
|
||||
|
||||
## Authors
|
||||
|
||||
- Steven ([@styfle](https://twitter.com/styfle)) - [Vercel](https://vercel.com)
|
||||
- Evil Rabbit ([@evilrabbit](https://twitter.com/evilrabbit_)) - [Vercel](https://vercel.com)
|
BIN
og-image/api/_fonts/Inter-Bold.woff2
Normal file
BIN
og-image/api/_fonts/Inter-Bold.woff2
Normal file
Binary file not shown.
92
og-image/api/_fonts/Inter-License.txt
Normal file
92
og-image/api/_fonts/Inter-License.txt
Normal file
|
@ -0,0 +1,92 @@
|
|||
Copyright (c) 2016-2018 The Inter Project Authors (me@rsms.me)
|
||||
|
||||
This Font Software is licensed under the SIL Open Font License, Version 1.1.
|
||||
This license is copied below, and is also available with a FAQ at:
|
||||
http://scripts.sil.org/OFL
|
||||
|
||||
-----------------------------------------------------------
|
||||
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
|
||||
-----------------------------------------------------------
|
||||
|
||||
PREAMBLE
|
||||
The goals of the Open Font License (OFL) are to stimulate worldwide
|
||||
development of collaborative font projects, to support the font creation
|
||||
efforts of academic and linguistic communities, and to provide a free and
|
||||
open framework in which fonts may be shared and improved in partnership
|
||||
with others.
|
||||
|
||||
The OFL allows the licensed fonts to be used, studied, modified and
|
||||
redistributed freely as long as they are not sold by themselves. The
|
||||
fonts, including any derivative works, can be bundled, embedded,
|
||||
redistributed and/or sold with any software provided that any reserved
|
||||
names are not used by derivative works. The fonts and derivatives,
|
||||
however, cannot be released under any other type of license. The
|
||||
requirement for fonts to remain under this license does not apply
|
||||
to any document created using the fonts or their derivatives.
|
||||
|
||||
DEFINITIONS
|
||||
"Font Software" refers to the set of files released by the Copyright
|
||||
Holder(s) under this license and clearly marked as such. This may
|
||||
include source files, build scripts and documentation.
|
||||
|
||||
"Reserved Font Name" refers to any names specified as such after the
|
||||
copyright statement(s).
|
||||
|
||||
"Original Version" refers to the collection of Font Software components as
|
||||
distributed by the Copyright Holder(s).
|
||||
|
||||
"Modified Version" refers to any derivative made by adding to, deleting,
|
||||
or substituting -- in part or in whole -- any of the components of the
|
||||
Original Version, by changing formats or by porting the Font Software to a
|
||||
new environment.
|
||||
|
||||
"Author" refers to any designer, engineer, programmer, technical
|
||||
writer or other person who contributed to the Font Software.
|
||||
|
||||
PERMISSION AND CONDITIONS
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of the Font Software, to use, study, copy, merge, embed, modify,
|
||||
redistribute, and sell modified and unmodified copies of the Font
|
||||
Software, subject to the following conditions:
|
||||
|
||||
1) Neither the Font Software nor any of its individual components,
|
||||
in Original or Modified Versions, may be sold by itself.
|
||||
|
||||
2) Original or Modified Versions of the Font Software may be bundled,
|
||||
redistributed and/or sold with any software, provided that each copy
|
||||
contains the above copyright notice and this license. These can be
|
||||
included either as stand-alone text files, human-readable headers or
|
||||
in the appropriate machine-readable metadata fields within text or
|
||||
binary files as long as those fields can be easily viewed by the user.
|
||||
|
||||
3) No Modified Version of the Font Software may use the Reserved Font
|
||||
Name(s) unless explicit written permission is granted by the corresponding
|
||||
Copyright Holder. This restriction only applies to the primary font name as
|
||||
presented to the users.
|
||||
|
||||
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
|
||||
Software shall not be used to promote, endorse or advertise any
|
||||
Modified Version, except to acknowledge the contribution(s) of the
|
||||
Copyright Holder(s) and the Author(s) or with their explicit written
|
||||
permission.
|
||||
|
||||
5) The Font Software, modified or unmodified, in part or in whole,
|
||||
must be distributed entirely under this license, and must not be
|
||||
distributed under any other license. The requirement for fonts to
|
||||
remain under this license does not apply to any document created
|
||||
using the Font Software.
|
||||
|
||||
TERMINATION
|
||||
This license becomes null and void if any of the above conditions are
|
||||
not met.
|
||||
|
||||
DISCLAIMER
|
||||
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
|
||||
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
|
||||
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
|
||||
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
|
||||
OTHER DEALINGS IN THE FONT SOFTWARE.
|
BIN
og-image/api/_fonts/Inter-Regular.woff2
Normal file
BIN
og-image/api/_fonts/Inter-Regular.woff2
Normal file
Binary file not shown.
124
og-image/api/_fonts/Vera-License.txt
Normal file
124
og-image/api/_fonts/Vera-License.txt
Normal file
|
@ -0,0 +1,124 @@
|
|||
Bitstream Vera Fonts Copyright
|
||||
|
||||
The fonts have a generous copyright, allowing derivative works (as
|
||||
long as "Bitstream" or "Vera" are not in the names), and full
|
||||
redistribution (so long as they are not *sold* by themselves). They
|
||||
can be be bundled, redistributed and sold with any software.
|
||||
|
||||
The fonts are distributed under the following copyright:
|
||||
|
||||
Copyright
|
||||
=========
|
||||
|
||||
Copyright (c) 2003 by Bitstream, Inc. All Rights Reserved. Bitstream
|
||||
Vera is a trademark of Bitstream, Inc.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of the fonts accompanying this license ("Fonts") and associated
|
||||
documentation files (the "Font Software"), to reproduce and distribute
|
||||
the Font Software, including without limitation the rights to use,
|
||||
copy, merge, publish, distribute, and/or sell copies of the Font
|
||||
Software, and to permit persons to whom the Font Software is furnished
|
||||
to do so, subject to the following conditions:
|
||||
|
||||
The above copyright and trademark notices and this permission notice
|
||||
shall be included in all copies of one or more of the Font Software
|
||||
typefaces.
|
||||
|
||||
The Font Software may be modified, altered, or added to, and in
|
||||
particular the designs of glyphs or characters in the Fonts may be
|
||||
modified and additional glyphs or characters may be added to the
|
||||
Fonts, only if the fonts are renamed to names not containing either
|
||||
the words "Bitstream" or the word "Vera".
|
||||
|
||||
This License becomes null and void to the extent applicable to Fonts
|
||||
or Font Software that has been modified and is distributed under the
|
||||
"Bitstream Vera" names.
|
||||
|
||||
The Font Software may be sold as part of a larger software package but
|
||||
no copy of one or more of the Font Software typefaces may be sold by
|
||||
itself.
|
||||
|
||||
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
|
||||
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL
|
||||
BITSTREAM OR THE GNOME FOUNDATION BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
||||
OTHER LIABILITY, INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL,
|
||||
OR CONSEQUENTIAL DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||
OTHERWISE, ARISING FROM, OUT OF THE USE OR INABILITY TO USE THE FONT
|
||||
SOFTWARE OR FROM OTHER DEALINGS IN THE FONT SOFTWARE.
|
||||
|
||||
Except as contained in this notice, the names of Gnome, the Gnome
|
||||
Foundation, and Bitstream Inc., shall not be used in advertising or
|
||||
otherwise to promote the sale, use or other dealings in this Font
|
||||
Software without prior written authorization from the Gnome Foundation
|
||||
or Bitstream Inc., respectively. For further information, contact:
|
||||
fonts at gnome dot org.
|
||||
|
||||
Copyright FAQ
|
||||
=============
|
||||
|
||||
1. I don't understand the resale restriction... What gives?
|
||||
|
||||
Bitstream is giving away these fonts, but wishes to ensure its
|
||||
competitors can't just drop the fonts as is into a font sale system
|
||||
and sell them as is. It seems fair that if Bitstream can't make money
|
||||
from the Bitstream Vera fonts, their competitors should not be able to
|
||||
do so either. You can sell the fonts as part of any software package,
|
||||
however.
|
||||
|
||||
2. I want to package these fonts separately for distribution and
|
||||
sale as part of a larger software package or system. Can I do so?
|
||||
|
||||
Yes. A RPM or Debian package is a "larger software package" to begin
|
||||
with, and you aren't selling them independently by themselves.
|
||||
See 1. above.
|
||||
|
||||
3. Are derivative works allowed?
|
||||
Yes!
|
||||
|
||||
4. Can I change or add to the font(s)?
|
||||
Yes, but you must change the name(s) of the font(s).
|
||||
|
||||
5. Under what terms are derivative works allowed?
|
||||
|
||||
You must change the name(s) of the fonts. This is to ensure the
|
||||
quality of the fonts, both to protect Bitstream and Gnome. We want to
|
||||
ensure that if an application has opened a font specifically of these
|
||||
names, it gets what it expects (though of course, using fontconfig,
|
||||
substitutions could still could have occurred during font
|
||||
opening). You must include the Bitstream copyright. Additional
|
||||
copyrights can be added, as per copyright law. Happy Font Hacking!
|
||||
|
||||
6. If I have improvements for Bitstream Vera, is it possible they might get
|
||||
adopted in future versions?
|
||||
|
||||
Yes. The contract between the Gnome Foundation and Bitstream has
|
||||
provisions for working with Bitstream to ensure quality additions to
|
||||
the Bitstream Vera font family. Please contact us if you have such
|
||||
additions. Note, that in general, we will want such additions for the
|
||||
entire family, not just a single font, and that you'll have to keep
|
||||
both Gnome and Jim Lyles, Vera's designer, happy! To make sense to add
|
||||
glyphs to the font, they must be stylistically in keeping with Vera's
|
||||
design. Vera cannot become a "ransom note" font. Jim Lyles will be
|
||||
providing a document describing the design elements used in Vera, as a
|
||||
guide and aid for people interested in contributing to Vera.
|
||||
|
||||
7. I want to sell a software package that uses these fonts: Can I do so?
|
||||
|
||||
Sure. Bundle the fonts with your software and sell your software
|
||||
with the fonts. That is the intent of the copyright.
|
||||
|
||||
8. If applications have built the names "Bitstream Vera" into them,
|
||||
can I override this somehow to use fonts of my choosing?
|
||||
|
||||
This depends on exact details of the software. Most open source
|
||||
systems and software (e.g., Gnome, KDE, etc.) are now converting to
|
||||
use fontconfig (see www.fontconfig.org) to handle font configuration,
|
||||
selection and substitution; it has provisions for overriding font
|
||||
names and subsituting alternatives. An example is provided by the
|
||||
supplied local.conf file, which chooses the family Bitstream Vera for
|
||||
"sans", "serif" and "monospace". Other software (e.g., the XFree86
|
||||
core server) has other mechanisms for font substitution.
|
||||
|
BIN
og-image/api/_fonts/Vera-Mono.woff2
Normal file
BIN
og-image/api/_fonts/Vera-Mono.woff2
Normal file
Binary file not shown.
26
og-image/api/_lib/chromium.ts
Normal file
26
og-image/api/_lib/chromium.ts
Normal file
|
@ -0,0 +1,26 @@
|
|||
import core from "puppeteer-core";
|
||||
import { getOptions } from "./options";
|
||||
import { FileType } from "./types";
|
||||
let _page: core.Page | null;
|
||||
|
||||
async function getPage(isDev: boolean) {
|
||||
if (_page) {
|
||||
return _page;
|
||||
}
|
||||
const options = await getOptions(isDev);
|
||||
const browser = await core.launch(options);
|
||||
_page = await browser.newPage();
|
||||
return _page;
|
||||
}
|
||||
|
||||
export async function getScreenshot(
|
||||
html: string,
|
||||
type: FileType,
|
||||
isDev: boolean
|
||||
) {
|
||||
const page = await getPage(isDev);
|
||||
await page.setViewport({ width: 2048, height: 1170 });
|
||||
await page.setContent(html);
|
||||
const file = await page.screenshot({ type });
|
||||
return file;
|
||||
}
|
31
og-image/api/_lib/options.ts
Normal file
31
og-image/api/_lib/options.ts
Normal file
|
@ -0,0 +1,31 @@
|
|||
import chrome from "chrome-aws-lambda";
|
||||
const exePath =
|
||||
process.platform === "win32"
|
||||
? "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"
|
||||
: process.platform === "linux"
|
||||
? "/usr/bin/google-chrome"
|
||||
: "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome";
|
||||
|
||||
interface Options {
|
||||
args: string[];
|
||||
executablePath: string;
|
||||
headless: boolean;
|
||||
}
|
||||
|
||||
export async function getOptions(isDev: boolean) {
|
||||
let options: Options;
|
||||
if (isDev) {
|
||||
options = {
|
||||
args: [],
|
||||
executablePath: exePath,
|
||||
headless: true,
|
||||
};
|
||||
} else {
|
||||
options = {
|
||||
args: chrome.args,
|
||||
executablePath: await chrome.executablePath,
|
||||
headless: chrome.headless,
|
||||
};
|
||||
}
|
||||
return options;
|
||||
}
|
60
og-image/api/_lib/parser.ts
Normal file
60
og-image/api/_lib/parser.ts
Normal file
|
@ -0,0 +1,60 @@
|
|||
import { IncomingMessage } from "http";
|
||||
import { parse } from "url";
|
||||
import { ParsedRequest } from "./types";
|
||||
|
||||
export function parseRequest(req: IncomingMessage) {
|
||||
console.log("HTTP " + req.url);
|
||||
const { pathname, query } = parse(req.url || "/", true);
|
||||
const { fontSize, images, widths, heights, theme, md } = query || {};
|
||||
|
||||
if (Array.isArray(fontSize)) {
|
||||
throw new Error("Expected a single fontSize");
|
||||
}
|
||||
if (Array.isArray(theme)) {
|
||||
throw new Error("Expected a single theme");
|
||||
}
|
||||
|
||||
const arr = (pathname || "/").slice(1).split(".");
|
||||
let extension = "";
|
||||
let text = "";
|
||||
if (arr.length === 0) {
|
||||
text = "";
|
||||
} else if (arr.length === 1) {
|
||||
text = arr[0];
|
||||
} else {
|
||||
extension = arr.pop() as string;
|
||||
text = arr.join(".");
|
||||
}
|
||||
|
||||
const parsedRequest: ParsedRequest = {
|
||||
fileType: extension === "jpeg" ? extension : "png",
|
||||
text: decodeURIComponent(text),
|
||||
theme: theme === "dark" ? "dark" : "light",
|
||||
md: md === "1" || md === "true",
|
||||
fontSize: fontSize || "96px",
|
||||
images: getArray(images),
|
||||
widths: getArray(widths),
|
||||
heights: getArray(heights),
|
||||
};
|
||||
parsedRequest.images = getDefaultImages(parsedRequest.images);
|
||||
return parsedRequest;
|
||||
}
|
||||
|
||||
function getArray(stringOrArray: string[] | string | undefined): string[] {
|
||||
if (typeof stringOrArray === "undefined") {
|
||||
return [];
|
||||
} else if (Array.isArray(stringOrArray)) {
|
||||
return stringOrArray;
|
||||
} else {
|
||||
return [stringOrArray];
|
||||
}
|
||||
}
|
||||
|
||||
function getDefaultImages(images: string[]): string[] {
|
||||
const defaultImage = "https://manifold.markets/logo.png";
|
||||
|
||||
if (!images || !images[0]) {
|
||||
return [defaultImage];
|
||||
}
|
||||
return images;
|
||||
}
|
12
og-image/api/_lib/sanitizer.ts
Normal file
12
og-image/api/_lib/sanitizer.ts
Normal file
|
@ -0,0 +1,12 @@
|
|||
const entityMap: { [key: string]: string } = {
|
||||
"&": "&",
|
||||
"<": "<",
|
||||
">": ">",
|
||||
'"': """,
|
||||
"'": "'",
|
||||
"/": "/",
|
||||
};
|
||||
|
||||
export function sanitizeHtml(html: string) {
|
||||
return String(html).replace(/[&<>"'\/]/g, (key) => entityMap[key]);
|
||||
}
|
157
og-image/api/_lib/template.ts
Normal file
157
og-image/api/_lib/template.ts
Normal file
|
@ -0,0 +1,157 @@
|
|||
import { readFileSync } from "fs";
|
||||
import marked from "marked";
|
||||
import { sanitizeHtml } from "./sanitizer";
|
||||
import { ParsedRequest } from "./types";
|
||||
const twemoji = require("twemoji");
|
||||
const twOptions = { folder: "svg", ext: ".svg" };
|
||||
const emojify = (text: string) => twemoji.parse(text, twOptions);
|
||||
|
||||
const rglr = readFileSync(
|
||||
`${__dirname}/../_fonts/Inter-Regular.woff2`
|
||||
).toString("base64");
|
||||
const bold = readFileSync(`${__dirname}/../_fonts/Inter-Bold.woff2`).toString(
|
||||
"base64"
|
||||
);
|
||||
const mono = readFileSync(`${__dirname}/../_fonts/Vera-Mono.woff2`).toString(
|
||||
"base64"
|
||||
);
|
||||
|
||||
function getCss(theme: string, fontSize: string) {
|
||||
let background = "white";
|
||||
let foreground = "black";
|
||||
let radial = "lightgray";
|
||||
|
||||
if (theme === "dark") {
|
||||
background = "black";
|
||||
foreground = "white";
|
||||
radial = "dimgray";
|
||||
}
|
||||
// To use Readex Pro: `font-family: 'Readex Pro', sans-serif;`
|
||||
return `
|
||||
@import url('https://fonts.googleapis.com/css2?family=Major+Mono+Display&family=Readex+Pro:wght@400;700&display=swap');
|
||||
|
||||
@font-face {
|
||||
font-family: 'Inter';
|
||||
font-style: normal;
|
||||
font-weight: normal;
|
||||
src: url(data:font/woff2;charset=utf-8;base64,${rglr}) format('woff2');
|
||||
}
|
||||
|
||||
@font-face {
|
||||
font-family: 'Inter';
|
||||
font-style: normal;
|
||||
font-weight: bold;
|
||||
src: url(data:font/woff2;charset=utf-8;base64,${bold}) format('woff2');
|
||||
}
|
||||
|
||||
@font-face {
|
||||
font-family: 'Vera';
|
||||
font-style: normal;
|
||||
font-weight: normal;
|
||||
src: url(data:font/woff2;charset=utf-8;base64,${mono}) format("woff2");
|
||||
}
|
||||
|
||||
body {
|
||||
background: ${background};
|
||||
background-image: radial-gradient(circle at 25px 25px, ${radial} 2%, transparent 0%), radial-gradient(circle at 75px 75px, ${radial} 2%, transparent 0%);
|
||||
background-size: 100px 100px;
|
||||
height: 100vh;
|
||||
display: flex;
|
||||
text-align: center;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
code {
|
||||
color: #D400FF;
|
||||
font-family: 'Vera';
|
||||
white-space: pre-wrap;
|
||||
letter-spacing: -5px;
|
||||
}
|
||||
|
||||
code:before, code:after {
|
||||
content: '\`';
|
||||
}
|
||||
|
||||
.logo-wrapper {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
align-content: center;
|
||||
justify-content: center;
|
||||
justify-items: center;
|
||||
}
|
||||
|
||||
.logo {
|
||||
margin: 0 75px;
|
||||
}
|
||||
|
||||
.plus {
|
||||
color: #BBB;
|
||||
font-family: Times New Roman, Verdana;
|
||||
font-size: 100px;
|
||||
}
|
||||
|
||||
.spacer {
|
||||
margin: 150px;
|
||||
}
|
||||
|
||||
.emoji {
|
||||
height: 1em;
|
||||
width: 1em;
|
||||
margin: 0 .05em 0 .1em;
|
||||
vertical-align: -0.1em;
|
||||
}
|
||||
|
||||
.heading {
|
||||
font-family: 'Major Mono Display', monospace;
|
||||
font-size: ${sanitizeHtml(fontSize)};
|
||||
font-style: normal;
|
||||
color: ${foreground};
|
||||
line-height: 1.8;
|
||||
}`;
|
||||
}
|
||||
|
||||
export function getHtml(parsedReq: ParsedRequest) {
|
||||
const { text, theme, md, fontSize, images, widths, heights } = parsedReq;
|
||||
return `<!DOCTYPE html>
|
||||
<html>
|
||||
<meta charset="utf-8">
|
||||
<title>Generated Image</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<style>
|
||||
${getCss(theme, fontSize)}
|
||||
</style>
|
||||
<body>
|
||||
<div>
|
||||
<div class="spacer">
|
||||
<div class="logo-wrapper">
|
||||
${images
|
||||
.map(
|
||||
(img, i) =>
|
||||
getPlusSign(i) + getImage(img, widths[i], heights[i])
|
||||
)
|
||||
.join("")}
|
||||
</div>
|
||||
<div class="spacer">
|
||||
<div class="heading">${emojify(
|
||||
md ? marked(text) : sanitizeHtml(text)
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>`;
|
||||
}
|
||||
|
||||
function getImage(src: string, width = "auto", height = "225") {
|
||||
return `<img
|
||||
class="logo"
|
||||
alt="Generated Image"
|
||||
src="${sanitizeHtml(src)}"
|
||||
width="${sanitizeHtml(width)}"
|
||||
height="${sanitizeHtml(height)}"
|
||||
/>`;
|
||||
}
|
||||
|
||||
function getPlusSign(i: number) {
|
||||
return i === 0 ? "" : '<div class="plus">+</div>';
|
||||
}
|
13
og-image/api/_lib/types.ts
Normal file
13
og-image/api/_lib/types.ts
Normal file
|
@ -0,0 +1,13 @@
|
|||
export type FileType = "png" | "jpeg";
|
||||
export type Theme = "light" | "dark";
|
||||
|
||||
export interface ParsedRequest {
|
||||
fileType: FileType;
|
||||
text: string;
|
||||
theme: Theme;
|
||||
md: boolean;
|
||||
fontSize: string;
|
||||
images: string[];
|
||||
widths: string[];
|
||||
heights: string[];
|
||||
}
|
36
og-image/api/index.ts
Normal file
36
og-image/api/index.ts
Normal file
|
@ -0,0 +1,36 @@
|
|||
import { IncomingMessage, ServerResponse } from "http";
|
||||
import { parseRequest } from "./_lib/parser";
|
||||
import { getScreenshot } from "./_lib/chromium";
|
||||
import { getHtml } from "./_lib/template";
|
||||
|
||||
const isDev = !process.env.AWS_REGION;
|
||||
const isHtmlDebug = process.env.OG_HTML_DEBUG === "1";
|
||||
|
||||
export default async function handler(
|
||||
req: IncomingMessage,
|
||||
res: ServerResponse
|
||||
) {
|
||||
try {
|
||||
const parsedReq = parseRequest(req);
|
||||
const html = getHtml(parsedReq);
|
||||
if (isHtmlDebug) {
|
||||
res.setHeader("Content-Type", "text/html");
|
||||
res.end(html);
|
||||
return;
|
||||
}
|
||||
const { fileType } = parsedReq;
|
||||
const file = await getScreenshot(html, fileType, isDev);
|
||||
res.statusCode = 200;
|
||||
res.setHeader("Content-Type", `image/${fileType}`);
|
||||
res.setHeader(
|
||||
"Cache-Control",
|
||||
`public, immutable, no-transform, s-maxage=31536000, max-age=31536000`
|
||||
);
|
||||
res.end(file);
|
||||
} catch (e) {
|
||||
res.statusCode = 500;
|
||||
res.setHeader("Content-Type", "text/html");
|
||||
res.end("<h1>Internal Error</h1><p>Sorry, there was a problem</p>");
|
||||
console.error(e);
|
||||
}
|
||||
}
|
20
og-image/api/tsconfig.json
Normal file
20
og-image/api/tsconfig.json
Normal file
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"outDir": "dist",
|
||||
"module": "commonjs",
|
||||
"target": "esnext",
|
||||
"moduleResolution": "node",
|
||||
"jsx": "react",
|
||||
"sourceMap": true,
|
||||
"strict": true,
|
||||
"noFallthroughCasesInSwitch": true,
|
||||
"noImplicitReturns": true,
|
||||
"noEmitOnError": true,
|
||||
"noUnusedLocals": true,
|
||||
"noUnusedParameters": true,
|
||||
"removeComments": true,
|
||||
"preserveConstEnums": true,
|
||||
"esModuleInterop": true
|
||||
},
|
||||
"include": ["./"]
|
||||
}
|
20
og-image/package.json
Normal file
20
og-image/package.json
Normal file
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"build": "tsc -p api/tsconfig.json && tsc -p web/tsconfig.json",
|
||||
"start": "vercel dev"
|
||||
},
|
||||
"dependencies": {
|
||||
"chrome-aws-lambda": "7.0.0",
|
||||
"marked": "2.0.0",
|
||||
"puppeteer-core": "7.0.0",
|
||||
"twemoji": "13.0.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/marked": "1.2.2",
|
||||
"@types/puppeteer": "5.4.3",
|
||||
"@types/puppeteer-core": "5.4.0",
|
||||
"typescript": "4.1.5",
|
||||
"vercel": "23.1.2"
|
||||
}
|
||||
}
|
BIN
og-image/public/favicon.ico
Normal file
BIN
og-image/public/favicon.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 4.2 KiB |
89
og-image/public/index.html
Normal file
89
og-image/public/index.html
Normal file
|
@ -0,0 +1,89 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<meta http-equiv="x-ua-compatible" content="ie=edge" />
|
||||
|
||||
<meta name="twitter:card" content="summary_large_image" />
|
||||
<meta name="twitter:site" content="@vercel" />
|
||||
<meta property="og:site_name" content="Open Graph Image as a Service" />
|
||||
<meta property="og:type" content="website" />
|
||||
<meta property="og:title" content="Open Graph Image as a Service" />
|
||||
<meta property="og:locale" content="en" />
|
||||
<meta property="og:url" content="https://og-image.vercel.app" />
|
||||
<link rel="canonical" href="https://og-image.vercel.app" />
|
||||
<meta
|
||||
name="description"
|
||||
content="A service to generate dynamic Open Graph images on-the-fly for the purpose of sharing a website to social media. Proudly hosted on Vercel."
|
||||
/>
|
||||
<meta
|
||||
property="og:description"
|
||||
content="A service to generate dynamic Open Graph images on-the-fly for the purpose of sharing a website to social media. Proudly hosted on Vercel."
|
||||
/>
|
||||
<meta
|
||||
property="og:image"
|
||||
content="https://og-image.vercel.app/Open%20Graph%20Image%20as%20a%20Service.png?theme=light&md=1&fontSize=95px&images=https%3A%2F%2Fassets.vercel.com%2Fimage%2Fupload%2Ffront%2Fassets%2Fdesign%2Fzeit-black-triangle.svg"
|
||||
/>
|
||||
<title>Open Graph Image as a Service</title>
|
||||
<meta name="viewport" content="width=device-width, maximum-scale=1.0" />
|
||||
<link rel="stylesheet" href="style.css" />
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="wrapper">
|
||||
<a
|
||||
href="https://github.com/vercel/og-image"
|
||||
class="github-corner"
|
||||
aria-label="View source on GitHub"
|
||||
>
|
||||
<svg width="80" height="80" viewBox="0 0 250 250" class="svg">
|
||||
<path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
|
||||
<path
|
||||
d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2"
|
||||
fill="currentColor"
|
||||
style="transform-origin: 130px 106px"
|
||||
class="octo-arm"
|
||||
></path>
|
||||
<path
|
||||
d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z"
|
||||
fill="currentColor"
|
||||
class="octo-body"
|
||||
></path>
|
||||
</svg>
|
||||
</a>
|
||||
<h1>Open Graph Image as a Service</h1>
|
||||
<div id="app">
|
||||
<em>Loading...</em>
|
||||
</div>
|
||||
<div class="center">
|
||||
<h2>What is this?</h2>
|
||||
<p>
|
||||
This is a service that generates dynamic
|
||||
<a href="http://ogp.me">Open Graph</a> images that you can embed in
|
||||
your <code><meta></code> tags.
|
||||
</p>
|
||||
<p>
|
||||
For each keystroke, headless chromium is used to render an HTML page
|
||||
and take a screenshot of the result which gets cached.
|
||||
</p>
|
||||
<p>
|
||||
Find out how this works and deploy your own image generator by
|
||||
visiting <a href="https://github.com/vercel/og-image">GitHub</a>.
|
||||
</p>
|
||||
<footer>
|
||||
Proudly hosted on <a href="https://vercel.com">▲Vercel</a>
|
||||
</footer>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<script
|
||||
type="text/javascript"
|
||||
src="https://cdn.jsdelivr.net/npm/dot-dom@0.3.0/dotdom.min.js"
|
||||
></script>
|
||||
<script
|
||||
type="text/javascript"
|
||||
src="https://cdn.jsdelivr.net/npm/copee@1.0.6/dist/copee.umd.js"
|
||||
></script>
|
||||
<script type="module" src="dist/web/index.js"></script>
|
||||
</body>
|
||||
</html>
|
1
og-image/public/robots.txt
Normal file
1
og-image/public/robots.txt
Normal file
|
@ -0,0 +1 @@
|
|||
User-agent: *
|
362
og-image/public/style.css
Normal file
362
og-image/public/style.css
Normal file
|
@ -0,0 +1,362 @@
|
|||
body {
|
||||
font-family: "SF Pro Text", "SF Pro Icons", "Helvetica Neue", "Helvetica",
|
||||
"Arial", sans-serif;
|
||||
margin: 20px;
|
||||
overflow-x: hidden;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
a {
|
||||
cursor: pointer;
|
||||
color: #0076ff;
|
||||
text-decoration: none;
|
||||
transition: all 0.2s ease;
|
||||
border-bottom: 1px solid white;
|
||||
}
|
||||
|
||||
a:hover {
|
||||
border-bottom: 1px solid #0076ff;
|
||||
}
|
||||
|
||||
footer {
|
||||
opacity: 0.5;
|
||||
font-size: 0.8em;
|
||||
}
|
||||
|
||||
.center,
|
||||
h1 {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.container {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.split {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.pull-left {
|
||||
min-width: 50%;
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
}
|
||||
|
||||
.pull-right {
|
||||
min-width: 50%;
|
||||
}
|
||||
|
||||
button {
|
||||
appearance: none;
|
||||
align-items: center;
|
||||
color: #fff;
|
||||
background: #000;
|
||||
display: inline-flex;
|
||||
width: 100px;
|
||||
height: 40px;
|
||||
padding: 0 25px;
|
||||
outline: none;
|
||||
border: 1px solid #000;
|
||||
font-size: 12px;
|
||||
justify-content: center;
|
||||
text-transform: uppercase;
|
||||
cursor: pointer;
|
||||
text-align: center;
|
||||
user-select: none;
|
||||
font-weight: 100;
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
transition: border 0.2s, background 0.2s, color 0.2s ease-out;
|
||||
border-radius: 5px;
|
||||
white-space: nowrap;
|
||||
text-decoration: none;
|
||||
line-height: 0;
|
||||
|
||||
height: 24px;
|
||||
width: calc(100% + 2px);
|
||||
padding: 0 10px;
|
||||
font-size: 12px;
|
||||
background: #fff;
|
||||
border: 1px solid #eaeaea;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
button:hover {
|
||||
color: #000;
|
||||
border-color: #000;
|
||||
background: #fff;
|
||||
}
|
||||
|
||||
.input-outer-wrapper {
|
||||
align-items: center;
|
||||
border-radius: 5px;
|
||||
border: 1px solid #e1e1e1;
|
||||
display: inline-flex;
|
||||
height: 37px;
|
||||
position: relative;
|
||||
transition: border 0.2s ease, color 0.2s ease;
|
||||
vertical-align: middle;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.input-outer-wrapper:hover {
|
||||
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
|
||||
border-color: #ddd;
|
||||
}
|
||||
|
||||
.input-inner-wrapper {
|
||||
display: block;
|
||||
margin: 4px 10px;
|
||||
position: relative;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.input-inner-wrapper input {
|
||||
background-color: transparent;
|
||||
border-radius: 0;
|
||||
border: none;
|
||||
box-shadow: none;
|
||||
box-sizing: border-box;
|
||||
display: block;
|
||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
|
||||
"Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
|
||||
sans-serif;
|
||||
font-size: 14px;
|
||||
line-height: 27px;
|
||||
outline: 0;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.select-wrapper {
|
||||
appearance: none;
|
||||
color: #000;
|
||||
background: #fff;
|
||||
display: inline-flex;
|
||||
height: 40px;
|
||||
outline: none;
|
||||
border: 1px solid #eaeaea;
|
||||
font-size: 12px;
|
||||
text-transform: uppercase;
|
||||
user-select: none;
|
||||
font-weight: 100;
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
transition: border 0.2s, background 0.2s, color 0.2s ease-out,
|
||||
box-shadow 0.2s ease;
|
||||
border-radius: 5px;
|
||||
white-space: nowrap;
|
||||
line-height: 0;
|
||||
width: auto;
|
||||
min-width: 100%;
|
||||
}
|
||||
|
||||
.select-wrapper:hover {
|
||||
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1);
|
||||
border-color: #ddd;
|
||||
}
|
||||
|
||||
.select-wrapper.small {
|
||||
height: 24px;
|
||||
min-width: 100px;
|
||||
width: 100px;
|
||||
}
|
||||
|
||||
.select-arrow {
|
||||
border-left: 1px solid #eaeaea;
|
||||
background: #fff;
|
||||
width: 40px;
|
||||
height: 100%;
|
||||
position: absolute;
|
||||
right: 0;
|
||||
pointer-events: none;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.select-arrow.small {
|
||||
width: 22px;
|
||||
}
|
||||
|
||||
.svg {
|
||||
fill: #151513;
|
||||
color: #fff;
|
||||
position: absolute;
|
||||
top: 0;
|
||||
border: 0;
|
||||
right: 0;
|
||||
}
|
||||
select {
|
||||
height: 100%;
|
||||
border: none;
|
||||
box-shadow: none;
|
||||
background: transparent;
|
||||
background-image: none;
|
||||
color: #000;
|
||||
line-height: 40px;
|
||||
font-size: 14px;
|
||||
margin-right: -20px;
|
||||
width: calc(100% + 20px);
|
||||
padding: 0 0 0 16px;
|
||||
text-transform: none;
|
||||
}
|
||||
|
||||
.field-flex {
|
||||
display: flex;
|
||||
margin-top: 10px;
|
||||
}
|
||||
|
||||
.field-value {
|
||||
margin: 10px 80px;
|
||||
}
|
||||
|
||||
.field-label {
|
||||
display: inline-block;
|
||||
width: 100px;
|
||||
margin-right: 20px;
|
||||
text-align: right;
|
||||
}
|
||||
|
||||
.field-value {
|
||||
width: 200px;
|
||||
display: inline-block;
|
||||
}
|
||||
|
||||
label {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.toast-area {
|
||||
position: fixed;
|
||||
bottom: 10px;
|
||||
right: 20px;
|
||||
max-width: 420px;
|
||||
z-index: 2000;
|
||||
transition: transform 0.4s ease;
|
||||
}
|
||||
|
||||
.toast-outer {
|
||||
width: 420px;
|
||||
height: 72px;
|
||||
position: absolute;
|
||||
bottom: 0;
|
||||
right: 0;
|
||||
transition: all 0.4s ease;
|
||||
transform: translate3d(0, 130%, 0px) scale(1);
|
||||
animation: show-jsx-1861505484 0.4s ease forwards;
|
||||
opacity: 1;
|
||||
}
|
||||
|
||||
.toast-inner {
|
||||
width: 420px;
|
||||
background: black;
|
||||
color: white;
|
||||
border: 0;
|
||||
border-radius: 5px;
|
||||
height: 60px;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
|
||||
box-shadow: 0 4px 9px rgba(0, 0, 0, 0.12);
|
||||
font-size: 14px;
|
||||
display: flex;
|
||||
}
|
||||
|
||||
.toast-message {
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
width: 100%;
|
||||
overflow: hidden;
|
||||
margin-top: -1px;
|
||||
margin-left: 20px;
|
||||
}
|
||||
|
||||
img {
|
||||
max-width: 100%;
|
||||
transition: all 0.3s ease-in 0s;
|
||||
}
|
||||
|
||||
.image-wrapper {
|
||||
display: block;
|
||||
cursor: pointer;
|
||||
text-decoration: none;
|
||||
background: #fff;
|
||||
box-shadow: 0px 1px 5px 0px rgba(0, 0, 0, 0.12);
|
||||
border-radius: 5px;
|
||||
margin-bottom: 50px;
|
||||
transition: all 0.2s ease;
|
||||
max-width: 100%;
|
||||
}
|
||||
|
||||
.image-wrapper:hover {
|
||||
box-shadow: 0 1px 16px rgba(0, 0, 0, 0.1);
|
||||
border-color: #ddd;
|
||||
}
|
||||
|
||||
@media (max-width: 1000px) {
|
||||
.field {
|
||||
margin: 20px 10px;
|
||||
}
|
||||
.pull-left {
|
||||
margin-left: 1.5em;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 900px) {
|
||||
.split {
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.field-label {
|
||||
width: 60px;
|
||||
font-size: 13px;
|
||||
}
|
||||
}
|
||||
|
||||
.github-corner:hover .octo-arm {
|
||||
animation: octocat-wave 560ms ease-in-out;
|
||||
}
|
||||
|
||||
@keyframes octocat-wave {
|
||||
0%,
|
||||
100% {
|
||||
transform: rotate(0);
|
||||
}
|
||||
20%,
|
||||
60% {
|
||||
transform: rotate(-25deg);
|
||||
}
|
||||
40%,
|
||||
80% {
|
||||
transform: rotate(10deg);
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 500px) {
|
||||
.github-corner:hover .octo-arm {
|
||||
animation: none;
|
||||
}
|
||||
.github-corner .octo-arm {
|
||||
animation: octocat-wave 560ms ease-in-out;
|
||||
}
|
||||
.container {
|
||||
margin: 19%;
|
||||
}
|
||||
.svg {
|
||||
right: -33%;
|
||||
}
|
||||
}
|
||||
@media (max-width: 360px) {
|
||||
.container {
|
||||
margin: 29.5%;
|
||||
}
|
||||
.svg {
|
||||
right: -52%;
|
||||
}
|
||||
}
|
BIN
og-image/public/tweet.png
Normal file
BIN
og-image/public/tweet.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 217 KiB |
3
og-image/vercel.json
Normal file
3
og-image/vercel.json
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"rewrites": [{ "source": "/(.+)", "destination": "/api" }]
|
||||
}
|
483
og-image/web/index.ts
Normal file
483
og-image/web/index.ts
Normal file
|
@ -0,0 +1,483 @@
|
|||
import { ParsedRequest, Theme, FileType } from "../api/_lib/types";
|
||||
const { H, R, copee } = window as any;
|
||||
let timeout = -1;
|
||||
|
||||
interface ImagePreviewProps {
|
||||
src: string;
|
||||
onclick: () => void;
|
||||
onload: () => void;
|
||||
onerror: () => void;
|
||||
loading: boolean;
|
||||
}
|
||||
|
||||
const ImagePreview = ({
|
||||
src,
|
||||
onclick,
|
||||
onload,
|
||||
onerror,
|
||||
loading,
|
||||
}: ImagePreviewProps) => {
|
||||
const style = {
|
||||
filter: loading ? "blur(5px)" : "",
|
||||
opacity: loading ? 0.1 : 1,
|
||||
};
|
||||
const title = "Click to copy image URL to clipboard";
|
||||
return H(
|
||||
"a",
|
||||
{ className: "image-wrapper", href: src, onclick },
|
||||
H("img", { src, onload, onerror, style, title })
|
||||
);
|
||||
};
|
||||
|
||||
interface DropdownOption {
|
||||
text: string;
|
||||
value: string;
|
||||
}
|
||||
|
||||
interface DropdownProps {
|
||||
options: DropdownOption[];
|
||||
value: string;
|
||||
onchange: (val: string) => void;
|
||||
small: boolean;
|
||||
}
|
||||
|
||||
const Dropdown = ({ options, value, onchange, small }: DropdownProps) => {
|
||||
const wrapper = small ? "select-wrapper small" : "select-wrapper";
|
||||
const arrow = small ? "select-arrow small" : "select-arrow";
|
||||
return H(
|
||||
"div",
|
||||
{ className: wrapper },
|
||||
H(
|
||||
"select",
|
||||
{ onchange: (e: any) => onchange(e.target.value) },
|
||||
options.map((o) =>
|
||||
H("option", { value: o.value, selected: value === o.value }, o.text)
|
||||
)
|
||||
),
|
||||
H("div", { className: arrow }, "▼")
|
||||
);
|
||||
};
|
||||
|
||||
interface TextInputProps {
|
||||
value: string;
|
||||
oninput: (val: string) => void;
|
||||
}
|
||||
|
||||
const TextInput = ({ value, oninput }: TextInputProps) => {
|
||||
return H(
|
||||
"div",
|
||||
{ className: "input-outer-wrapper" },
|
||||
H(
|
||||
"div",
|
||||
{ className: "input-inner-wrapper" },
|
||||
H("input", {
|
||||
type: "text",
|
||||
value,
|
||||
oninput: (e: any) => oninput(e.target.value),
|
||||
})
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
interface ButtonProps {
|
||||
label: string;
|
||||
onclick: () => void;
|
||||
}
|
||||
|
||||
const Button = ({ label, onclick }: ButtonProps) => {
|
||||
return H("button", { onclick }, label);
|
||||
};
|
||||
|
||||
interface FieldProps {
|
||||
label: string;
|
||||
input: any;
|
||||
}
|
||||
|
||||
const Field = ({ label, input }: FieldProps) => {
|
||||
return H(
|
||||
"div",
|
||||
{ className: "field" },
|
||||
H(
|
||||
"label",
|
||||
H("div", { className: "field-label" }, label),
|
||||
H("div", { className: "field-value" }, input)
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
interface ToastProps {
|
||||
show: boolean;
|
||||
message: string;
|
||||
}
|
||||
|
||||
const Toast = ({ show, message }: ToastProps) => {
|
||||
const style = { transform: show ? "translate3d(0,-0px,-0px) scale(1)" : "" };
|
||||
return H(
|
||||
"div",
|
||||
{ className: "toast-area" },
|
||||
H(
|
||||
"div",
|
||||
{ className: "toast-outer", style },
|
||||
H(
|
||||
"div",
|
||||
{ className: "toast-inner" },
|
||||
H("div", { className: "toast-message" }, message)
|
||||
)
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
const themeOptions: DropdownOption[] = [
|
||||
{ text: "Light", value: "light" },
|
||||
{ text: "Dark", value: "dark" },
|
||||
];
|
||||
|
||||
const fileTypeOptions: DropdownOption[] = [
|
||||
{ text: "PNG", value: "png" },
|
||||
{ text: "JPEG", value: "jpeg" },
|
||||
];
|
||||
|
||||
const fontSizeOptions: DropdownOption[] = Array.from({ length: 10 })
|
||||
.map((_, i) => i * 25)
|
||||
.filter((n) => n > 0)
|
||||
.map((n) => ({ text: n + "px", value: n + "px" }));
|
||||
|
||||
const markdownOptions: DropdownOption[] = [
|
||||
{ text: "Plain Text", value: "0" },
|
||||
{ text: "Markdown", value: "1" },
|
||||
];
|
||||
|
||||
const imageLightOptions: DropdownOption[] = [
|
||||
{
|
||||
text: "Manifold",
|
||||
value: "https://manifold.markets/logo.png",
|
||||
},
|
||||
{
|
||||
text: "Vercel",
|
||||
value:
|
||||
"https://assets.vercel.com/image/upload/front/assets/design/vercel-triangle-black.svg",
|
||||
},
|
||||
{
|
||||
text: "Next.js",
|
||||
value:
|
||||
"https://assets.vercel.com/image/upload/front/assets/design/nextjs-black-logo.svg",
|
||||
},
|
||||
{
|
||||
text: "Hyper",
|
||||
value:
|
||||
"https://assets.vercel.com/image/upload/front/assets/design/hyper-color-logo.svg",
|
||||
},
|
||||
];
|
||||
|
||||
const imageDarkOptions: DropdownOption[] = [
|
||||
{
|
||||
text: "Manifold",
|
||||
value: "https://manifold.markets/logo.png",
|
||||
},
|
||||
{
|
||||
text: "Vercel",
|
||||
value:
|
||||
"https://assets.vercel.com/image/upload/front/assets/design/vercel-triangle-white.svg",
|
||||
},
|
||||
{
|
||||
text: "Next.js",
|
||||
value:
|
||||
"https://assets.vercel.com/image/upload/front/assets/design/nextjs-white-logo.svg",
|
||||
},
|
||||
{
|
||||
text: "Hyper",
|
||||
value:
|
||||
"https://assets.vercel.com/image/upload/front/assets/design/hyper-bw-logo.svg",
|
||||
},
|
||||
];
|
||||
|
||||
const widthOptions = [
|
||||
{ text: "width", value: "auto" },
|
||||
{ text: "50", value: "50" },
|
||||
{ text: "100", value: "100" },
|
||||
{ text: "150", value: "150" },
|
||||
{ text: "200", value: "200" },
|
||||
{ text: "250", value: "250" },
|
||||
{ text: "300", value: "300" },
|
||||
{ text: "350", value: "350" },
|
||||
];
|
||||
|
||||
const heightOptions = [
|
||||
{ text: "height", value: "auto" },
|
||||
{ text: "50", value: "50" },
|
||||
{ text: "100", value: "100" },
|
||||
{ text: "150", value: "150" },
|
||||
{ text: "200", value: "200" },
|
||||
{ text: "250", value: "250" },
|
||||
{ text: "300", value: "300" },
|
||||
{ text: "350", value: "350" },
|
||||
];
|
||||
|
||||
interface AppState extends ParsedRequest {
|
||||
loading: boolean;
|
||||
showToast: boolean;
|
||||
messageToast: string;
|
||||
selectedImageIndex: number;
|
||||
widths: string[];
|
||||
heights: string[];
|
||||
overrideUrl: URL | null;
|
||||
}
|
||||
|
||||
type SetState = (state: Partial<AppState>) => void;
|
||||
|
||||
const App = (_: any, state: AppState, setState: SetState) => {
|
||||
const setLoadingState = (newState: Partial<AppState>) => {
|
||||
window.clearTimeout(timeout);
|
||||
if (state.overrideUrl && state.overrideUrl !== newState.overrideUrl) {
|
||||
newState.overrideUrl = state.overrideUrl;
|
||||
}
|
||||
if (newState.overrideUrl) {
|
||||
timeout = window.setTimeout(() => setState({ overrideUrl: null }), 200);
|
||||
}
|
||||
|
||||
setState({ ...newState, loading: true });
|
||||
};
|
||||
const {
|
||||
fileType = "png",
|
||||
fontSize = "100px",
|
||||
theme = "light",
|
||||
md = true,
|
||||
text = "**Hello** World",
|
||||
images = [imageLightOptions[0].value],
|
||||
widths = [],
|
||||
heights = [],
|
||||
showToast = false,
|
||||
messageToast = "",
|
||||
loading = true,
|
||||
selectedImageIndex = 0,
|
||||
overrideUrl = null,
|
||||
} = state;
|
||||
const mdValue = md ? "1" : "0";
|
||||
const imageOptions = theme === "light" ? imageLightOptions : imageDarkOptions;
|
||||
const url = new URL(window.location.origin);
|
||||
url.pathname = `${encodeURIComponent(text)}.${fileType}`;
|
||||
url.searchParams.append("theme", theme);
|
||||
url.searchParams.append("md", mdValue);
|
||||
url.searchParams.append("fontSize", fontSize);
|
||||
for (let image of images) {
|
||||
url.searchParams.append("images", image);
|
||||
}
|
||||
for (let width of widths) {
|
||||
url.searchParams.append("widths", width);
|
||||
}
|
||||
for (let height of heights) {
|
||||
url.searchParams.append("heights", height);
|
||||
}
|
||||
|
||||
return H(
|
||||
"div",
|
||||
{ className: "split" },
|
||||
H(
|
||||
"div",
|
||||
{ className: "pull-left" },
|
||||
H(
|
||||
"div",
|
||||
H(Field, {
|
||||
label: "Theme",
|
||||
input: H(Dropdown, {
|
||||
options: themeOptions,
|
||||
value: theme,
|
||||
onchange: (val: Theme) => {
|
||||
const options =
|
||||
val === "light" ? imageLightOptions : imageDarkOptions;
|
||||
let clone = [...images];
|
||||
clone[0] = options[selectedImageIndex].value;
|
||||
setLoadingState({ theme: val, images: clone });
|
||||
},
|
||||
}),
|
||||
}),
|
||||
H(Field, {
|
||||
label: "File Type",
|
||||
input: H(Dropdown, {
|
||||
options: fileTypeOptions,
|
||||
value: fileType,
|
||||
onchange: (val: FileType) => setLoadingState({ fileType: val }),
|
||||
}),
|
||||
}),
|
||||
H(Field, {
|
||||
label: "Font Size",
|
||||
input: H(Dropdown, {
|
||||
options: fontSizeOptions,
|
||||
value: fontSize,
|
||||
onchange: (val: string) => setLoadingState({ fontSize: val }),
|
||||
}),
|
||||
}),
|
||||
H(Field, {
|
||||
label: "Text Type",
|
||||
input: H(Dropdown, {
|
||||
options: markdownOptions,
|
||||
value: mdValue,
|
||||
onchange: (val: string) => setLoadingState({ md: val === "1" }),
|
||||
}),
|
||||
}),
|
||||
H(Field, {
|
||||
label: "Text Input",
|
||||
input: H(TextInput, {
|
||||
value: text,
|
||||
oninput: (val: string) => {
|
||||
console.log("oninput " + val);
|
||||
setLoadingState({ text: val, overrideUrl: url });
|
||||
},
|
||||
}),
|
||||
}),
|
||||
H(Field, {
|
||||
label: "Image 1",
|
||||
input: H(
|
||||
"div",
|
||||
H(Dropdown, {
|
||||
options: imageOptions,
|
||||
value: imageOptions[selectedImageIndex].value,
|
||||
onchange: (val: string) => {
|
||||
let clone = [...images];
|
||||
clone[0] = val;
|
||||
const selected = imageOptions.map((o) => o.value).indexOf(val);
|
||||
setLoadingState({
|
||||
images: clone,
|
||||
selectedImageIndex: selected,
|
||||
});
|
||||
},
|
||||
}),
|
||||
H(
|
||||
"div",
|
||||
{ className: "field-flex" },
|
||||
H(Dropdown, {
|
||||
options: widthOptions,
|
||||
value: widths[0],
|
||||
small: true,
|
||||
onchange: (val: string) => {
|
||||
let clone = [...widths];
|
||||
clone[0] = val;
|
||||
setLoadingState({ widths: clone });
|
||||
},
|
||||
}),
|
||||
H(Dropdown, {
|
||||
options: heightOptions,
|
||||
value: heights[0],
|
||||
small: true,
|
||||
onchange: (val: string) => {
|
||||
let clone = [...heights];
|
||||
clone[0] = val;
|
||||
setLoadingState({ heights: clone });
|
||||
},
|
||||
})
|
||||
)
|
||||
),
|
||||
}),
|
||||
...images.slice(1).map((image, i) =>
|
||||
H(Field, {
|
||||
label: `Image ${i + 2}`,
|
||||
input: H(
|
||||
"div",
|
||||
H(TextInput, {
|
||||
value: image,
|
||||
oninput: (val: string) => {
|
||||
let clone = [...images];
|
||||
clone[i + 1] = val;
|
||||
setLoadingState({ images: clone, overrideUrl: url });
|
||||
},
|
||||
}),
|
||||
H(
|
||||
"div",
|
||||
{ className: "field-flex" },
|
||||
H(Dropdown, {
|
||||
options: widthOptions,
|
||||
value: widths[i + 1],
|
||||
small: true,
|
||||
onchange: (val: string) => {
|
||||
let clone = [...widths];
|
||||
clone[i + 1] = val;
|
||||
setLoadingState({ widths: clone });
|
||||
},
|
||||
}),
|
||||
H(Dropdown, {
|
||||
options: heightOptions,
|
||||
value: heights[i + 1],
|
||||
small: true,
|
||||
onchange: (val: string) => {
|
||||
let clone = [...heights];
|
||||
clone[i + 1] = val;
|
||||
setLoadingState({ heights: clone });
|
||||
},
|
||||
})
|
||||
),
|
||||
H(
|
||||
"div",
|
||||
{ className: "field-flex" },
|
||||
H(Button, {
|
||||
label: `Remove Image ${i + 2}`,
|
||||
onclick: (e: MouseEvent) => {
|
||||
e.preventDefault();
|
||||
const filter = (arr: any[]) =>
|
||||
[...arr].filter((_, n) => n !== i + 1);
|
||||
const imagesClone = filter(images);
|
||||
const widthsClone = filter(widths);
|
||||
const heightsClone = filter(heights);
|
||||
setLoadingState({
|
||||
images: imagesClone,
|
||||
widths: widthsClone,
|
||||
heights: heightsClone,
|
||||
});
|
||||
},
|
||||
})
|
||||
)
|
||||
),
|
||||
})
|
||||
),
|
||||
H(Field, {
|
||||
label: `Image ${images.length + 1}`,
|
||||
input: H(Button, {
|
||||
label: `Add Image ${images.length + 1}`,
|
||||
onclick: () => {
|
||||
const nextImage =
|
||||
images.length === 1
|
||||
? "https://cdn.jsdelivr.net/gh/remojansen/logo.ts@master/ts.svg"
|
||||
: "";
|
||||
setLoadingState({ images: [...images, nextImage] });
|
||||
},
|
||||
}),
|
||||
})
|
||||
)
|
||||
),
|
||||
H(
|
||||
"div",
|
||||
{ className: "pull-right" },
|
||||
H(ImagePreview, {
|
||||
src: overrideUrl ? overrideUrl.href : url.href,
|
||||
loading: loading,
|
||||
onload: () => setState({ loading: false }),
|
||||
onerror: () => {
|
||||
setState({
|
||||
showToast: true,
|
||||
messageToast: "Oops, an error occurred",
|
||||
});
|
||||
setTimeout(() => setState({ showToast: false }), 2000);
|
||||
},
|
||||
onclick: (e: Event) => {
|
||||
e.preventDefault();
|
||||
const success = copee.toClipboard(url.href);
|
||||
if (success) {
|
||||
setState({
|
||||
showToast: true,
|
||||
messageToast: "Copied image URL to clipboard",
|
||||
});
|
||||
setTimeout(() => setState({ showToast: false }), 3000);
|
||||
} else {
|
||||
window.open(url.href, "_blank");
|
||||
}
|
||||
return false;
|
||||
},
|
||||
})
|
||||
),
|
||||
H(Toast, {
|
||||
message: messageToast,
|
||||
show: showToast,
|
||||
})
|
||||
);
|
||||
};
|
||||
|
||||
R(H(App), document.getElementById("app"));
|
8
og-image/web/tsconfig.json
Normal file
8
og-image/web/tsconfig.json
Normal file
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"extends": "../api/tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"module": "esnext",
|
||||
"outDir": "../public/dist"
|
||||
},
|
||||
"include": ["./"]
|
||||
}
|
1061
og-image/yarn.lock
Normal file
1061
og-image/yarn.lock
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user