Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: publish blogs automatically from github to hashnode #305

Closed
wants to merge 16 commits into from
37 changes: 37 additions & 0 deletions .github/workflows/article-publisher.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Articles Publisher

on:
pull_request:
types: [closed]

env:
HASHNODE_API_TOKEN: ${{ secrets.HASHNODE_API_TOKEN }}
HASHNODE_PUBLICATION_ID: ${{ secrets.HASHNODE_PUBLICATION_ID }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BRANCH_NAME: "develop"

jobs:
article-publisher:
if: github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'develop'
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v3

- name: set up Node.js
uses: actions/setup-node@v3
with:
node-version: "20"

- name: install deps and build
run: |
cd ./publisher
npm install
npm run build
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Build should have happened in the previous build. I think we can just wait for it here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I don't quite get which build do you mean, if you mean build-and-deploy in the CI workflow here's why I didn't go with that approach :

I actually don't want this job to depend on build-and-deploy because build-and-deploy runs on every push to any branch.

Blog publishing will only work for merged PRs, in case you want to do editing or other peers want to double check the blog before publishing it.

So in this workflow we only download the dependencies necessary for publishing blogs and run the script to do it, we don't really need to wait on build-and-deploy.


- name: run publisher
run: |
node ./publish_articles.js

- name: (AUTO COMMIT) Commit and Push changes
uses: mikeal/publish-to-github-action@master
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Dependencies
/node_modules
/publisher/node_modules

# Production
/build
/publisher/dist

# Generated files
.docusaurus
Expand Down
4,930 changes: 1,740 additions & 3,190 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"name": "my-website",
"workspaces": [
"publisher"
],
"version": "0.0.0",
"private": true,
"scripts": {
Expand Down
9 changes: 9 additions & 0 deletions publish_articles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const publisher = require("publisher")
const path = require("path")

function main() {
let posts_directory = path.join(__dirname, "blog")
publisher.init(posts_directory)
}

main()
17 changes: 17 additions & 0 deletions publisher/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "publisher",
"version": "1.0.0",
"main": "dist/main.js",
"scripts": {
"dev": "ts-node main.ts",
"build": "tsc",
"start": "node dist/main.js"
},
"dependencies": {
"axios": "^1.7.2",
"gray-matter": "^4.0.3",
"slugify": "^1.6.6",
"ts-node": "^10.9.2",
"typescript": "^5.5.3"
}
}
64 changes: 64 additions & 0 deletions publisher/src/domain/article.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
export interface SEO {
title: string | null
description: string | null
}

export interface Article {
title: string
subtitle: string | null
cover: string | null
tags: string[]
slug: string
content: string
canonicalUrl: string | null
seo: SEO | null
}

export class ArticleBuilder {
private article: Partial<Article>

constructor(title: string, slug: string, content: string) {
this.article = {
title,
slug,
content,
tags: [],
subtitle: null,
cover: null,
canonicalUrl: null,
seo: null,
}
}

withSubtitle(subtitle: string | null): ArticleBuilder {
this.article.subtitle = subtitle
return this
}

withCover(cover: string | null): ArticleBuilder {
this.article.cover = cover
return this
}

withTags(tags: string[]): ArticleBuilder {
this.article.tags = tags
return this
}

withCanonicalUrl(canonicalUrl: string | null): ArticleBuilder {
this.article.canonicalUrl = canonicalUrl
return this
}

withSEO(seo: SEO | null): ArticleBuilder {
this.article.seo = seo
return this
}

build(): Article {
if (!this.article.title || !this.article.slug || !this.article.content) {
throw new Error("Title, slug, and content are required.")
}
return this.article as Article
}
}
24 changes: 24 additions & 0 deletions publisher/src/domain/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import HashnodePublisher from "../integrations/hashnode_publisher"
import Publisher from "./publisher"

export interface Config {
posts_directory: string
publishers: Array<Publisher>
}

export function buildConfig(posts_directory: string): Config {
let config: Config = {
posts_directory,
publishers: [],
}

let hashnode_token = process.env.HASHNODE_API_TOKEN
let hashnode_publication_id = process.env.HASHNODE_PUBLICATION_ID
let hashnode_enabled = hashnode_token !== undefined && hashnode_publication_id !== undefined

if (hashnode_enabled) {
config.publishers.push(new HashnodePublisher(hashnode_token as string, hashnode_publication_id as string))
}

return config
}
9 changes: 9 additions & 0 deletions publisher/src/domain/publisher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {Article} from "./article"

export default interface Publisher {
publishDraft(article: Article): Promise<void>
publish(article: Article): Promise<string>
edit(id: string, article: Article): Promise<void>
getContentId(matter_data: {[key: string]: any}): string
addContentId(matter_data: {[key: string]: any}, id: string): {[key: string]: any}
}
Loading
Loading