From a94e0427bfcb5a0e7422d23715cb253d5cfa0e87 Mon Sep 17 00:00:00 2001 From: Lisandra-dev Date: Mon, 30 Oct 2023 18:16:13 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=84=20synced=20local=20'overrides/'=20?= =?UTF-8?q?with=20remote=20'overrides/'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- overrides/hooks/on_page_markdown.py | 49 +++++++++++++++-------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/overrides/hooks/on_page_markdown.py b/overrides/hooks/on_page_markdown.py index 884d882..98dfa87 100644 --- a/overrides/hooks/on_page_markdown.py +++ b/overrides/hooks/on_page_markdown.py @@ -1,48 +1,51 @@ import re + def non_breaking_space(markdown): - return re.sub('[\u00A0\u1680\u180E\u2000-\u200B\u202F\u205F\u3000\uFEFF]', ' ', markdown) + return re.sub( + "[\u00A0\u1680\u180E\u2000-\u200B\u202F\u205F\u3000\uFEFF]", " ", markdown + ) + def update_heading(markdown): - file_content = markdown.split('\n') - markdown = '' + file_content = markdown.split("\n") + markdown = "" code = False for line in file_content: if not code: - if line.startswith('```'): + if line.startswith("```"): code = True - elif line.startswith('#') and line.count('#')<=5: - heading_number = line.count('#') + 1 - line = '#' * heading_number + ' ' + line.replace('#', '') - elif line.startswith('```') and code: + elif line.startswith("#") and line.count("#") <= 5: + heading_number = line.count("#") + 1 + line = "#" * heading_number + " " + line.replace("#", "") + elif line.startswith("```") and code: code = True - markdown += line + '\n' + markdown += line + "\n" return markdown + def strip_comments(markdown): - file_content = markdown.split('\n') - markdown = '' - for line in file_content: - if not re.search(r'%%(.*)%%', line) or not line.startswith('%%') or not line.endswith('%%'): - markdown += line + '\n' - markdown = re.sub(r'%%(.*)%%', '', markdown, flags=re.DOTALL) - return markdown + return re.sub(r"%%.*?%%", "", markdown, flags=re.DOTALL) + def fix_tags(metadata): - tags = metadata.get('tags', None) or metadata.get('tag', None) + tags = metadata.get("tags", None) or metadata.get("tag", None) if tags and isinstance(tags, str): - tags = tags.split('/') + tags = tags.split("/") tags = [tag.strip() for tag in tags] - metadata['tags'] = tags + metadata["tags"] = tags return metadata + def on_page_markdown(markdown, files, page, config, **kwargs): - config_hooks = config['extra'].get('hooks', {'strip_comments': True, 'fix_heading': False}) - if config_hooks['strip_comments']: + config_hooks = config.get( + "extra", {"hooks": {"strip_comments": True, "fix_heading": False}} + ).get("hooks", {"strip_comments": True, "fix_heading": False}) + if config_hooks.get("strip_comments", True): markdown = strip_comments(markdown) - if config_hooks['fix_heading']: + if config_hooks.get("fix_heading", False): markdown = update_heading(markdown) metadata = fix_tags(page.meta) page.meta = metadata markdown = non_breaking_space(markdown) - return markdown \ No newline at end of file + return markdown