Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ If a tree falls in the woods, does it make a sound?
Publish your repo's markdown to multiple distributors with a simple `git push`, so that when your tree falls, it makes a sound.

## Current Integration
* [DEV](https://dev.to)

- [DEV](https://dev.to)

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

## Arguments

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

## Example usage

Expand All @@ -25,20 +28,20 @@ name: CrossPost
on:
push:
paths:
- 'content/articles/*.md'
- 'content/articles/*.md'

jobs:
crosspost:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v2

- uses: basicBrogrammer/crosspost-markdown@v0.1.1
with:
content-dir: 'content/articles/'
dev-to-token: ${{ secrets.DEV_TO }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Checkout Code
uses: actions/checkout@v2

- uses: basicBrogrammer/crosspost-markdown@v0.1.1
with:
content-dir: 'content/articles/'
dev-to-token: ${{ secrets.DEV_TO }}
github-token: ${{ secrets.GITHUB_TOKEN }}
```

## Contribute
Expand Down
12 changes: 9 additions & 3 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"@actions/core": "^1.2.6",
"@actions/github": "^4.0.0",
"@github-docs/frontmatter": "^1.3.1",
"@octokit/webhooks": "^7.21.0",
"@octokit/webhooks": "^8.4.1",
"@types/node-fetch": "^2.5.8",
"node-fetch": "^2.6.1"
},
Expand Down
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as core from '@actions/core';
import {getFiles} from './get-files';
import DevTo from './publish/dev-to';
import Medium from './publish/medium';

async function run(): Promise<void> {
try {
Expand All @@ -9,6 +10,7 @@ async function run(): Promise<void> {
const files = await getFiles();
files.forEach((path: string) => {
new DevTo(path, 'dev-to-token').publish();
new Medium(path, 'medium-token').publish();
});
} catch (error) {
core.setFailed(error.message);
Expand Down
31 changes: 31 additions & 0 deletions src/publish/medium.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import fetch from 'node-fetch';
import {Response} from 'node-fetch';
import Publisher from './publisher';
import * as core from '@actions/core';

class Medium extends Publisher {
_publish(): Promise<Response> {
const body = {
title: this.data.title,
contentFormat: 'markdown',
content: this.content,
tags: this.data.tags.split(',').map((tag: string) => tag.trim()),
publishStatus: 'public',
};

return fetch(`https://api.medium.com/v1/users/${this.authorId}/posts`, {
method: 'post',
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${this.token}`,
},
});
}

get authorId(): string {
return core.getInput('medium-author-id');
}
}

export default Medium;
6 changes: 4 additions & 2 deletions src/publish/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default class Publisher {
token: string;
markdown: string = 'not-configured';
data: any = null;
content: string | null = null;
content: string = 'no content';

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

if (this.data?.published) {
this._publish().then(this._logResponse);
this._publish()
.then(this._logResponse)
.catch((e) => console.log(e));
} else {
console.log(`Article ${this.data?.title} NOT published. Skipping.`);
}
Expand Down