frontend: let a figure be sized by its height as well as its width

A writer with a tall panel does not want to shrink it from the left edge: what
they know is "this one is too tall". The format had a width and nothing else, so
the only way to make a figure shorter was to guess a percentage and check the
page — and the previous commit's answer, "type 638px", asked them to do the
arithmetic themselves.

So `{height=300px}` joins `{width=…}`, in the same attribute block, and the
editor grows a second box beside the first. Both are typeable as well as
pre-set, both write through the reader's own parse function, and the hint beside
them says what the picture will measure on the page.

Three rules, and the point of all three is that a figure cannot be stretched:

* **a width is exact, a height is a cap.** A width has a reference — the column,
  or a length the writer chose — while nothing measures a height against
  anything, and enlarging a picture past its own pixels only makes it soft. In
  the flow of a sentence a height is always a downscale, which comes to the same
  thing as exact.
* **given one, the other follows from the picture's own shape** — `max-height`
  for a height, `width` for a width — so the height is never written by hand.
* **given both, they are a box to fit inside**, not a box to fill. Two numbers
  that need not have the picture's proportions can only mean "no bigger than",
  and the check asserts the drawn rectangle still matches the file's ratio.

Whichever box is left empty keeps the other's setting, so a size can be set in
two steps, and clearing either goes back to the natural size.

`check-image.mjs`: 26 checks to 36. Two of the new ones found real bugs while
being written. The first: with both a width and a height in one `{…}` block,
neither side parsed — the patterns were anchored to the whole block, so a figure
with two numbers silently printed at its natural size. The attribute block is now
split into declarations first. The second was mine: returning "contain" for a
height-only figure left a 1276×2000 box around a small picture, which the caption
then sat under. A height is a cap, and a cap needs no contain.

The MCP tool takes the same two arguments, so a model can write either number,
and a percentage height is refused with the reason it cannot work: a sentence is
flowed text, so there is nothing for it to be a percentage of and the browser
ignores it. v8 is deployed and DSH has verified both numbers round-tripping.
This commit is contained in:
2026-09-24 13:18:52 +08:00
parent b19f28492e
commit 1531263bd2
10 changed files with 499 additions and 104 deletions
+58 -7
View File
@@ -68,6 +68,15 @@ def register(server: MCPServer) -> None:
)
),
] = None,
height: Annotated[
str | None,
Field(
description=(
"可选高度,只能填像素(300 或 300px,1–4000):图片不会超过这个高度,"
"宽度按原比例跟着变。高度不能用百分比(句子里没有可参照的高度)"
)
),
] = None,
) -> str:
"""上传一张图片,返回可直接写进句子的 Markdown 与那个句子对象。"""
data = _decode(data_base64)
@@ -83,7 +92,9 @@ def register(server: MCPServer) -> None:
# origin, and the caller's content type or filename is only a claim.
key, content_type, digest = get_store().save(data)
markdown = _embed(key, caption=caption or _stem(filename), width=width)
markdown = _embed(
key, caption=caption or _stem(filename), width=width, height=height
)
return s.dumps(
{
"key": key,
@@ -119,6 +130,10 @@ def register(server: MCPServer) -> None:
#: silently does not apply is worse than a refusal that says so.
_WIDTH_ATTRIBUTE = re.compile(r"^\s*(?:w|width)\s*=\s*(\d{1,4})\s*(%|px)?\s*$", re.IGNORECASE)
_WIDTH_BARE = re.compile(r"^\s*(\d{1,4})\s*(%|px)?\s*$")
#: A height is pixels and nothing else; see :func:`_height_declaration`. A bare
#: number is pixels because there is nothing else it could be.
_HEIGHT_ATTRIBUTE = re.compile(r"^\s*(?:h|height)\s*=\s*(\d{1,4})\s*(?:px)?\s*$", re.IGNORECASE)
_HEIGHT_BARE = re.compile(r"^\s*(\d{1,4})\s*(?:px)?\s*$", re.IGNORECASE)
_MAX_PIXELS = 4000
_MAX_PERCENT = 100
@@ -152,19 +167,55 @@ def _stem(filename: str | None) -> str:
return name.rsplit(".", 1)[0] if "." in name else name
def _embed(key: str, *, caption: str, width: str | None) -> str:
"""The Markdown a sentence carries: the address, plus an optional width.
def _embed(
key: str, *, caption: str, width: str | None, height: str | None = None
) -> str:
"""The Markdown a sentence carries: the address, plus an optional size.
``markdown_for`` writes the ``![图注](地址)`` half — the same function the
upload endpoint answers with — and this adds the size, which the endpoint
does not take because a browser appends it in the editor.
"""
parts = [
part
for part in (_width_declaration(width), _height_declaration(height))
if part
]
markdown = markdown_for(key, caption)
attribute = _width_attribute(width)
return f"{markdown}{attribute}" if attribute else markdown
return f"{markdown}{{{' '.join(parts)}}}" if parts else markdown
def _width_attribute(width: str | None) -> str:
def _height_declaration(height: str | None) -> str:
"""``height=300px`` from what the caller wrote, or a refusal saying why not.
**Pixels only.** A percentage height has nothing to be a percentage *of* — a
sentence is flowed text, so there is no container height to measure against —
and the reader's answer to one is to ignore it, which is a size that silently
does not apply. ``1–4000`` for the same reason as a width: a typo should be
refused, not laid out.
"""
if height is None or not str(height).strip():
return ""
written = str(height).strip().strip("{}").strip()
match = _HEIGHT_ATTRIBUTE.match(written) or _HEIGHT_BARE.match(written)
if match is None:
# A percentage gets the reason it cannot work; anything else just gets
# told what a height looks like.
why = (
"百分比高度在句子里没有参照物,读的时候会被忽略"
if "%" in written
else "填像素数字,例如 300"
)
s.fail(f"height 只能填像素,例如 300 或 300px,收到的是“{height}”。{why}")
digits = int(match.group(1))
if not 1 <= digits <= _MAX_PIXELS:
s.fail(f"height 要在 1 到 {_MAX_PIXELS} 之间,收到的是“{height}”")
return f"height={digits}px"
def _width_declaration(width: str | None) -> str:
"""``{width=70%}`` from what the caller wrote, or a refusal saying why not.
Tolerant about how it is written — ``70%``, ``70 %``, ``{width=70%}`` and
@@ -198,7 +249,7 @@ def _width_attribute(width: str | None) -> str:
s.fail(
f"width 的 {unit} 值要在 1 到 {limit} 之间,收到的是“{width}”。{hint}"
)
return f"{{width={digits}{unit}}}"
return f"width={digits}{unit}"
__all__ = ["register"]