help-beta: Make it easier to keep track of imports in conversion.

Before this, every function itself was responsible for adding it's own
imports. That is extra string manipulation which we had to think about
everytime we were adding new conversion function.
Now, we have an import statement set that the functions can modify, and
we have a function responsible for adding those imports. This makes it
easier in the future to add more conversion functions which might be
introducing imports.
This commit is contained in:
Shubham Padia
2025-05-09 11:25:12 +00:00
committed by Tim Abbott
parent a82921f32c
commit b378e33136

View File

@@ -21,19 +21,23 @@ os.environ["DJANGO_SETTINGS_MODULE"] = "zproject.settings"
django.setup()
def replace_emoticon_translation_table(markdown_string: str) -> str:
def replace_emoticon_translation_table(markdown_string: str, import_statement_set: set[str]) -> str:
"""
We will replace emoticon_translations custom syntax in Python with
<EmoticonTranslations> astro component.
"""
return markdown_string.replace(
result = markdown_string.replace(
"\\{emoticon_translations\\}",
"""
import EmoticonTranslations from '../../components/EmoticonTranslations.astro';
<EmoticonTranslations />
""",
)
if result != markdown_string:
import_statement_set.add(
"import EmoticonTranslations from '../../components/EmoticonTranslations.astro';"
)
return result
def replace_image_path(markdown_string: str) -> str:
@@ -51,33 +55,19 @@ def replace_image_path(markdown_string: str) -> str:
return result.replace('="/static/images/help-beta', '="../../../../static/images/help')
def fix_file_imports(markdown_string: str) -> str:
def fix_file_imports(markdown_string: str, import_statement_set: set[str]) -> str:
def convert_to_pascal(text: str) -> str:
return to_pascal(text).replace("-", "").replace(".Md", "")
def convert_to_astro_tag(match: re.Match[str]) -> str:
return "<" + convert_to_pascal(match.group(1)) + " />"
def append_str_to_line(text: str, destination_str: str, n: int) -> str:
lines = destination_str.splitlines()
if 1 <= n <= len(lines):
lines[n - 1] += "\n" + text + "\n"
return "\n".join(lines)
RE = re.compile(r"^ {,3}\{!([^!]+)!\} *$", re.MULTILINE)
result = RE.sub(convert_to_astro_tag, markdown_string)
matches = RE.findall(markdown_string)
import_statement_set = {
f'import {convert_to_pascal(match)} from "./include/_{match}"' for match in matches
}
for import_statement in import_statement_set:
# First line of the file is always the heading/title of the
# file. We rely on the heading being the first line later
# in the function inserting frontmatter and thus we add
# this to the second line.
result = append_str_to_line(import_statement, result, 2)
for match in matches:
import_statement_set.add(f'import {convert_to_pascal(match)} from "./include/_{match}"')
return result
@@ -101,6 +91,31 @@ def fix_relative_path(markdown_string: str) -> str:
return markdown_string.replace("help/", "help-beta/")
def append_str_to_line(text: str, destination_str: str, n: int) -> str:
lines = destination_str.splitlines()
if 1 <= n <= len(lines):
lines[n - 1] += "\n" + text
return "\n".join(lines)
def insert_imports(markdown_string: str, import_statement_set: set[str]) -> str:
if len(import_statement_set) == 0:
return markdown_string
# This function is called when the frontmatter has not yet been
# inserted. First line of the file is always the heading/title of
# the file. We rely on the heading being the first line later in
# the conversion when inserting frontmatter. For this reason, we
# add the imports to the second line.
for import_statement in import_statement_set:
markdown_string = append_str_to_line(import_statement, markdown_string, 2)
# Add empty line at the end of import statement list.
markdown_string = append_str_to_line("", markdown_string, 2 + len(import_statement_set))
return markdown_string
def insert_frontmatter(markdown_string: str) -> str:
"""
We use the heading in the first line for the
@@ -116,12 +131,16 @@ def insert_frontmatter(markdown_string: str) -> str:
def convert_string_to_mdx(markdown_string: str) -> str:
# All imports inserted during conversion should be tracked here.
import_statement_set: set[str] = set()
result = markdown_string
result = fix_file_imports(result)
result = fix_file_imports(result, import_statement_set)
result = escape_curly_braces(result)
result = fix_relative_path(result)
result = replace_emoticon_translation_table(result)
result = replace_emoticon_translation_table(result, import_statement_set)
result = replace_image_path(result)
result = insert_imports(result, import_statement_set)
result = insert_frontmatter(result)
return result