Skip to content

Commit 3b12e76

Browse files
committed
feat: serve markdown automatically based on Accept header for child repositories
Quite a bit of duplication, couldn't find a way to make it generic without delegating it to a separate file.
1 parent 552fe44 commit 3b12e76

File tree

2 files changed

+219
-20
lines changed

2 files changed

+219
-20
lines changed

.github/workflows/test.yaml

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ jobs:
7777
echo "$actual" | grep -q "$expected" || (echo "❌ Expected '$expected' in '$header' for $url" && exit 1)
7878
}
7979
80-
echo "🧪 Checking Nginx responses..."
81-
80+
echo "🧪 Checking Nginx responses... (apify-docs)"
8281
assert_header "http://localhost:8080/" "Content-Type" "text/html"
8382
assert_header "http://localhost:8080/" "Content-Type" "text/markdown" -H "Accept: text/markdown"
8483
assert_header "http://localhost:8080/platform/proxy/usage" "Content-Type" "text/html"
@@ -89,6 +88,51 @@ jobs:
8988
assert_header "http://localhost:8080/llms.txt" "Content-Type" "text/markdown"
9089
assert_header "http://localhost:8080/llms-full.txt" "Content-Type" "text/markdown"
9190
91+
echo "🧪 Checking Nginx responses... (apify-sdk-js)"
92+
assert_header "http://localhost:8080/sdk/js" "Content-Type" "text/html"
93+
assert_header "http://localhost:8080/sdk/js" "Content-Type" "text/markdown" -H "Accept: text/markdown"
94+
assert_header "http://localhost:8080/sdk/js/docs/guides/apify-platform" "Content-Type" "text/html"
95+
assert_header "http://localhost:8080/sdk/js/docs/guides/apify-platform.md" "Content-Type" "text/markdown"
96+
assert_header "http://localhost:8080/sdk/js/docs/guides/apify-platform" "Content-Type" "text/markdown" -H "Accept: text/markdown"
97+
assert_header "http://localhost:8080/sdk/js/llms.txt" "Content-Type" "text/markdown"
98+
assert_header "http://localhost:8080/sdk/js/llms-full.txt" "Content-Type" "text/markdown"
99+
100+
echo "🧪 Checking Nginx responses... (apify-sdk-python)"
101+
assert_header "http://localhost:8080/sdk/python" "Content-Type" "text/html"
102+
assert_header "http://localhost:8080/sdk/python" "Content-Type" "text/markdown" -H "Accept: text/markdown"
103+
assert_header "http://localhost:8080/sdk/python/docs/changelog" "Content-Type" "text/html"
104+
assert_header "http://localhost:8080/sdk/python/docs/changelog.md" "Content-Type" "text/markdown"
105+
assert_header "http://localhost:8080/sdk/python/docs/changelog" "Content-Type" "text/markdown" -H "Accept: text/markdown"
106+
assert_header "http://localhost:8080/sdk/python/llms.txt" "Content-Type" "text/markdown"
107+
assert_header "http://localhost:8080/sdk/python/llms-full.txt" "Content-Type" "text/markdown"
108+
109+
echo "🧪 Checking Nginx responses... (apify-client-js)"
110+
assert_header "http://localhost:8080/api/client/js" "Content-Type" "text/html"
111+
assert_header "http://localhost:8080/api/client/js" "Content-Type" "text/markdown" -H "Accept: text/markdown"
112+
assert_header "http://localhost:8080/api/client/js/docs/changelog" "Content-Type" "text/html"
113+
assert_header "http://localhost:8080/api/client/js/docs/changelog.md" "Content-Type" "text/markdown"
114+
assert_header "http://localhost:8080/api/client/js/docs/changelog" "Content-Type" "text/markdown" -H "Accept: text/markdown"
115+
assert_header "http://localhost:8080/api/client/js/llms.txt" "Content-Type" "text/markdown"
116+
assert_header "http://localhost:8080/api/client/js/llms-full.txt" "Content-Type" "text/markdown"
117+
118+
echo "🧪 Checking Nginx responses... (apify-client-python)"
119+
assert_header "http://localhost:8080/api/client/python" "Content-Type" "text/html"
120+
assert_header "http://localhost:8080/api/client/python" "Content-Type" "text/markdown" -H "Accept: text/markdown"
121+
assert_header "http://localhost:8080/api/client/python/docs/changelog" "Content-Type" "text/html"
122+
assert_header "http://localhost:8080/api/client/python/docs/changelog.md" "Content-Type" "text/markdown"
123+
assert_header "http://localhost:8080/api/client/python/docs/changelog" "Content-Type" "text/markdown" -H "Accept: text/markdown"
124+
assert_header "http://localhost:8080/api/client/python/llms.txt" "Content-Type" "text/markdown"
125+
assert_header "http://localhost:8080/api/client/python/llms-full.txt" "Content-Type" "text/markdown"
126+
127+
echo "🧪 Checking Nginx responses... (apify-cli)"
128+
assert_header "http://localhost:8080/cli" "Content-Type" "text/html"
129+
assert_header "http://localhost:8080/cli" "Content-Type" "text/markdown" -H "Accept: text/markdown"
130+
assert_header "http://localhost:8080/cli/docs/changelog" "Content-Type" "text/html"
131+
assert_header "http://localhost:8080/cli/docs/changelog.md" "Content-Type" "text/markdown"
132+
assert_header "http://localhost:8080/cli/docs/changelog" "Content-Type" "text/markdown" -H "Accept: text/markdown"
133+
assert_header "http://localhost:8080/cli/llms.txt" "Content-Type" "text/markdown"
134+
assert_header "http://localhost:8080/cli/llms-full.txt" "Content-Type" "text/markdown"
135+
92136
echo "✅ All Nginx header checks passed."
93137
94138
- name: Stop Nginx

nginx.conf

Lines changed: 173 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,177 @@ server {
4444
proxy_pass $backend$proxy_path;
4545
}
4646

47-
# proxies to other repositories
48-
location /api/client/js {
47+
### Repository path: "/sdk/js"
48+
49+
location = /sdk/js {
50+
if ($serve_markdown) {
51+
rewrite ^ /sdk-js-llms.txt last;
52+
}
53+
proxy_pass https://apify.github.io/apify-sdk-js/;
54+
}
55+
location = /sdk/js/ {
56+
if ($serve_markdown) {
57+
rewrite ^ /sdk-js-llms.txt last;
58+
}
59+
proxy_pass https://apify.github.io/apify-sdk-js/;
60+
}
61+
62+
location = /sdk-js-llms.txt {
63+
proxy_hide_header Content-Type;
64+
add_header Content-Type 'text/markdown; charset=utf-8' always;
65+
proxy_pass https://apify.github.io/apify-sdk-js/llms.txt;
66+
}
67+
68+
location ~ ^/sdk/js/(.*)$ {
69+
set $path_suffix $1;
70+
set $proxy_path "/$path_suffix";
71+
72+
set $rewrite_condition "$serve_markdown$has_no_extension";
73+
74+
if ($rewrite_condition = "11") {
75+
set $proxy_path "${proxy_path}.md";
76+
}
77+
78+
proxy_pass https://apify.github.io/apify-sdk-js$proxy_path;
79+
}
80+
81+
### Repository path: "/sdk/python"
82+
83+
location = /sdk/python {
84+
if ($serve_markdown) {
85+
rewrite ^ /sdk-python-llms.txt last;
86+
}
87+
proxy_pass https://apify.github.io/apify-sdk-python/;
88+
}
89+
location = /sdk/python/ {
90+
if ($serve_markdown) {
91+
rewrite ^ /sdk-python-llms.txt last;
92+
}
93+
proxy_pass https://apify.github.io/apify-sdk-python/;
94+
}
95+
96+
location = /sdk-python-llms.txt {
97+
proxy_hide_header Content-Type;
98+
add_header Content-Type 'text/markdown; charset=utf-8' always;
99+
proxy_pass https://apify.github.io/apify-sdk-python/llms.txt;
100+
}
101+
102+
location ~ ^/sdk/python/(.*)$ {
103+
set $path_suffix $1;
104+
set $proxy_path "/$path_suffix";
105+
106+
set $rewrite_condition "$serve_markdown$has_no_extension";
107+
108+
if ($rewrite_condition = "11") {
109+
set $proxy_path "${proxy_path}.md";
110+
}
111+
112+
proxy_pass https://apify.github.io/apify-sdk-python$proxy_path;
113+
}
114+
115+
### Repository path: "/api/client/js"
116+
117+
location = /api/client/js {
118+
if ($serve_markdown) {
119+
rewrite ^ /client-js-llms.txt last;
120+
}
121+
proxy_pass https://apify.github.io/apify-client-js/;
122+
}
123+
location = /api/client/js/ {
124+
if ($serve_markdown) {
125+
rewrite ^ /client-js-llms.txt last;
126+
}
49127
proxy_pass https://apify.github.io/apify-client-js/;
50128
}
51-
location /api/client/python {
129+
130+
location = /client-js-llms.txt {
131+
proxy_hide_header Content-Type;
132+
add_header Content-Type 'text/markdown; charset=utf-8' always;
133+
proxy_pass https://apify.github.io/apify-client-js/llms.txt;
134+
}
135+
136+
location ~ ^/api/client/js/(.*)$ {
137+
set $path_suffix $1;
138+
set $proxy_path "/$path_suffix";
139+
140+
set $rewrite_condition "$serve_markdown$has_no_extension";
141+
142+
if ($rewrite_condition = "11") {
143+
set $proxy_path "${proxy_path}.md";
144+
}
145+
146+
proxy_pass https://apify.github.io/apify-client-js$proxy_path;
147+
}
148+
149+
### Repository path: "/api/client/python"
150+
151+
location = /api/client/python {
152+
if ($serve_markdown) {
153+
rewrite ^ /client-python-llms.txt last;
154+
}
52155
proxy_pass https://apify.github.io/apify-client-python/;
53156
}
54-
location /sdk/js {
55-
proxy_pass https://apify.github.io/apify-sdk-js/;
157+
location = /api/client/python/ {
158+
if ($serve_markdown) {
159+
rewrite ^ /client-python-llms.txt last;
160+
}
161+
proxy_pass https://apify.github.io/apify-client-python/;
56162
}
57-
location /sdk/python {
58-
proxy_pass https://apify.github.io/apify-sdk-python/;
163+
164+
location = /client-python-llms.txt {
165+
proxy_hide_header Content-Type;
166+
add_header Content-Type 'text/markdown; charset=utf-8' always;
167+
proxy_pass https://apify.github.io/apify-client-python/llms.txt;
168+
}
169+
170+
location ~ ^/api/client/python {
171+
set $path_suffix $1;
172+
set $proxy_path "/$path_suffix";
173+
174+
set $rewrite_condition "$serve_markdown$has_no_extension";
175+
176+
if ($rewrite_condition = "11") {
177+
set $proxy_path "${proxy_path}.md";
178+
}
179+
180+
proxy_pass https://apify.github.io/apify-client-python$proxy_path;
181+
}
182+
183+
184+
### Repository path: "/cli"
185+
186+
location = /cli {
187+
if ($serve_markdown) {
188+
rewrite ^ /cli-llms.txt last;
189+
}
190+
proxy_pass https://apify.github.io/apify-cli/;
59191
}
60-
location /cli {
192+
location = /cli/ {
193+
if ($serve_markdown) {
194+
rewrite ^ /cli-llms.txt last;
195+
}
61196
proxy_pass https://apify.github.io/apify-cli/;
62197
}
198+
199+
location = /cli-llms.txt {
200+
proxy_hide_header Content-Type;
201+
add_header Content-Type 'text/markdown; charset=utf-8' always;
202+
proxy_pass https://apify.github.io/apify-cli/llms.txt;
203+
}
204+
205+
location ~ ^/cli/(.*)$ {
206+
set $path_suffix $1;
207+
set $proxy_path "/$path_suffix";
208+
209+
set $rewrite_condition "$serve_markdown$has_no_extension";
210+
211+
if ($rewrite_condition = "11") {
212+
set $proxy_path "${proxy_path}.md";
213+
}
214+
215+
proxy_pass https://apify.github.io/apify-cli$proxy_path;
216+
}
217+
63218
location = /health {
64219
access_log off;
65220
add_header 'Content-Type' 'application/json';
@@ -260,16 +415,16 @@ server {
260415
rewrite ^/sdk/js/docs/typedefs(.*)$ /sdk/js/docs/2.3/typedefs$1 redirect;
261416

262417
# old integrated docs -> new docs in GH pages
263-
rewrite ^/apify-client-js/?$ /api/client/js/ redirect;
264-
rewrite ^/apify-client-js/latest/?$ /api/client/js/ redirect;
265-
rewrite ^/apify-client-python/?$ /api/client/python/ redirect;
266-
267-
# add trailing slashes to the root of GH pages docs
268-
rewrite ^/api/client/js$ /api/client/js/ redirect;
269-
rewrite ^/api/client/python$ /api/client/python/ redirect;
270-
rewrite ^/sdk/js$ /sdk/js/ redirect;
271-
rewrite ^/sdk/python$ /sdk/python/ redirect;
272-
rewrite ^/cli$ /cli/ redirect;
418+
rewrite ^/apify-client-js/?$ /api/client/js redirect;
419+
rewrite ^/apify-client-js/latest/?$ /api/client/js redirect;
420+
rewrite ^/apify-client-python/?$ /api/client/python redirect;
421+
422+
# remove trailing slashes from the root of GH pages docs
423+
rewrite ^/api/client/js/$ /api/client/js redirect;
424+
rewrite ^/api/client/python/$ /api/client/python redirect;
425+
rewrite ^/sdk/js/$ /sdk/js redirect;
426+
rewrite ^/sdk/python/$ /sdk/python redirect;
427+
rewrite ^/cli/$ /cli redirect;
273428

274429
# versions page redirects
275430
rewrite ^/versions/?$ / permanent; # no docs-wide changelog, redirect to the root

0 commit comments

Comments
 (0)