Skip to content

Commit

Permalink
Merge pull request #8 from BoyWithSilverWings/feature/process-emojis
Browse files Browse the repository at this point in the history
Feature/process emojis
  • Loading branch information
agneym authored Sep 21, 2019
2 parents 71da9b7 + 04a024e commit 09b16e9
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 28 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ The component currently being used is on [Github](https://github.com/BoyWithSilv

You can substitute the same with `componentUrl` input in your workflow file. For more info on creating this web component, visit [source](https://github.com/BoyWithSilverWings/generate-og-image/blob/304fd9aa0b21b01b0fdc8a3d1a63a19ffdc1840d/demo/test-file.jpg)

## Contributing

See [docs](./docs/contributors.md)

## Credits

1. [Zeit OG Image](https://github.com/zeit/og-image)
Expand Down
8 changes: 7 additions & 1 deletion __tests__/generate-html.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,15 @@ describe("Generate HTML", () => {
const result = generateHtml({
background: "linear-gradient(to right, #000428, #004e92)"
});
console.log(result);
expect(result.includes("linear-gradient(to right, #000428, #004e92)")).toBe(
true
);
});

it("process emojis", () => {
const result = generateHtml({
imageUrl: "😍"
});
expect(result.includes(`class="emoji"`)).toBe(true);
});
});
7 changes: 2 additions & 5 deletions demo/test-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
title: Just hack'n
description: Nothing to see here
ogImage:
title: Generating open graph images with Github Actions
title: Generating *open graph* images with Github Actions
subtitle: Works with Markdown files
imageUrl: https://avatars3.githubusercontent.com/u/8883368?s=40&v=4
background: "linear-gradient(to right, #000428, #004e92)"
fontColor: yellow
fontSize: 90%
imageUrl: 😍
filename: this-file
---

Expand Down
Binary file added demo/this-file.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 27 additions & 13 deletions docs/contributors.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
# Contributors

### Checkin
## Getting started

- Do checkin source (src)
- Do checkin build output (lib)
- Do checkin runtime node_modules
- Do not checkin devDependency node_modules (husky can help see below)
1. Clone the repository

### devDependencies
## Prerequisites

In order to handle correctly checking in node_modules without devDependencies, we run [Husky](https://github.com/typicode/husky) before each commit.
This step ensures that formatting and checkin rules are followed and that devDependencies are excluded. To make sure Husky runs correctly, please use the following workflow:
1. NodeJS

## Developing

```
npm install
```

Push the branch after making the changes and create a PR.

The output is the workflow named _Generate OG Images_ running in Github Actions on every PR.

## Build

Uses `ncc` to generate builds for application.

```
npm install # installs all devDependencies including Husky
git add abc.ext # Add the files you've changed. This should include files in src, lib, and node_modules (see above)
git commit -m "Informative commit message" # Commit. This will run Husky
npm install
npm run build-release
```

During the commit step, Husky will take care of formatting all files with [Prettier](https://github.com/prettier/prettier) as well as pruning out devDependencies using `npm prune --production`.
It will also make sure these changes are appropriately included in your commit (no further work is needed)
## Tests

Tests are written with `Jest`.

```
npm test
```
53 changes: 51 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"front-matter": "^3.0.2",
"lodash": "^4.17.15",
"marked": "^0.7.0",
"puppeteer-core": "^1.20.0"
"puppeteer-core": "^1.20.0",
"twemoji": "^12.1.3"
},
"devDependencies": {
"@types/jest": "^24.0.13",
Expand Down
30 changes: 24 additions & 6 deletions src/generate-html.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import marked from "marked";
import twemoji from "twemoji";

import { IRepoProps } from "./types";

Expand All @@ -9,6 +10,27 @@ function createVariables(name: string, value?: string) {
return "";
}

function getImageUrl(imageUrl?: string) {
if (!imageUrl) {
return "";
}
if (twemoji.test(imageUrl)) {
return twemoji.parse(imageUrl, {
attributes: () => ({
slot: "image"
})
});
}
return `<img slot="image" src="${imageUrl}" height="100%" />`;
}

function getMarked(text?: string) {
if (!text) {
return "";
}
return marked(text);
}

function generateHtml(prop: Partial<IRepoProps>) {
return `
<!doctype html>
Expand All @@ -32,12 +54,8 @@ function generateHtml(prop: Partial<IRepoProps>) {
</head>
<body>
<og-image-element subtitle="${prop.subtitle || ""}">
${
prop.imageUrl
? `<img slot="image" src="${prop.imageUrl}" height="100%" />`
: ``
}
<div slot="title">${marked(prop.title || "")}</div>
${getImageUrl(prop.imageUrl)}
<div slot="title">${getMarked(prop.title)}</div>
</og-image-element>
</body>
</html>
Expand Down

0 comments on commit 09b16e9

Please sign in to comment.