Skip to content

Commit 62bb75a

Browse files
authored
Merge pull request #5 from dev-warner/beta
Beta
2 parents c81b9c5 + c78c6ec commit 62bb75a

File tree

26 files changed

+1008
-746
lines changed

26 files changed

+1008
-746
lines changed
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
---
2+
name: Bug report
3+
about: Create a report to help us improve
4+
title: ''
5+
labels: ''
6+
assignees: ''
7+
---
8+
19
### Description
210

311
<!-- Example: Could not subscribe to updates when... -->
@@ -33,7 +41,7 @@ Example:
3341

3442
### Versions
3543

36-
- @dev-warner/post-card: vX.X.X
44+
- @post-cards/core: vX.X.X
3745

3846
### Other information
3947

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
name: Feature request
3+
about: Suggest an idea for this project
4+
title: ''
5+
labels: ''
6+
assignees: ''
7+
8+
---
9+
10+
**Is your feature request related to a problem? Please describe.**
11+
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12+
13+
**Describe the solution you'd like**
14+
A clear and concise description of what you want to happen.
15+
16+
**Describe alternatives you've considered**
17+
A clear and concise description of any alternative solutions or features you've considered.
18+
19+
**Additional context**
20+
Add any other context or screenshots about the feature request here.

.github/workflows/greetings.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ jobs:
1212
- uses: actions/first-interaction@v1
1313
with:
1414
repo-token: ${{ secrets.GITHUB_TOKEN }}
15-
issue-message: 'Welcome to `@dev-warner/post-card` community! Thanks so much for creating your first issue ✔️'
16-
pr-message: 'Thanks so much for creating your first PR, the `@dev-warner/post-card` community thanks you 🎉'
15+
issue-message: 'Welcome to `@post-cards/core` community! Thanks so much for creating your first issue ✔️'
16+
pr-message: 'Thanks so much for creating your first PR, the `@post-cards/core` community thanks you 🎉'

.github/workflows/release.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ on:
33
push:
44
branches:
55
- main
6-
- next
76
- beta
8-
- alpha
97
jobs:
108
release:
119
name: Release

