Skip to content

Improve the line breaking algorithm for links - #9085

Open
YDX-2147483647 wants to merge 1 commit into
typst:mainfrom
YDX-2147483647:linebreak-link
Open

YDX-2147483647 wants to merge 1 commit into
typst:mainfrom
YDX-2147483647:linebreak-link

Conversation

@YDX-2147483647

@YDX-2147483647 YDX-2147483647 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

UAX #14: Unicode Line Breaking Algorithm does not handle URLs well, so Typst has a special linebreak_link() function.

This PR improves two details of this function:

  1. In Typst v0.15.1, https:// always stays on the same line; but Fix line breaking for hosts starting with a digit #8039 accidentally made it split into three parts: https:, /, /.

    This PR restores the old behavior by distinguishing slashes (/, Class::Slash) from other characters (Class::Other), and forbiding line breaks before all slashes.

    Detailed analysis can be found in https:// should not be broke into three lines #9056.

  2. linebreak_link() has always had a special code that makes long pieces in URLs breakable. However on main, that code is not effective for the last piece. For example:

    #show: block.with(width: 0pt)
    https://a.com/01234567890123456789.html // This is breakable on `main`…
    https://a.com/01234567890123456789 // …but this is not.

    This PR fixes that by adding an additional call after the for-loop.

Additional considerations

Organization of the test code

The test util assert_linebreaks() is put inside test_linebreak_link(), because there might be another assert_linebreaks() added in the future. In #9002, one assert_linebreaks() is for linebreak_link(), and the other one is for the UAX #14 algorithm.

Non-ASCII URLs

No non-ASCII test cases are added, because the support of Typst for non-ASCII URLs is still somewhat limited, and I don't indent to change that in this PR.

Let's take https://en.wikipedia.org/wiki/Antonín_Dvořák as an example. (This example is copied from UTS #58: Unicode Link Detection and Formatting: URLs and Email Addresses.)

#show: block.with(width: 0pt)
#"https://en.wikipedia.org/wiki/Antonín_Dvořák"

Typst v0.15.1, main, and this PR all turn the above document1 to this:

https://
en.
wikipedia.
org/
wiki/
Antonín_Dvořák

Well, that looks acceptable, but what linebreak_link() actually receives is https://en.wikipedia.org/wiki/Anton — the rest part, ín_Dvořák, is left to the UAX #14 algorithm…

Footnotes

  1. That URL does not need to be quoted. However, we are talking about line breaking rather than linkification, so I quote it to disable the smart linkification. ↩

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant