Skip to content

Commit

Permalink
- added post_url for better extending of post sub-url
Browse files Browse the repository at this point in the history
- added example of dynamic usage of action based on committed files
- fixed Uncaught TypeError when no base_url specified
  • Loading branch information
maZahaca committed Feb 12, 2021
1 parent 8e55746 commit e3813fb
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
53 changes: 51 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ Github Action for posting a markdown post to medium.com

Base blog's url e.g. https://myblog.com

### `post_url`

Base blog's post url e.g. https://myblog.com/posts. If not specified, `base_url` is used.

### `post_status`

Post's status. Valid values are `draft`, `public`, or `unlisted`. Default `draft`.
Expand All @@ -42,8 +46,10 @@ ID of the created post.
Medium URL of the created post.

## Example usage
Let's assume the post markdown file is located at `./content/post.md`.

### Post content from static file

Let's assume the post markdown file is located at `./content/post.md`.
```yaml
name: publish-to-medium
on: [push]
Expand All @@ -55,10 +61,53 @@ jobs:
- name: Read the post
id: post
run: echo "::set-output name=data::$(cat ./content/post.md)"
- uses: infraway/medium-post-markdown@v1
- uses: infraway/medium-post-markdown@v1.3.0
with:
app_id: ${{ secrets.MEDIUM_APP_ID }}
app_secret: ${{ secrets.MEDIUM_APP_SECRET }}
access_token: ${{ secrets.MEDIUM_ACCESS_TOKEN }}
markdown: ${{ steps.post.outputs.data }}
```
### Post content from newly committed file
This example gets files from commit, find blog posts and publish it to Medium.
- `steps.files.outputs.added_modified` extracts add+modified files. If you need added only, use `steps.files.outputs.added` instead.
- `content/posts` - a repo folder, which contains markdown posts. Replace with your own.
- `steps.posts.outputs.post0` - markdown posts path, it will contain as many as you have added `post0`, `post1`, `post2`.

```yaml
name: publish-to-medium
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- id: files
uses: jitterbit/get-changed-files@v1
- id: posts
name: Detecting posts from the changes
run: |
i=0
for changed_file in ${{ steps.files.outputs.added_modified }}; do
echo "Do something with ${changed_file}."
if [[ "${changed_file}" == "content/posts"* ]];
then
echo "File ${changed_file} matched post."
echo "::set-output name=post${i}::${changed_file}"
((i=i+1))
fi
done
- if: steps.posts.outputs.post0
name: Publish to medium
uses: infraway/post-medium-action@v1.3.0
with:
app_id: ${{ secrets.MEDIUM_APP_ID }}
app_secret: ${{ secrets.MEDIUM_APP_SECRET }}
access_token: ${{ secrets.MEDIUM_ACCESS_TOKEN }}
markdown_file: ${{ steps.posts.outputs.post0 }}
base_url: https://myblog.com
post_url: https://myblog.com/posts
```
6 changes: 5 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ inputs:
description: "Markdown post file"
required: true
base_url:
description: "Base blog's URL"
description: "Base blog's URL. Example: https://myblog"
required: false
default: ""
post_url:
description: "Base blog's post URL. Example: https://myblog/posts"
required: false
default: ""
post_status:
Expand Down
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {

INPUT_MARKDOWN_FILE,
INPUT_BASE_URL,
INPUT_POST_URL,
INPUT_POST_STATUS = sdk.PostPublishStatus.DRAFT,
INPUT_POST_LICENSE = sdk.PostLicense.ALL_RIGHTS_RESERVED,
} = process.env;
Expand Down Expand Up @@ -61,6 +62,9 @@ const getFileContents = async (filepath) => {
};

const replaceLocalLinks = (content) => {
if (!INPUT_BASE_URL) {
return content;
}
return content.replace(/\]\((\/[^\)]+)\)/gi, `](${INPUT_BASE_URL}$1)`)
};

Expand All @@ -70,7 +74,8 @@ const replaceLocalLinks = (content) => {
const { id } = await getUser();
const { meta, markdown } = md(replaceLocalLinks(data));
const { title, tags = [], slug } = meta || {};
const postUrl = `${INPUT_BASE_URL}/posts/${slug}`;
const postBaseUrl = INPUT_POST_URL || INPUT_BASE_URL || '';
const postUrl = [postBaseUrl.replace(/\/+$/, ''), slug].join('/');
const post = await createPost(id, postUrl, title, tags, markdown);
console.log(`::set-output name=id::${post.id}`);
console.log(`::set-output name=url::${post.url}`);
Expand Down

0 comments on commit e3813fb

Please sign in to comment.