Skip to content

Add support for more characters in links - #379

Merged
laurmaedje merged 6 commits into
typst:mainfrom
frozolotl:links
Mar 30, 2023
Merged

laurmaedje merged 6 commits into
typst:mainfrom
frozolotl:links

Conversation

@frozolotl

@frozolotl frozolotl commented Mar 27, 2023 •

Copy link
Copy Markdown
Contributor

This PR allows additional characters to be part of a link.

It excludes support for both ) and ] out of concern that parenthesized URLs would fail to parse.
In that case, users are recommended to make use of the link function.

This commit excludes support for both `)` and `]` out of concern that
parenthesized URLs fail to parse.
In that case, are recommended to use the `link` function.
@laurmaedje

Copy link
Copy Markdown
Member

Do you have a source on what constitutes a valid URL?

@frozolotl

frozolotl commented Mar 27, 2023 •

Copy link
Copy Markdown
Contributor Author

Correct URL parsing is notoriously complex, which is why I only added some remaining characters found in URLs.

However, here you go: https://url.spec.whatwg.org

@reknih

reknih commented Mar 28, 2023 •

Copy link
Copy Markdown
Member

This will also allow direct links to IPv6s, etc. We generally aim to keep automatic link recognition narrow to avoid false positives. You can always use the link function explicitly! Do you think it's worth it to keep all of these extra characters in here or should this PR be narrowed down in that respect?

@frozolotl

frozolotl commented Mar 28, 2023 •

Copy link
Copy Markdown
Contributor Author

I have now added a bit of common punctuation which a link can't end with. I assume with that change, false positives should be relatively rare.

Nevertheless, one caveat here would be the trailing question mark. URLs may end with them and still be perfectly valid. I think that is actually not a rare occurance.
However, URLs with a trailing question mark should still be valid without it. So even if a user accidentally forgot to wrap the link in the corresponding function, little harm would be done.
I think it's reasonable to not include them in a URL.

Unfortunately, this solution will still not allow IPv6 addresses in links, because closing brackets are excluded from the list of allowed characters. Wikipedia links especially will also often not be parsed correctly as it is.
Examples for that:

  • https://[::1]:8080/
  • https://en.wikipedia.org/wiki/Rust_(programming_language)

I can think of one heuristic that should work pretty well. That is, require an opening parenthesis for every closing parenthesis. My only real worry here is that the parsing would become too complex.
I would be happy to do that if you think that's the best solution.

Alternatively, we could bypass this discussion completely, by just not having any special link syntax and requiring users to use #link("…"). I'm not sure that's the right solution though.

@Julian-Wollersberger

Copy link
Copy Markdown

IMO the ability to just copy-paste a link into a document and have it work is still great, even if it fails in obscure cases.

That said, I'd also remove ( and [ from the allowed list of punctuation, for two reasons:

  • "Can't use parens in link syntax" is a more intuitive rule to remember than "Can't use closing parens, but opening parens work".
  • It's harder to discover why a link doesn't work when only the very last character is cut off, like in https://en.wikipedia.org/wiki/Rust_(programming_language. It's more obvious when a larger part is missing. And since open parens are usually paired with closed ones, the link wouldn't work anyway.

Ideally, there would be a warning that the link isn't parsed as the user might expect it, but I don't know whether typst has the infrastructure for warnings yet.

@frozolotl

Copy link
Copy Markdown
Contributor Author

Good points! I think I will tweak the behaviour to be in line with your suggestion if the "heuristic" approach isn't chosen. Regarding that, what's your opinion on it?

@Julian-Wollersberger

Julian-Wollersberger commented Mar 29, 2023 •

Copy link
Copy Markdown

The heuristic should probably be fine?

Theory states that lexers should use regular grammars, which is good for simple regex-based syntax highlighters. Though block_comment() already supports nested comments, so typst's lexing is context-free instead of regular anyway.

I quickly checked, and the VS code terminal recognizes closing parens in links if there was at least one open paren, but doesn't check for proper nesting. gnome-terminal checks for proper nesting. MS Word tries to be smart with editing, and ends up being confusing, so no idea what its precise rules are.

Parens in URLs seem to be common enough that all the above tools have special rules to handle them somewhat-smartly, so it's probably a good idea to follow their lead.

Edit: Requiring properly nested parens, like you suggested, should be the most correct way to do it, to make sure it doesn't conflict with other syntax features.

@laurmaedje

Copy link
Copy Markdown
Member

Checking paren nesting seems sensible to me.

@frozolotl

Copy link
Copy Markdown
Contributor Author

I have now implemented bracket-nesting respecting behavior.

@laurmaedje
laurmaedje merged commit ed36ef3 into typst:main Mar 30, 2023
@laurmaedje

Copy link
Copy Markdown
Member

Thanks!

@frozolotl
frozolotl deleted the links branch March 30, 2023 21:02
@LaurenzV LaurenzV mentioned this pull request Apr 3, 2023
gpradofe added a commit to gpradofe/typst that referenced this pull request May 7, 2026
Squashed port of the 14-commit fork/feat/memory-optimization-stack onto
upstream typst/main at 86e3843. Same content as the previous fork branch
(detailed history preserved on fork/feat/memory-optimization-stack and
the experimental-2x-pdf branch); this commit replays the work onto the
current upstream so the PR base is clean.

Notable rebase fixups:
- typst-library: take upstream's moxcms-based color.rs (replaces qcms);
  drop qcms workspace dep, add moxcms.
- typst-pdf/convert.rs: handle krilla's new
  KrillaError::DuplicateNamedDestination variant (upstream PR typst#379);
  consume register_named_destination's must_use return.
- typst/src/lib.rs: Phase 2 streaming Engine construction switched
  from `routines: &ROUTINES` to `library` field (upstream PR typst#8215
  request-stdlib-from-world-once).
- typst-library/foundations/styles.rs: import FxHashMap.
- pages/mod.rs: drop unused Routines import; merge upstream's slimmed
  import block with our streaming-layout-mode imports (At, Resolve, etc.).
- Bump krilla pin to gpradofe/krilla@6bcead6 (rebased streaming-tag
  branch).
- Drop unused [patch.crates-io] entry for pdf-writer; the renumber/io
  extensions in our pdf-writer fork are not consumed by the current
  typst-pdf code path.
- tests/ref/{pdf,svg}/hashes.txt regenerated against new upstream.

Verification:
- cargo build --release: clean.
- cargo test --release -p typst-tests: 3421/0 (parity with the previous
  rebased state at fork.4).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Dikluwe pushed a commit to Dikluwe/typst-crystalline that referenced this pull request Sep 12, 2026
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.

4 participants