|
| 1 | +name: Sync remote markdown |
| 2 | + |
| 3 | +on: |
| 4 | + schedule: |
| 5 | + # Daily at 06:00 UTC |
| 6 | + - cron: '0 6 * * *' |
| 7 | + # Allow manual runs |
| 8 | + workflow_dispatch: {} |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: write |
| 12 | + |
| 13 | +concurrency: |
| 14 | + group: sync-remote-markdown |
| 15 | + cancel-in-progress: false |
| 16 | + |
| 17 | +jobs: |
| 18 | + sync: |
| 19 | + runs-on: ubuntu-latest |
| 20 | + steps: |
| 21 | + - name: Checkout repository |
| 22 | + uses: actions/checkout@v4 |
| 23 | + with: |
| 24 | + ref: main |
| 25 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 26 | + |
| 27 | + - name: Download remote markdown files from list and strip front matter |
| 28 | + shell: bash |
| 29 | + run: | |
| 30 | + set -euo pipefail |
| 31 | +
|
| 32 | + mkdir -p _includes |
| 33 | +
|
| 34 | + CONFIG_TXT="_includes/sources.txt" |
| 35 | +
|
| 36 | + urls=() |
| 37 | +
|
| 38 | + # Load from plain text file: one URL per non-empty, non-comment line |
| 39 | + if [[ -f "$CONFIG_TXT" ]]; then |
| 40 | + while IFS= read -r line; do |
| 41 | + # trim leading/trailing whitespace (portable) |
| 42 | + line="$(sed -E 's/^[[:space:]]+//; s/[[:space:]]+$//' <<< "$line")" |
| 43 | + [[ -z "$line" || "$line" =~ ^# ]] && continue |
| 44 | + urls+=("$line") |
| 45 | + done < "$CONFIG_TXT" |
| 46 | + fi |
| 47 | +
|
| 48 | + if [[ ${#urls[@]} -eq 0 ]]; then |
| 49 | + echo "No sources found. Add URLs to _includes/sources.txt or a YAML list in _includes/sources.yml." |
| 50 | + exit 1 |
| 51 | + fi |
| 52 | +
|
| 53 | + echo "Found ${#urls[@]} source(s)." |
| 54 | +
|
| 55 | + download_and_process() { |
| 56 | + local url="$1" |
| 57 | +
|
| 58 | + # Convert GitHub UI blob URLs to raw URLs if needed |
| 59 | + if [[ "$url" =~ ^https://github.com/([^/]+)/([^/]+)/blob/([^/]+)/(.*)$ ]]; then |
| 60 | + url="https://raw.githubusercontent.com/${BASH_REMATCH[1]}/${BASH_REMATCH[2]}/${BASH_REMATCH[3]}/${BASH_REMATCH[4]}" |
| 61 | + base_name="$(basename "${BASH_REMATCH[4]}")" |
| 62 | + else |
| 63 | + base_name="$(basename "$url")" |
| 64 | + fi |
| 65 | +
|
| 66 | + # Ensure it ends with .md for saving |
| 67 | + if [[ ! "$base_name" =~ \.md$ ]]; then |
| 68 | + base_name="${base_name}.md" |
| 69 | + fi |
| 70 | +
|
| 71 | + tmp_file=$(mktemp) |
| 72 | + out_file="_includes/${base_name}" |
| 73 | +
|
| 74 | + echo "Downloading: $url -> $out_file" |
| 75 | + curl -fsSL "$url" -o "$tmp_file" |
| 76 | +
|
| 77 | + # Strip a single YAML front-matter block only if it is at the very top of the file |
| 78 | + awk ' |
| 79 | + NR==1 && $0 ~ /^---[[:space:]]*$/ { infront=1; next } |
| 80 | + infront && $0 ~ /^---[[:space:]]*$/ { infront=0; next } |
| 81 | + infront { next } |
| 82 | + { print } |
| 83 | + ' "$tmp_file" > "$out_file" |
| 84 | +
|
| 85 | + rm -f "$tmp_file" |
| 86 | + } |
| 87 | +
|
| 88 | + for u in "${urls[@]}"; do |
| 89 | + download_and_process "$u" |
| 90 | + done |
| 91 | +
|
| 92 | + - name: Commit and push if changes detected |
| 93 | + run: | |
| 94 | + git add _includes |
| 95 | + if git diff --staged --quiet; then |
| 96 | + echo "No changes to commit." |
| 97 | + exit 0 |
| 98 | + fi |
| 99 | + git config user.name "github-actions[bot]" |
| 100 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 101 | + git commit -m "chore: sync remote Markdown from sources list into _includes" |
| 102 | + git push origin main |
0 commit comments