1212import re
1313import sys
1414import urllib .error
15+ import urllib .parse
1516import urllib .request
1617from pathlib import Path
1718
@@ -66,9 +67,8 @@ def classify_link(url: str) -> str:
6667 """Classify a link as internal, external, anchor, or skip."""
6768 if url .startswith (("http://" , "https://" )):
6869 # Skip localhost/loopback URLs
69- from urllib .parse import urlparse
7070 try :
71- host = urlparse (url ).hostname or ""
71+ host = urllib . parse . urlparse (url ).hostname or ""
7272 if host in ("localhost" , "127.0.0.1" , "0.0.0.0" , "::1" ):
7373 return "skip"
7474 except Exception :
@@ -246,18 +246,34 @@ def check_external_links():
246246 broken = []
247247 skipped = 0
248248
249+ # Domains that reliably block automated requests or are geo-restricted
250+ skip_domains = {"assets-docs.dify.ai" , "volcengine.com" , "twitter.com" , "x.com" }
251+
249252 for i , url in enumerate (unique_urls ):
250253 if (i + 1 ) % 50 == 0 :
251254 print (f" Progress: { i + 1 } /{ len (unique_urls )} " )
252255
253- # Skip asset CDN URLs (usually reliable, many of them)
254- if "assets-docs.dify.ai" in url :
255- skipped += 1
256- continue
256+ # Skip unreliable domains by checking parsed hostname
257+ try :
258+ host = urllib .parse .urlparse (url ).hostname or ""
259+ if any (host == d or host .endswith ("." + d ) for d in skip_domains ):
260+ skipped += 1
261+ continue
262+ except Exception :
263+ pass
264+
265+ # Encode non-ASCII characters in URL path, preserving existing percent-escapes
266+ try :
267+ parsed = urllib .parse .urlparse (url )
268+ encoded_url = urllib .parse .urlunparse (parsed ._replace (
269+ path = urllib .parse .quote (parsed .path , safe = "/:@!$&'()*+,;=-._~%" )
270+ ))
271+ except Exception :
272+ encoded_url = url
257273
258274 try :
259275 req = urllib .request .Request (
260- url ,
276+ encoded_url ,
261277 method = "HEAD" ,
262278 headers = {"User-Agent" : "Mozilla/5.0 (Dify-Docs-LinkChecker/1.0)" }
263279 )
@@ -270,7 +286,7 @@ def check_external_links():
270286 if e .code == 405 :
271287 try :
272288 req = urllib .request .Request (
273- url ,
289+ encoded_url ,
274290 method = "GET" ,
275291 headers = {"User-Agent" : "Mozilla/5.0 (Dify-Docs-LinkChecker/1.0)" }
276292 )
0 commit comments