Skip to content

Commit b596b88

Browse files
authored
feat: serve markdown automatically based on Accept header for child repositories (#2032)
Contains quite a lot of duplication, couldn't find a flexible way to do it without that. As a side effect, this also resolves #520.
1 parent 98abb99 commit b596b88

File tree

2 files changed

+253
-20
lines changed

2 files changed

+253
-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: 207 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,211 @@ 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/(llms|llms-full)\.txt$ {
69+
rewrite ^/sdk/js(/.*)$ $1 break;
70+
proxy_hide_header Content-Type;
71+
add_header Content-Type 'text/markdown; charset=utf-8' always;
72+
proxy_pass https://apify.github.io/apify-sdk-js$uri;
73+
}
74+
75+
location ~ ^/sdk/js/(.*)$ {
76+
set $path_suffix $1;
77+
set $proxy_path "/$path_suffix";
78+
79+
set $rewrite_condition "$serve_markdown$has_no_extension";
80+
81+
if ($rewrite_condition = "11") {
82+
set $proxy_path "${proxy_path}.md";
83+
}
84+
85+
proxy_pass https://apify.github.io/apify-sdk-js$proxy_path;
86+
}
87+
88+
### Repository path: "/sdk/python"
89+
90+
location = /sdk/python {
91+
if ($serve_markdown) {
92+
rewrite ^ /sdk-python-llms.txt last;
93+
}
94+
proxy_pass https://apify.github.io/apify-sdk-python/;
95+
}
96+
location = /sdk/python/ {
97+
if ($serve_markdown) {
98+
rewrite ^ /sdk-python-llms.txt last;
99+
}
100+
proxy_pass https://apify.github.io/apify-sdk-python/;
101+
}
102+
103+
location = /sdk-python-llms.txt {
104+
proxy_hide_header Content-Type;
105+
add_header Content-Type 'text/markdown; charset=utf-8' always;
106+
proxy_pass https://apify.github.io/apify-sdk-python/llms.txt;
107+
}
108+
109+
location ~ ^/sdk/python/(llms|llms-full)\.txt$ {
110+
rewrite ^/sdk/python(/.*)$ $1 break;
111+
proxy_hide_header Content-Type;
112+
add_header Content-Type 'text/markdown; charset=utf-8' always;
113+
proxy_pass https://apify.github.io/apify-sdk-python$uri;
114+
}
115+
116+
location ~ ^/sdk/python/(.*)$ {
117+
set $path_suffix $1;
118+
set $proxy_path "/$path_suffix";
119+
120+
set $rewrite_condition "$serve_markdown$has_no_extension";
121+
122+
if ($rewrite_condition = "11") {
123+
set $proxy_path "${proxy_path}.md";
124+
}
125+
126+
proxy_pass https://apify.github.io/apify-sdk-python$proxy_path;
127+
}
128+
129+
### Repository path: "/api/client/js"
130+
131+
location = /api/client/js {
132+
if ($serve_markdown) {
133+
rewrite ^ /client-js-llms.txt last;
134+
}
49135
proxy_pass https://apify.github.io/apify-client-js/;
50136
}
51-
location /api/client/python {
137+
location = /api/client/js/ {
138+
if ($serve_markdown) {
139+
rewrite ^ /client-js-llms.txt last;
140+
}
141+
proxy_pass https://apify.github.io/apify-client-js/;
142+
}
143+
144+
location = /client-js-llms.txt {
145+
proxy_hide_header Content-Type;
146+
add_header Content-Type 'text/markdown; charset=utf-8' always;
147+
proxy_pass https://apify.github.io/apify-client-js/llms.txt;
148+
}
149+
150+
location ~ ^/api/client/js/(llms|llms-full)\.txt$ {
151+
rewrite ^/api/client/js(/.*)$ $1 break;
152+
proxy_hide_header Content-Type;
153+
add_header Content-Type 'text/markdown; charset=utf-8' always;
154+
proxy_pass https://apify.github.io/apify-client-js$uri;
155+
}
156+
157+
location ~ ^/api/client/js/(.*)$ {
158+
set $path_suffix $1;
159+
set $proxy_path "/$path_suffix";
160+
161+
set $rewrite_condition "$serve_markdown$has_no_extension";
162+
163+
if ($rewrite_condition = "11") {
164+
set $proxy_path "${proxy_path}.md";
165+
}
166+
167+
proxy_pass https://apify.github.io/apify-client-js$proxy_path;
168+
}
169+
170+
### Repository path: "/api/client/python"
171+
172+
location = /api/client/python {
173+
if ($serve_markdown) {
174+
rewrite ^ /client-python-llms.txt last;
175+
}
52176
proxy_pass https://apify.github.io/apify-client-python/;
53177
}
54-
location /sdk/js {
55-
proxy_pass https://apify.github.io/apify-sdk-js/;
178+
location = /api/client/python/ {
179+
if ($serve_markdown) {
180+
rewrite ^ /client-python-llms.txt last;
181+
}
182+
proxy_pass https://apify.github.io/apify-client-python/;
56183
}
57-
location /sdk/python {
58-
proxy_pass https://apify.github.io/apify-sdk-python/;
184+
185+
location = /client-python-llms.txt {
186+
proxy_hide_header Content-Type;
187+
add_header Content-Type 'text/markdown; charset=utf-8' always;
188+
proxy_pass https://apify.github.io/apify-client-python/llms.txt;
189+
}
190+
191+
location ~ ^/api/client/python/(llms|llms-full)\.txt$ {
192+
rewrite ^/api/client/python(/.*)$ $1 break;
193+
proxy_hide_header Content-Type;
194+
add_header Content-Type 'text/markdown; charset=utf-8' always;
195+
proxy_pass https://apify.github.io/apify-client-python$uri;
196+
}
197+
198+
location ~ ^/api/client/python/(.*)$ {
199+
set $path_suffix $1;
200+
set $proxy_path "/$path_suffix";
201+
202+
set $rewrite_condition "$serve_markdown$has_no_extension";
203+
204+
if ($rewrite_condition = "11") {
205+
set $proxy_path "${proxy_path}.md";
206+
}
207+
208+
proxy_pass https://apify.github.io/apify-client-python$proxy_path;
59209
}
60-
location /cli {
210+
211+
### Repository path: "/cli"
212+
213+
location = /cli {
214+
if ($serve_markdown) {
215+
rewrite ^ /cli-llms.txt last;
216+
}
217+
proxy_pass https://apify.github.io/apify-cli/;
218+
}
219+
location = /cli/ {
220+
if ($serve_markdown) {
221+
rewrite ^ /cli-llms.txt last;
222+
}
61223
proxy_pass https://apify.github.io/apify-cli/;
62224
}
225+
226+
location = /cli-llms.txt {
227+
proxy_hide_header Content-Type;
228+
add_header Content-Type 'text/markdown; charset=utf-8' always;
229+
proxy_pass https://apify.github.io/apify-cli/llms.txt;
230+
}
231+
232+
location ~ ^/cli/(llms|llms-full)\.txt$ {
233+
rewrite ^/cli(/.*)$ $1 break;
234+
proxy_hide_header Content-Type;
235+
add_header Content-Type 'text/markdown; charset=utf-8' always;
236+
proxy_pass https://apify.github.io/apify-cli$uri;
237+
}
238+
239+
location ~ ^/cli/(.*)$ {
240+
set $path_suffix $1;
241+
set $proxy_path "/$path_suffix";
242+
243+
set $rewrite_condition "$serve_markdown$has_no_extension";
244+
245+
if ($rewrite_condition = "11") {
246+
set $proxy_path "${proxy_path}.md";
247+
}
248+
249+
proxy_pass https://apify.github.io/apify-cli$proxy_path;
250+
}
251+
63252
location = /health {
64253
access_log off;
65254
add_header 'Content-Type' 'application/json';
@@ -260,16 +449,16 @@ server {
260449
rewrite ^/sdk/js/docs/typedefs(.*)$ /sdk/js/docs/2.3/typedefs$1 redirect;
261450

262451
# 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;
452+
rewrite ^/apify-client-js/?$ /api/client/js redirect;
453+
rewrite ^/apify-client-js/latest/?$ /api/client/js redirect;
454+
rewrite ^/apify-client-python/?$ /api/client/python redirect;
455+
456+
# remove trailing slashes from the root of GH pages docs
457+
rewrite ^/api/client/js/$ /api/client/js redirect;
458+
rewrite ^/api/client/python/$ /api/client/python redirect;
459+
rewrite ^/sdk/js/$ /sdk/js redirect;
460+
rewrite ^/sdk/python/$ /sdk/python redirect;
461+
rewrite ^/cli/$ /cli redirect;
273462

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

0 commit comments

Comments
 (0)