Skip to content

Commit 5f3dc0c

Browse files
Add Medium Integration
1 parent 5149893 commit 5f3dc0c

File tree

5 files changed

+61
-17
lines changed

5 files changed

+61
-17
lines changed

README.md

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,20 @@ If a tree falls in the woods, does it make a sound?
55
Publish your repo's markdown to multiple distributors with a simple `git push`, so that when your tree falls, it makes a sound.
66

77
## Current Integration
8-
* [DEV](https://dev.to)
8+
9+
- [DEV](https://dev.to)
910

1011
Create an Issue if you have another distributor that you'd like to add to this git action.
1112

1213
## Arguments
1314

14-
| Input | Description | Usage |
15-
| :---: | :---: | :---: |
16-
| `github-token` | GitHub Auth Token | *Required* |
17-
| `content-dir` | Path from the root to your markdown files. Defaults to `./content/articles/` | Optional |
18-
| `dev-to-token` | API token for dev.to. (https://dev.to/settings/account) | *Required* |
15+
| Input | Description | Usage |
16+
| :----------------: | :---------------------------------------------------------------------------------------------------------: | :--------: |
17+
| `github-token` | GitHub Auth Token | _Required_ |
18+
| `content-dir` | Path from the root to your markdown files. Defaults to `./content/articles/` | Optional |
19+
| `dev-to-token` | API token for dev.to. (https://dev.to/settings/account) | _Required_ |
20+
| `medium-token` | API token for medium.to. (https://medium.com/me/settings) | _Optional_ |
21+
| `medium-author-id` | You can access your author id from the (User Endpoint)[https://github.com/Medium/medium-api-docs#31-users]. | _Optional_ |
1922

2023
## Example usage
2124

@@ -25,20 +28,20 @@ name: CrossPost
2528
on:
2629
push:
2730
paths:
28-
- 'content/articles/*.md'
31+
- 'content/articles/*.md'
2932

3033
jobs:
3134
crosspost:
3235
runs-on: ubuntu-latest
3336
steps:
34-
- name: Checkout Code
35-
uses: actions/checkout@v2
36-
37-
- uses: basicBrogrammer/[email protected]
38-
with:
39-
content-dir: 'content/articles/'
40-
dev-to-token: ${{ secrets.DEV_TO }}
41-
github-token: ${{ secrets.GITHUB_TOKEN }}
37+
- name: Checkout Code
38+
uses: actions/checkout@v2
39+
40+
- uses: basicBrogrammer/[email protected]
41+
with:
42+
content-dir: 'content/articles/'
43+
dev-to-token: ${{ secrets.DEV_TO }}
44+
github-token: ${{ secrets.GITHUB_TOKEN }}
4245
```
4346
4447
## Contribute

action.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ inputs:
1515
dev-to-token:
1616
description: 'API Token for dev.to'
1717
required: false
18+
medium-token:
19+
description: 'API Token for medium.com'
20+
required: false
21+
medium-author-id:
22+
description: 'Author id from medium.com'
23+
required: false
1824
runs:
1925
using: 'node12'
2026
main: 'dist/index.js'

src/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as core from '@actions/core';
22
import {getFiles} from './get-files';
33
import DevTo from './publish/dev-to';
4+
import Medium from './publish/medium';
45

56
async function run(): Promise<void> {
67
try {
@@ -9,6 +10,7 @@ async function run(): Promise<void> {
910
const files = await getFiles();
1011
files.forEach((path: string) => {
1112
new DevTo(path, 'dev-to-token').publish();
13+
new Medium(path, 'medium-token').publish();
1214
});
1315
} catch (error) {
1416
core.setFailed(error.message);

src/publish/medium.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import fetch from 'node-fetch';
2+
import {Response} from 'node-fetch';
3+
import Publisher from './publisher';
4+
import * as core from '@actions/core';
5+
6+
class Medium extends Publisher {
7+
_publish(): Promise<Response> {
8+
const body = {
9+
title: this.data.title,
10+
contentFormat: 'markdown',
11+
content: this.content,
12+
tags: this.data.tags.split(',').map((tag: string) => tag.trim()),
13+
publishStatus: 'public',
14+
};
15+
16+
return fetch(`https://api.medium.com/v1/users/${this.authorId}/posts`, {
17+
method: 'post',
18+
body: JSON.stringify(body),
19+
headers: {
20+
'Content-Type': 'application/json',
21+
Authorization: `Bearer ${this.token}`,
22+
},
23+
});
24+
}
25+
26+
get authorId(): string {
27+
return core.getInput('medium-author-id');
28+
}
29+
}
30+
31+
export default Medium;

src/publish/publisher.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default class Publisher {
77
token: string;
88
markdown: string = 'not-configured';
99
data: any = null;
10-
content: string | null = null;
10+
content: string = 'no content';
1111

1212
constructor(path: string, tokenKey: string) {
1313
this.token = core.getInput(tokenKey);
@@ -27,7 +27,9 @@ export default class Publisher {
2727
if (!this._isConfigured) return;
2828

2929
if (this.data?.published) {
30-
this._publish().then(this._logResponse);
30+
this._publish()
31+
.then(this._logResponse)
32+
.catch((e) => console.log(e));
3133
} else {
3234
console.log(`Article ${this.data?.title} NOT published. Skipping.`);
3335
}

0 commit comments

Comments
 (0)