CONTRIBUTING.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
We're really glad you're reading this, because we need volunteer developers to help this project come to fruition. 👏
2+
3+
## Instructions
4+
5+
These steps will guide you through contributing to this project:
6+
7+
- Fork the repo
8+
- Clone it and install dependencies
9+
10+
git clone https://github.com/YOUR-USERNAME/post-card
11+
npm install
12+
npm test
13+
14+
Please make sure git commit messages conform to the [conventionalcommits](https://www.conventionalcommits.org/en/v1.0.0/) specification.
15+
16+
Make and commit your changes. Make sure the commands npm run build and npm run test are working and coverage remains high.
17+
18+
Finally send a [GitHub Pull Request](https://github.com/dev-warner/post-card/compare?expand=1) with a clear list of what you've done (read more [about pull requests](https://help.github.com/articles/about-pull-requests/)). Make sure all of your commits are atomic (one feature per commit).

README.md

Lines changed: 39 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# post-card
1+
# post-cards 📬
22

3-
[![test](https://github.com/dev-warner/post-card/actions/workflows/test.yml/badge.svg)](https://github.com/dev-warner/post-card/actions/workflows/test.yml)
3+
[![Coverage Status](https://coveralls.io/repos/github/dev-warner/post-card/badge.svg?branch=main)](https://coveralls.io/github/dev-warner/post-card?branch=main)
44
[![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.0-4baaaa.svg)](code_of_conduct.md)
55

6-
`PostCard` is a small node library for generating OpenGraph images for social media sharing.
6+
`@post-cards/core` is a small node library for generating OpenGraph images for social media sharing. 📬
77

8-
- [Templates]()
9-
- [Documentation]()
8+
- [Templates](https://github.com/dev-warner/post-card-templates)
9+
- [Documentation](https://dev-warner.github.io/post-card/)
1010

1111
<br>
1212

@@ -18,76 +18,38 @@
1818

1919
Simply as [opengraph.xyz](https://www.opengraph.xyz/) describes:
2020

21-
> Social networks and messaging apps use theOpen Graph meta tags to display your website.
21+
> Social networks and messaging apps use the Open Graph meta tags to display your website.
2222
2323
- [Open Graph documentation](https://ogp.me/)
2424
- [Open Graph card validator](https://www.opengraph.xyz/)
2525

2626
## Getting Started
2727

2828
```bash
29-
npm i @dev-warner/post-card -D
29+
npm i @post-cards/core -D
3030
```
3131

3232
```typescript
33-
import { PostCard } from '@dev-warner/post-card'
34-
35-
class Template {
36-
options = {
37-
styles: [
38-
`
39-
h1 {
40-
color: red;
41-
}
42-
`,
43-
],
44-
}
45-
render(item) {
46-
return `<h1>${item.data.title}</h1>`
47-
}
48-
}
49-
50-
await PostCard.create<{ title: string }>(new Template(), {
51-
output: 'media/home-page.png',
52-
data: {
53-
title: 'My great HomePage',
54-
},
55-
})
56-
```
57-
58-
## Create
59-
60-
Good for single use creation of a `Card`
61-
62-
> **WARNING: Would suggest against using this method if you're creating a lot of cards as it spins up a headless browser and closes for each call**
63-
64-
```typescript
65-
await PostCard.create<{ title: string }>(
66-
Template,
67-
{
68-
output: 'media/home-page.png',
69-
data: {
70-
title: 'My great HomePage',
71-
},
72-
},
73-
{
74-
...options,
75-
}
76-
)
77-
```
78-
79-
## Batch
80-
81-
Perfect for SSG usage as at build time you can create Open graph images for each Post/Page
82-
83-
```typescript
84-
await PostCard.batch<{ title: string }>(
85-
Template,
33+
import generate from '@post-cards/core'
34+
import BasicTemplate from '@post-cards/basic-template'
35+
36+
await generate(
37+
new BasicTemplate({
38+
text: '#fff',
39+
background: '#000',
40+
accent: 'orange',
41+
}),
8642
[
8743
{
8844
output: 'media/home-page.png',
8945
data: {
90-
title: 'My great HomePage',
46+
title: 'My great Home Page',
47+
},
48+
},
49+
{
50+
output: 'media/about-page.png',
51+
data: {
52+
title: 'My great About Page',
9153
},
9254
},
9355
],
@@ -98,13 +60,26 @@ await PostCard.batch<{ title: string }>(
9860
)
9961
```
10062

101-
## Configuration
102-
10363
### Template override
10464

65+
Sometimes you might not want the same template for each item but still want to batch them together.
66+
67+
```typescript
68+
await generate(Template, [
69+
{ output: 'media/first-image.jpg', data: {} },
70+
{
71+
output: 'media/second-image.jpg',
72+
data: {},
73+
options: { templateOveride: FancyTemplate },
74+
},
75+
])
76+
```
77+
10578
## Creating Templates
10679

107-
### Basic
80+
Creating template is an easy process, if you've ever worked with html canvas, creating templates should be a breeze, and we provide some utils to smooth out the process.
81+
82+
For more information: [Templates](https://github.com/dev-warner/post-card-templates)
10883

10984
## Contributting Guide
11085

benchmark/index.js

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,40 @@ import fs from 'fs-extra'
22
import path from 'path';
33
import faker from 'faker';
44

5-
import { PostCard } from '../dist/ost-card.es.js';
5+
import canvasNode from 'canvas';
6+
import generate from '../dist/post-card.es.js';
67

78
const BASE_PATH = './benchmark/tmp'
89

910
class Template {
1011
render(item) {
11-
return `<h1>${item.data.name}</h1>`
12+
const width = 1200
13+
const height = 630
14+
15+
const canvas = canvasNode.createCanvas(width, height)
16+
const context = canvas.getContext('2d')
17+
18+
context.fillStyle = '#000'
19+
context.fillRect(0, 0, width, height)
20+
21+
context.font = 'bold 70pt Menlo'
22+
context.textAlign = 'center'
23+
context.textBaseline = 'top'
24+
context.fillStyle = '#3574d4'
25+
26+
const text = item.data.name
27+
28+
const textWidth = context.measureText(text).width
29+
30+
context.fillRect(600 - textWidth / 2 - 10, 170 - 5, textWidth + 20, 120)
31+
context.fillStyle = '#fff'
32+
context.fillText(text, 600, 170)
33+
34+
context.fillStyle = '#fff'
35+
context.font = 'bold 30pt Menlo'
36+
context.fillText('dev-warner.io', 600, 530)
37+
38+
return canvas.toBuffer()
1239
}
1340
}
1441

@@ -22,7 +49,9 @@ async function benchmark(name, length) {
2249

2350
console.log(`Starting test: ${name} items: ${length}`)
2451
console.time(name);
25-
await PostCard.batch(new Template(), LOTS_OF_NAMES)
52+
53+
await generate(new Template(), LOTS_OF_NAMES, { concurrency: 100, verbose: true })
54+
2655
console.timeEnd(name)
2756

2857
fs.removeSync(path.join(process.cwd(), BASE_PATH))
@@ -32,6 +61,10 @@ process.on('exit', () => {
3261
fs.removeSync(path.join(process.cwd(), BASE_PATH))
3362
})
3463

64+
process.on('beforeExit', () => {
65+
fs.removeSync(path.join(process.cwd(), BASE_PATH))
66+
})
67+
3568
await benchmark('x-small', 1);
3669
await benchmark('small', 10);
3770
await benchmark('medium', 100);

lib/batch.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import PromisePool from '@supercharge/promise-pool'
2+
3+
import { create } from './create.js'
4+
5+
import type { ICardModel } from './card.js'
6+
import type { Template } from './template.js'
7+
8+
const DEFAULT_OPTIONS = Object.freeze<BatchOptions>({
9+
verbose: false,
10+
concurrency: 50,
11+
})
12+
13+
export type BatchOptions = {
14+
/** maximum amount of items created at once */
15+
concurrency: number
16+
/** logging of information */
17+
verbose: boolean
18+
}
19+
20+
/**
21+
* create many images using the specified template
22+
*
23+
* ```typescript
24+
* import generate from '@post-cards/core'
25+
*
26+
* await generate<{ title: string }>(Template, [
27+
* {
28+
* output: 'media/home-page.png',
29+
* data: {
30+
* title: 'Home Page Fun'
31+
* }
32+
* },
33+
* {
34+
* output: 'media/about-page.png',
35+
* data: {
36+
* title: 'About me'
37+
* }
38+
* }
39+
* ],
40+
* {
41+
* ... options
42+
* }
43+
* )
44+
* ```
45+
*/
46+
export async function batch<DataModel = Record<string, any>>(
47+
template: Template,
48+
items: Array<ICardModel<DataModel>> = [],
49+
options: Partial<BatchOptions> = {}
50+
) {
51+
const config = Object.assign<{}, BatchOptions, Partial<BatchOptions>>(
52+
{},
53+
DEFAULT_OPTIONS,
54+
{ ...options }
55+
)
56+
57+
const { results } = await PromisePool.for(items)
58+
.withConcurrency(config.concurrency)
59+
.handleError((err) => {
60+
throw err
61+
})
62+
.process((item) => create<DataModel>(template, item, config))
63+
64+
return results
65+
}

lib/card.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { Template } from './template.js'
2+
3+
export interface CardOptions {}
4+
5+
/**
6+
* Item Model for creating cards
7+
*
8+
* @param output - [Requried]: needed for a location to save your image.
9+
* @param data - [Required]: the data needed for your template, check the template you're using for information needed here
10+
* @param options - [Optional]: Options for configuring caputre settings,
11+
* @param templateOveride - [Optional]: Can be used to swap out a template for an item
12+
*/
13+
export interface ICardModel<DataModel = Record<string, any>> {
14+
output: string
15+
options?: CardOptions
16+
templateOveride?: Template
17+
data: DataModel
18+
}

0 commit comments

Comments
 (0)