-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: make mkdocs_hooks.transform more robust
- Keeps the markdown inside the code blocks as is - Logs the modified lines (mkdocs serve —verbose) Signed-off-by: Damien Martin <damlobster@gmail.com>
- Loading branch information
1 parent
eb17190
commit 0825e43
Showing
1 changed file
with
23 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,31 @@ | ||
import re | ||
import logging | ||
|
||
logger = logging.getLogger('mkdocs.mkdocs_hooks.transform') | ||
|
||
def transform(markdown: str, page, config, files): | ||
in_code_block = 0 | ||
in_list = False | ||
lines = markdown.splitlines() | ||
for i in range(len(lines)): | ||
lines[i] = lines[i].replace('](../', | ||
f"]({config['repo_url']}blob/master/") | ||
lines[i] = re.sub(r"\\", "<br>", lines[i]) | ||
# check that lists at level 0 are not indented (no space before *|-|1.) | ||
if len(lines[i]) == 0: | ||
in_list = False | ||
elif re.match(r"^(\*|-|\d+\.) ", lines[i]): | ||
in_list = True | ||
if not in_list: | ||
lines[i] = re.sub(r"^\s+(\*|-|\d+\.) ", r"\1 ", lines[i]) | ||
line_out = lines[i] | ||
in_code_block = (in_code_block + | ||
len(re.findall("\s*[`]{3,}", line_out))) % 2 | ||
if not in_code_block: | ||
line_out = line_out.replace('](../', | ||
f"]({config['repo_url']}blob/master/") | ||
line_out = re.sub("\\\s*$", "<br>", line_out) | ||
# check that lists at level 0 are not indented (no space before *|-|1.) | ||
if len(line_out) == 0: | ||
in_list = False | ||
elif re.match(r"^(\*|-|\d+\.) ", line_out): | ||
in_list = True | ||
if not in_list: | ||
line_out = re.sub(r"^\s+(\*|-|\d+\.) ", r"\1 ", line_out) | ||
if line_out != lines[i]: | ||
logger.debug((f'[mkdocs_hooks] rewrite line {i+1}: ' | ||
f'"{lines[i]}" -> "{line_out}"')) | ||
lines[i] = line_out | ||
|
||
output = "\n".join(lines) | ||
return output |