Skip to main content

parse_image_width

Function parse_image_width 

Source
pub fn parse_image_width(seg: &str) -> Option<String>
Expand description

Recognize a single image-figure width segment: a named token (body|wide|page|screen|full) OR a content-relative percent (55%).

Returns the canonical string to store in Block::Figure.width:

  • named token → its canonical form (fullscreen)
  • percent → normalized "NN%" (clamped to (0, 100])

Returns None for anything else (captions, 200x150 box sizing, px). Box/px are intentionally rejected: image figures only support content-relative widths in v1 (see design §“Out of scope”).

MIRROR: the editor’s read-side parseImageWidth in frontend/app/editor/cm-image-extract.ts agrees with this for every canonical/moss-emitted width (named tokens, NN%, NN.N%) — the only widths the write path (set_image_width) ever produces. The two may diverge on malformed hand-typed input (this f64::parse accepts "55 %", ".5%", "+5%" which the TS regex rejects); harmless, read-side only. f64 (not f32) is used so fractional percents format identically to the JS side, preserving the editor↔build string-equality the design relies on.