-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d956532
commit 7269e90
Showing
3 changed files
with
41 additions
and
3 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
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
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
"""Utility functions for the frontend.""" | ||
|
||
import re | ||
|
||
|
||
def protect_dollar(string: str) -> str: | ||
r"""Escape unescaped dollar signs in a string. | ||
This function takes a string and returns a new string where all dollar signs ($) | ||
that are not preceded by a backslash (\) are escaped with an additional backslash. | ||
This is useful for preparing strings that contain dollar signs for environments | ||
where the dollar sign may be interpreted as a special character, such as in | ||
Markdown. | ||
Args: | ||
string (str): The input string containing dollar signs to be escaped. | ||
Returns: | ||
str: A new string with unescaped dollar signs preceded by a backslash. | ||
""" | ||
return re.sub(r"(?<!\\)\$", r"\$", string) | ||
|
||
|
||
def format_string(string: str) -> str: | ||
"""Format a string for safe Markdown usage. | ||
Args: | ||
string (str): The input string to be formatted. | ||
Returns: | ||
str: The formatted string. | ||
""" | ||
string = protect_dollar(string) | ||
return string |