generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from BoyWithSilverWings/feature/generate-html
Generate HTML with Web Component
- Loading branch information
Showing
17 changed files
with
213 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1 @@ | ||
# JavaScript Action Template | ||
|
||
This template offers an easy way to get started writing a JavaScript action with TypeScript compile time support, unit testing with Jest and using the GitHub Actions Toolkit. | ||
|
||
## Getting Started | ||
|
||
See the walkthrough located [here](https://github.com/actions/toolkit/blob/master/docs/typescript-action.md). | ||
|
||
In addition to walking your through how to create an action, it also provides strategies for versioning, releasing and referencing your actions. | ||
# Open Graph Generator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,16 @@ | ||
name: "Generate OG Image" | ||
description: "Helps users generate a OG Image from frontmatter" | ||
author: "Agney" | ||
branding: | ||
icon: "share-2" | ||
color: "orange" | ||
inputs: | ||
path: | ||
description: "Path to place generated assets" | ||
default: "demo/" | ||
commitMsg: | ||
description: "commit message to be shown when adding image" | ||
default: "just some wholesome content. yo all!" | ||
runs: | ||
using: "docker" | ||
image: "Dockerfile" |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
title: Just hack'n | ||
description: Nothing to see here | ||
ogImage: | ||
title: Jack Kings | ||
subtitle: Another subtitle | ||
imageUrl: https://avatars3.githubusercontent.com/u/8883368?s=40&v=4 | ||
--- | ||
|
||
This is some text about some stuff that happened sometime ago |
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import fm from "front-matter"; | ||
import { readFileSync } from "fs"; | ||
import { PullsListFilesResponseItem } from "@octokit/rest"; | ||
|
||
import { | ||
GITHUB_CONTEXT, | ||
USER_REPO, | ||
FORMATS, | ||
REPO_DIRECTORY | ||
} from "./constants"; | ||
import octokit from "./github-api"; | ||
import { IFrontMatter, IFileProps } from "./types"; | ||
|
||
function getFileName(filename: string) { | ||
const { length: len, [len - 1]: last } = filename.split("/"); | ||
const name = last.replace(/.mdx?/, ""); | ||
return name; | ||
} | ||
|
||
function getAttributes(files: PullsListFilesResponseItem[]): IFileProps[] { | ||
return files.map(file => { | ||
const filename = file.filename; | ||
const repoDirectory = REPO_DIRECTORY as string; | ||
const contents = readFileSync(`${repoDirectory}/${filename}`, { | ||
encoding: "utf8" | ||
}); | ||
const { attributes } = fm<IFrontMatter>(contents); | ||
return { | ||
filename: getFileName(filename), | ||
attributes: Object.keys(attributes).length | ||
? { ...(attributes.ogImage || {}) } | ||
: {} | ||
}; | ||
}); | ||
} | ||
|
||
async function findFile() { | ||
const [owner, repo] = USER_REPO; | ||
const githubCtx: any = JSON.parse(GITHUB_CONTEXT as string); | ||
const pullNumber = githubCtx.event.number; | ||
const { data: filesList } = await octokit.pulls.listFiles({ | ||
owner, | ||
repo, | ||
pull_number: pullNumber | ||
}); | ||
const markdownFiles = filesList.filter(file => { | ||
return FORMATS.some(format => file.filename.endsWith(format)); | ||
}); | ||
const frontmatterAttributes = getAttributes(markdownFiles); | ||
|
||
return frontmatterAttributes.filter( | ||
frontmatterAttribute => Object.keys(frontmatterAttribute.attributes).length | ||
); | ||
} | ||
export default findFile; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { IProps } from "./types"; | ||
|
||
function generateHtml(prop: Partial<IProps>) { | ||
return ` | ||
<!doctype html> | ||
<html lang="en-GB"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<link href="https://fonts.googleapis.com/css?family=Aleo|Open+Sans&display=swap" rel="stylesheet"> | ||
<style> | ||
body { | ||
margin: 0; | ||
font-family: 'Open Sans', sans-serif; | ||
} | ||
og-image-element { | ||
--heading-font: 'Aleo', serif; | ||
} | ||
</style> | ||
<script type="module" rel="preload" src="https://unpkg.com/@agney/og-image-element@0.1.0"></script> | ||
</head> | ||
<body> | ||
<og-image-element .subtitle=${prop.subtitle || ""}> | ||
${ | ||
prop.imageUrl | ||
? `<img slot="image" src="${prop.imageUrl}" height="100%" />` | ||
: `` | ||
} | ||
<div slot="title">${prop.title}</div> | ||
</og-image-element> | ||
</body> | ||
</html> | ||
`; | ||
} | ||
|
||
export default generateHtml; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,43 @@ | ||
#!/usr/bin/env node | ||
|
||
import * as core from '@actions/core'; | ||
import { warning, debug } from "@actions/core"; | ||
|
||
import { GITHUB_TOKEN, GITHUB_EVENT_NAME } from "./constants"; | ||
import generateImage from './generate-image'; | ||
import commitFile from './commit-file'; | ||
import generateImage from "./generate-image"; | ||
import commitFile from "./commit-file"; | ||
import generateHtml from "./generate-html"; | ||
import findFile from "./find-file"; | ||
import getRepoProps from "./repo-props"; | ||
|
||
if (!GITHUB_TOKEN) { | ||
console.log("You must enable the GITHUB_TOKEN secret"); | ||
process.exit(1); | ||
} | ||
|
||
async function run() { | ||
|
||
// Bail out if the event that executed the action wasn’t a pull_request | ||
if (GITHUB_EVENT_NAME !== "pull_request") { | ||
console.log("This action only runs for pushes to PRs"); | ||
process.exit(78); | ||
} | ||
|
||
const image = await generateImage(); | ||
const repoProps = await getRepoProps(); | ||
const fileProperties = await findFile(); | ||
debug(JSON.stringify(fileProperties)); | ||
|
||
if (!fileProperties.length) { | ||
warning("No compatible files found"); | ||
} | ||
|
||
fileProperties.forEach(async property => { | ||
const html = generateHtml({ | ||
...repoProps, | ||
...property.attributes | ||
}); | ||
|
||
const image = await generateImage(html); | ||
|
||
commitFile(image); | ||
commitFile(image, repoProps, property.filename); | ||
}); | ||
} | ||
|
||
run(); |
Oops, something went wrong.