jugglerchris/rust-html2text

Support OSC 8 hyperlinks

Closed this issue · 6 comments

For use-cases like reading HTML email in TUI email clients, it would be nice with inline hyperlinks, using OSC8.

If you think that makes sense, I can try to implement it.

Hi,
I'm happy to have that feature, though not on by default.

I don't think there's any work to do (thinking aloud) in the library:

  • If the client integrates the html2text library and uses the Rich mode, then it's up to client to add the OSC8 based on the annotations when it writes to the terminal, so there's nothing to do here.
  • Otherwise, I think the place to do it would be in a map function passed to from_read_coloured() / Config::coloured().

However one place it could be useful is in the html2text example - it could easily have an extra --hyperlinks or similar option which would switch to an alternative map function. It'd be nice if there were some code sharing between the different combinations of --colour and the hyperlink option, though it wouldn't be a showstopper having a bit more duplication at this point.

Right! I forgot that I am just using one of the example programs. :)

Thanks for the pointers, I will give it a try.

I wanted to point out the limited usefulness of this support at the moment.

There are still only a few tools that support hyperlinks in the terminal, and you have to bear in mind that if you're working in a multiplexer, the whole chain has to be compatible: terminal, multiplexer and application.

What's more, while in a GUI you can see a URL by hovering over the hyperlink, in a TUI you're more likely to open the link directly, so in terms of security for emails, it's probably best to use footnote references.

And as @jugglerchris wrote, avoid it by default. For example, mdcat has imposed it, requiring you to pass the output to sed to remove the osc 8 sequences now.

Finally got around to trying it out, and it wasn't as good as I hoped.

One issue is that the hints utility in Kitty finds inline URLs or OSC8 hyperlinks, but not both. Maybe its time has not quite come yet.

Anyway, here's the diff if someone wants to try it. Termion didn't support OSC8 that I could see, so I hardcoded it to test.

diff --git a/examples/html2text.rs b/examples/html2text.rs
index 88bd8fd..b0a6a91 100644
--- a/examples/html2text.rs
+++ b/examples/html2text.rs
@@ -26,8 +26,12 @@ fn default_colour_map(
     for annotation in annotations.iter() {
         match annotation {
             Default => {}
-            Link(_) => {
+            Link(url) => {
                 start.push(format!("{}", termion::style::Underline));
+                if [your condition] {
+                    start.push(format!("\x1B]8;;{}\x1B\\", url));
+                    finish.push(format!("\x1B]8;;\x1B\\"));
+                }
                 finish.push(format!("{}", termion::style::Reset));
             }
             Image(_) => {

Thanks for your time and input.

Thanks for trying it and feeding back!

Thanks, when I wrote the comment above, a little on the reserve, I was still on Tmux.
Since then I switched to Kitty and I will use this code.