Skip to content

Commit 3dec09f

Browse files
committed
feature:(devto auto publish via github actions)
1 parent 75e6a14 commit 3dec09f

File tree

4 files changed

+233
-11
lines changed

4 files changed

+233
-11
lines changed

.github/workflows/syndicate.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: syndicate
2+
on:
3+
push:
4+
branches:
5+
- main
6+
workflow_dispatch:
7+
jobs:
8+
blog-dev-to:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v3
12+
- uses: actions/setup-node@v4
13+
name: Install node
14+
with:
15+
node-version: 18
16+
- uses: pnpm/action-setup@v2
17+
name: Install pnpm
18+
with:
19+
version: 8
20+
run_install: false
21+
- name: Get pnpm store directory
22+
shell: bash
23+
run: |
24+
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
25+
- uses: actions/cache@v3
26+
name: Setup pnpm cache
27+
with:
28+
path: ${{ env.STORE_PATH }}
29+
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
30+
restore-keys: |
31+
${{ runner.os }}-pnpm-store-
32+
- name: Install dependencies
33+
working-directory: ./apps/codingcatdev
34+
run: pnpm i
35+
- name: syndicate:dev-to
36+
working-directory: ./apps/codingcatdev/scripts
37+
run: node dev-to.js
38+
env:
39+
PRIVATE_DEVTO: ${{ secrets.PRIVATE_DEVTO }}
40+
- uses: stefanzweifel/git-auto-commit-action@v4
41+
with:
42+
commit_message: Shrink images

apps/codingcatdev/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
"feed": "^4.2.2",
3737
"firebase-admin": "^11.11.0",
3838
"flexsearch": "^0.7.31",
39+
"glob": "^10.3.10",
40+
"gray-matter": "^4.0.3",
3941
"marked": "^9.0.3",
4042
"mdsvex": "^0.11.0",
4143
"postcss": "^8.4.31",
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* You can test this using act
3+
* run act -s PRIVATE_DEVTO=yourapikey
4+
*/
5+
6+
import { Glob } from 'glob';
7+
import matter from 'gray-matter';
8+
import fs from 'fs';
9+
10+
const TYPE = 'post';
11+
const BASE = `../src/routes/(content-single)/(non-course)/${TYPE}/`;
12+
const g = new Glob(`${BASE}**/*.md`, {});
13+
14+
const addArticle = async (data) => {
15+
return fetch('https://dev.to/api/articles/', {
16+
method: 'POST',
17+
headers: {
18+
'api-key': process.env.PRIVATE_DEVTO,
19+
'Content-Type': 'application/json'
20+
},
21+
body: JSON.stringify(data)
22+
});
23+
};
24+
25+
for await (const file of g) {
26+
const mdFile = fs.readFileSync(file, { encoding: 'utf8', flag: 'r' });
27+
const { data, content } = await matter(mdFile); // data has frontmatter, code is html
28+
const fm = data;
29+
if (!fm) continue; // Skip if missing frontmattter
30+
if (
31+
fm?.slug &&
32+
fm?.slug === 'the-myth-of-just-press-record-in-podcasting' &&
33+
fm?.title &&
34+
fm?.cover &&
35+
fm?.published === 'published' &&
36+
new Date(fm?.start) < new Date() &&
37+
!fm?.devto
38+
) {
39+
console.log('Adding', { slug: fm?.slug, devto: fm?.devto });
40+
41+
try {
42+
console.log('addArticle to devto');
43+
const response = await addArticle({
44+
article: {
45+
title: fm.title,
46+
published: false,
47+
tags: ['podcast', 'webdev', 'javascript', 'beginners'],
48+
main_image: fm.cover.replace('upload/', 'upload/b_rgb:5e1186,c_pad,w_1000,h_420/'),
49+
canonical_url: `https://codingcat.dev/${TYPE}/${fm.slug}`,
50+
description: fm?.excerpt || '',
51+
organization_id: '1009',
52+
body_markdown: content
53+
}
54+
});
55+
console.log('addArticle result:', response.status);
56+
57+
// Get new devto url and update
58+
if (response.status === 201) {
59+
const { url } = response.json();
60+
if (url) {
61+
const newMdFile = matter.stringify(content, {
62+
...data,
63+
devto: url
64+
});
65+
console.log('Updating', file, { devto: url });
66+
fs.writeFileSync(file, newMdFile, { encoding: 'utf8', flag: 'r' });
67+
}
68+
}
69+
} catch (error) {
70+
console.error(error);
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)