Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: find a code block's backtick runs by regex when picking its fence (#15913)
Daniel Hanc1c4cdcc43a1
diff --git a/studio/backend/core/inference/_html_to_md.py b/studio/backend/core/inference/_html_to_md.pyindex 5eb57261f2d..2f131b466a6 100644--- a/studio/backend/core/inference/_html_to_md.py+++ b/studio/backend/core/inference/_html_to_md.py@@ -2328,14 +2328,15 @@ def _visible_chars(text: str) -> int:return sum(_visible_len(line) for line in text.split("\n") if line.strip())+_BACKTICK_RUN = re.compile(r"`+")++def _fence_for(raw: str) -> str:"""Fence long enough to survive backticks in *raw*, as CommonMark requires:a literal ``` line inside the block would otherwise close it early."""- longest = run = 0- for char in raw:- run = run + 1 if char == "`" else 0- longest = max(longest, run)- return "`" * max(3, longest + 1)+ if "`" not in raw:+ return "```"+ return "`" * max(3, max(map(len, _BACKTICK_RUN.findall(raw))) + 1)def _visible_len(line: str) -> int:diff --git a/studio/backend/tests/test_html_to_md_fence_regex.py b/studio/backend/tests/test_html_to_md_fence_regex.pynew file mode 100644index 00000000000..205ecfb2605--- /dev/null+++ b/studio/backend/tests/test_html_to_md_fence_regex.py@@ -0,0 +1,50 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""_fence_for must not walk a <pre> one character at a time, and must pick the fence the walk picked."""++from __future__ import annotations++import random+import sys+from pathlib import Path++_BACKEND_DIR = str(Path(__file__).resolve().parent.parent)+if _BACKEND_DIR not in sys.path:+ sys.path.insert(0, _BACKEND_DIR)++from core.inference._html_to_md import _fence_for, html_to_markdown+++def _walking_fence_for(raw: str) -> str:+ longest = run = 0+ for char in raw:+ run = run + 1 if char == "`" else 0+ longest = max(longest, run)+ return "`" * max(3, longest + 1)+++class _NoIterStr(str):+ def __iter__(self):+ raise AssertionError("_fence_for walked the block character by character")+++def test_fence_for_does_not_walk_the_block():+ raw = _NoIterStr("x = 1\n" * 1000 + "s = '````'\n")+ assert _fence_for(raw) == "`````"+ assert _fence_for(_NoIterStr("x = 1\n" * 1000)) == "```"+++def test_fence_for_matches_the_walk():+ rng = random.Random(0)+ alphabet = ["`", "``", "```", "````", "a", " ", "\n", "é", "", "\x00", ""]+ cases = ["", "`", "``", "```", "a`b``c```d", "`" * 40, "x" * 1000]+ cases += ["".join(rng.choice(alphabet) for _ in range(rng.randint(0, 60))) for _ in range(5000)]+ for raw in cases:+ assert _fence_for(raw) == _walking_fence_for(raw), repr(raw)+++def test_pre_with_backtick_runs_gets_a_longer_fence():+ html = "<pre>a ``` b\n```` c</pre>"+ out = html_to_markdown(html)+ assert "`````\na ``` b\n```` c\n`````" in out