This guide explains how to create an iOS Shortcut that uploads a Markdown file to a GitHub repository using the GitHub API.
Steps to Create the Shortcut
1. Prepare Your GitHub Personal Access Token
- Go to GitHub Developer Settings.
- Generate a Personal Access Token with the following permissions:
-
repo
(for private repositories). -
public_repo
(for public repositories).
-
- Save the token securely, as it will be used in the Shortcut.
2. Create a New Shortcut
Open the Shortcuts app on your iPhone or iPad and follow these steps:
Step 1: Add Input (Markdown Content)
-
Use "Ask for Input" to get the Markdown file path and name:
- Prompt:
Enter file path (e.g., posts/article1.md)
. - Save this as a variable (e.g.,
FilePath
).
- Prompt:
-
Use "Get File" to select the Markdown content:
- Use the
File
action to allow the user to pick a Markdown file. - Choose
Open file
and save its contents into a variable (e.g.,MarkdownContent
).
- Use the
Step 2: Encode the Content in Base64
-
Add "Base64 Encode" action:
- Input:
MarkdownContent
. - Save the Base64-encoded content as a variable (e.g.,
EncodedContent
).
- Input:
-
Add "Replace Text" to remove newlines (
\n
) from the encoded content:- Replace:
\n
. - With: (leave blank).
- Save the result back to
EncodedContent
.
- Replace:
Step 3: Construct the GitHub API URL
-
Add a "Text" action:
- Input:
https://api.github.com/repos/{owner}/{repo}/contents/{FilePath}
- Replace
{owner}
with your GitHub username. - Replace
{repo}
with your repository name. - Replace
{FilePath}
with the variableFilePath
.
- Save the result as a variable (e.g.,
GitHubURL
).
Step 4: Construct the Request Body
-
Add a "Dictionary" action:
- Key:
message
→ Value:Add new post
. - Key:
content
→ Value:EncodedContent
. - Optional: Add
branch
→ Value:main
(if uploading to themain
branch).
- Key:
Save this dictionary as
RequestBody
.
Step 5: Send the Request to GitHub
-
Add a "Get Contents of URL" action:
- Method:
PUT
. - URL:
GitHubURL
. - Headers:
-
Authorization
:token YOUR_PERSONAL_ACCESS_TOKEN
. -
Content-Type
:application/json
.
-
- Body:
RequestBody
.
- Method:
Add error handling to check the response.
3. Test the Shortcut
Run the Shortcut:
- Input the file path (e.g.,
posts/article1.md
). - Select a Markdown file from your device.
- The Shortcut will upload the file to the specified GitHub repository.
Troubleshooting
- Ensure your GitHub token has the correct permissions.
- Check the Base64-encoded content for unwanted line breaks.
- Verify the repository path and branch exist.
- Debug using the "Show Result" action to inspect variables at each step.
This Shortcut will dynamically handle file paths, encode content, and upload Markdown files directly to your GitHub repository.