Skip to content

Fix line breaking for hosts starting with a digit - #8039

Merged
laurmaedje merged 4 commits into
typst:mainfrom
I-Al-Istannen:fix/link-breaking-digit-hosts
Jul 31, 2026
Merged

laurmaedje merged 4 commits into
typst:mainfrom
I-Al-Istannen:fix/link-breaking-digit-hosts

Conversation

@I-Al-Istannen

@I-Al-Istannen I-Al-Istannen commented Mar 28, 2026 •

Copy link
Copy Markdown
Contributor

The what

This PR implements more general URL detection during line breaking, so the custom URL-breaking algorithm is actually applied to links. Discussed briefly on Discord, though I am not sure this qualifies as "clean fix"… I am also very happy to remove more comments, things were just new to me and I am not sure if they are new to the next person too. In my own code I would probably delete a few of them.

The why

Sometimes bibliographies (or text!) contain URLs, which need to be broken nicely. This is already implemented in Typst by adjusting UAX #14 and detecting links by:

  • having an UAX #14 break after :// (i.e. the URL scheme)
  • having an UAX #14 break before www.

While this mostly works for URLs, it falls flat for IPs and domains starting with a digit: UAX #14 does not produce a break after a / if the slash is followed by a digit, trying to keep fractions together. This causes http://1.1.1.1 or http://1up.example to be ignored by the custom URL-breaking heuristic.

As a picture:

Before After
image image

The how

This PR tries to detect a URL scheme and the separating ://, e.g. http://, and then applies the custom line breaking heuristic. It does so by scanning the next 40 characters (chosen by taking the maximum registered schema length and rounding up, accounting for a +3 due to ://) for the scheme separator :// and then ensuring the portion up until that separator conforms to the scheme grammar.

It still relies on UAX #14 placing a break right before the URL, but that should be fine, as long as URLs aren't directly conjoined with neighbouring words.

@I-Al-Istannen
I-Al-Istannen marked this pull request as ready for review March 28, 2026 23:13
@laurmaedje laurmaedje added the waiting-on-review This PR is waiting to be reviewed. label Mar 30, 2026
@isuffix isuffix added the text Related to the text category, which is all about text handling, shaping, etc. label May 17, 2026
// (i.e. prevent every step from scanning the whole text for the separating colon).
// Most URL schemes are very short, 40 was chosen as it contains all current schemes
// https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml.
let end = match text.char_indices().nth(40) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not super comfortable with decoding the next fourty chars for every break opportunity. Do you know whether UAX #40 already uses lookahead? That would be interesting to know.

If we accept lookahead, at least it seems possible to retain the next searched :// across multiple breakpoints if the breakpoint is still in front of it. This would turn it from a potentially quadratic (and therefore capped to 40) search into two zipped linear traversals of the string.

Not sure how easy it is to make this clean without too much code, but worth exploring.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, understandable. I will give it a shot :)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UAX does lookahead in some cases (for more complicated break rules), but not as far as necessary for URLs, I think.

I refactored it to a version that first searches for :// and then walks back to find the longest potential scheme start. Things like This is a url.http:// will treat url.http as a scheme then, which seems to work okay in practice. If we wanted to more reliably detect URLs that are adjoint to text and where no break opportunity falls before them, we need a different approach anyways.

This is maybe a bit worse than the solution you envisioned, as it still scans (potentially the whole string) first, but it at least only reads the whole thing twice at worst. Once for finding the :// and then a backwards search to find where the scheme begins. I first prototyped a version that fused both into a single loop, but that was quite unwieldy and hard to read. I hope that memchr is fast enough that doing it once "wastefully" is okay. The string is already materialized as a str and we do not need to work on anything but chars, as URL schemes must be ascii.

@laurmaedje laurmaedje added waiting-on-author Pull request waits on author and removed waiting-on-review This PR is waiting to be reviewed. labels May 27, 2026
This affects IP addresses as well as any domain name starting with a
digit.
@I-Al-Istannen
I-Al-Istannen force-pushed the fix/link-breaking-digit-hosts branch from 3f5609c to e40b13d Compare June 22, 2026 17:06
Fix line breaking for hosts starting with a digit

This affects IP addresses as well as any domain name starting with a
digit.
@I-Al-Istannen
I-Al-Istannen force-pushed the fix/link-breaking-digit-hosts branch from e40b13d to ddac796 Compare June 22, 2026 17:20
@laurmaedje laurmaedje added waiting-on-review This PR is waiting to be reviewed. and removed waiting-on-author Pull request waits on author labels Jun 29, 2026
@laurmaedje

Copy link
Copy Markdown
Member

I think the approach is fine now. It's not perfect, but good enough in terms of the complexity/gain trade-off. I also ran a benchmark and the impact is miniscule.

I just refactored it a bit and also added a test case that's still suboptimal, but it practically be very rare.

Thanks for your work and patience!

@laurmaedje
laurmaedje added this pull request to the merge queue Jul 31, 2026
Merged via the queue into typst:main with commit 32fd4cc Jul 31, 2026
9 checks passed
@I-Al-Istannen
I-Al-Istannen deleted the fix/link-breaking-digit-hosts branch July 31, 2026 19:41
@I-Al-Istannen

Copy link
Copy Markdown
Contributor Author

Thanks a lot!

Dikluwe pushed a commit to Dikluwe/typst-crystalline that referenced this pull request Aug 7, 2026
93 commits novos desde v0.15.1 (tag 9dfd3a0) até upstream/main (a51e028).
397 ficheiros alterados, 5782 inserções, 2831 remoções.

Destaques do upstream:
- Store required features in bindings (typst#8599)
- Support raw serialization format for typst eval (typst#8295)
- Add diagnostics-format environment variable (typst#8438)
- More robust overhang (typst#8131)
- Fix logical indices of hyphens with respect to surrounding tags (typst#8714)
- Bump krilla and hayro (typst#8716)
- Fix line breaking for hosts starting with a digit (typst#8039)
- Fix auto hanging-indent for heading numbering (typst#7247)

Build vanilla verificado: cargo build ok (3m02s).
Dikluwe added a commit to Dikluwe/typst-crystalline that referenced this pull request Sep 12, 2026
93 commits novos desde v0.15.1 (tag 9dfd3a0) até upstream/main (a51e028).
397 ficheiros alterados, 5782 inserções, 2831 remoções.

Destaques do upstream:
- Store required features in bindings (typst#8599)
- Support raw serialization format for typst eval (typst#8295)
- Add diagnostics-format environment variable (typst#8438)
- More robust overhang (typst#8131)
- Fix logical indices of hyphens with respect to surrounding tags (typst#8714)
- Bump krilla and hayro (typst#8716)
- Fix line breaking for hosts starting with a digit (typst#8039)
- Fix auto hanging-indent for heading numbering (typst#7247)

Build vanilla verificado: cargo build ok (3m02s).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

text Related to the text category, which is all about text handling, shaping, etc. waiting-on-review This PR is waiting to be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants