forked from crystal-lang/crystal-book
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.py
27 lines (20 loc) · 966 Bytes
/
hooks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import logging
import os
import re
log = logging.getLogger('mkdocs')
version = os.getenv('CRYSTAL_VERSION', 'master')
def on_page_markdown(markdown, page, **kwargs):
path = page.file.src_uri
for bad in re.findall(r'\]\(((?:https?://(?:www\.)?crystal-lang\.org/reference)?/[^ )]*?)\)', markdown):
log.warning(f"Documentation file '{path}' contains an absolute link to the site itself: '{bad}'")
for m in re.finditer(r'\bhttps?://(www\.)?crystal-lang\.org/api/([0-9]+(\.[0-9]+)+|latest|master)/([^ )]+\.html)\b', markdown):
bad, good = m[0], f'https://crystal-lang.org/api/{m[4]}'
log.warning(
f"Documentation file '{path}' contains a version-pinned link to the API: '{bad}'\n"
f"Suggested fix: '{good}'"
)
return re.sub(
r'\bhttps?://(www\.)?crystal-lang\.org/api/(?!([0-9]+(\.[0-9]+)+|latest|master)/)',
f'https://crystal-lang.org/api/{version}/',
markdown
)