Skip to content

Commit 6c7c301

Browse files
committed
feat: serve markdown automatically based on Accept header
1 parent 8665595 commit 6c7c301

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

.github/workflows/test.yaml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,57 @@ jobs:
3535
INTERCOM_APP_ID: ${{ secrets.INTERCOM_APP_ID }}
3636
SEGMENT_TOKEN: ${{ secrets.SEGMENT_TOKEN }}
3737

38+
- name: Install Nginx
39+
run: |
40+
sudo apt-get update
41+
sudo apt-get install -y nginx
42+
43+
- name: Start Docusaurus server
44+
run: |
45+
nohup npx docusaurus serve --port 3000 --no-open &
46+
sleep 5
47+
curl -f http://localhost:3000 > /dev/null
48+
49+
- name: Start Nginx with project config
50+
run: |
51+
cat > default.conf <<EOF
52+
worker_processes auto;
53+
events {}
54+
http {
55+
include nginx.conf;
56+
}
57+
EOF
58+
nginx -c $(pwd)/default.conf
59+
sleep 1
60+
61+
- name: Run header assertions
62+
run: |
63+
set -euo pipefail
64+
function assert_header() {
65+
url=$1
66+
header=$2
67+
expected=$3
68+
shift 3
69+
extra_args=("$@")
70+
actual=$(curl -s -D - -o /dev/null "${extra_args[@]}" "$url" | grep -i "^$header" | tr -d '\r' || true)
71+
echo "→ $url → $actual"
72+
echo "$actual" | grep -q "$expected" || (echo "❌ Expected '$expected' in '$header' for $url" && exit 1)
73+
}
74+
75+
echo "🧪 Checking Nginx responses..."
76+
77+
assert_header "http://localhost:8080/" "Content-Type" "text/html"
78+
assert_header "http://localhost:8080/" "Content-Type" "text/markdown" -H "Accept: text/markdown"
79+
assert_header "http://localhost:8080/platform/proxy/usage" "Content-Type" "text/markdown" -H "Accept: text/markdown"
80+
assert_header "http://localhost:8080/img/docs-og.png" "Content-Type" "image/svg"
81+
assert_header "http://localhost:8080/llms.txt" "Content-Type" "text/markdown"
82+
83+
echo "✅ All Nginx header checks passed."
84+
85+
- name: Stop Nginx
86+
if: always()
87+
run: nginx -s stop
88+
3889
lint_content:
3990
name: Lint markdown content
4091
runs-on: ubuntu-latest

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ This will be enough to work on Platform, Academy and OpenAPI. If you want to wor
7070
5. Add a record to `/etc/hosts`, which maps the `docs.apify.loc` to a localhost:
7171
7272
```text
73-
127.0.01 docs.apify.loc
73+
127.0.0.1 docs.apify.loc
7474
```
7575
7676
You should be able to open https://docs.apify.loc in your browser and run all the repositories jointly as one project.

nginx.conf

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,51 @@
1+
map $http_accept $wants_markdown {
2+
default 0;
3+
"~*text/markdown" 1;
4+
"~*text/plain" 1;
5+
}
6+
17
server {
28
listen 0.0.0.0:8080;
39
server_name 'docs.apify.com';
410

11+
# homepage: only rewrite to internal proxy when client wants markdown
12+
location = / {
13+
if ($wants_markdown) {
14+
# use 'last' to force a new location lookup so /__proxy/ handler runs
15+
rewrite ^ /__proxy/llms.txt last;
16+
}
17+
proxy_pass http://localhost:3000;
18+
}
19+
20+
# direct llms.txt files, serve with markdown content type
21+
location ~ ^/llms(-full)?\.txt$ {
22+
proxy_pass http://localhost:3000;
23+
proxy_hide_header Content-Type;
24+
add_header Content-Type "text/markdown; charset=utf-8" always;
25+
}
26+
27+
# generic docs fallback: rewrite clean URLs to .md when client wants markdown
528
location / {
6-
proxy_pass https://apify.github.io/apify-docs/;
29+
if ($wants_markdown) {
30+
# rewrite to internal proxy path; use last so new location is matched
31+
rewrite ^(/[^.]+)$ /__proxy$1.md last;
32+
}
33+
proxy_pass http://localhost:3000;
34+
}
35+
36+
# internal proxy handler that actually forces the markdown header
37+
location ^~ /__proxy/ {
38+
internal;
39+
40+
# strip the /__proxy prefix so upstream sees the real path
41+
rewrite ^/__proxy(/.*)$ $1 break;
42+
43+
proxy_pass http://localhost:3000;
44+
proxy_hide_header Content-Type;
45+
add_header Content-Type "text/markdown; charset=utf-8" always;
746
}
47+
48+
# proxies to other repositories
849
location /api/client/js {
950
proxy_pass https://apify.github.io/apify-client-js/;
1051
}

0 commit comments

Comments
 (0)