revert: precompile hex color regex pattern at module level (#35016)

Reverts langchain-ai/langchain#34480
This commit is contained in:
Mason Daugherty
2026-02-04 16:47:52 -05:00
committed by GitHub
parent 1bb366315f
commit 9db00d3a6e

View File

@@ -41,8 +41,6 @@ except ImportError:
MARKDOWN_SPECIAL_CHARS = "*_`"
_HEX_COLOR_PATTERN = re.compile(r"^#(?:[0-9a-fA-F]{3}){1,2}$")
def draw_mermaid(
nodes: dict[str, Node],
@@ -432,8 +430,10 @@ def _render_mermaid_using_api(
)
# Check if the background color is a hexadecimal color code using regex
if background_color is not None and not _HEX_COLOR_PATTERN.match(background_color):
background_color = f"!{background_color}"
if background_color is not None:
hex_color_pattern = re.compile(r"^#(?:[0-9a-fA-F]{3}){1,2}$")
if not hex_color_pattern.match(background_color):
background_color = f"!{background_color}"
# URL-encode the background_color to handle special characters like '!'
encoded_bg_color = urllib.parse.quote(str(background_color), safe="")