makew0rld / amfora

A fancy terminal browser for the Gemini protocol.
GNU General Public License v3.0
1.14k stars 68 forks source link

Add top level 1 heading as bookmark title suggestion #293

Closed amohamed11 closed 2 years ago

amohamed11 commented 2 years ago

Added basic top level 1 # heading extraction to suggest it as title for bookmark. I'm a regex newbie, but this was the best regex I could come up with that handled most cases that I tested for. Any regex wizardry advice is welcome.

Closes #267

makew0rld commented 2 years ago

Thanks for your PR, this looks simple and nice! It may be replaced once #182 is implemented, but that's not happening any time soon.

However, I think your regex is faulty. I played around and came up with (?m)^#[^#][\t ]*[^\s].*$ which I think is better. Anything can be in a header, but your regex disallows Unicode symbols. It also matches across newlines due to use of \s.

If you didn't know already, you can test and play with your regex at https://regex101.com/. It's a great site and very handy for understanding regex and creating your own.

If you don't spot any issues with my regex, you can add it to this PR and I'll merge it after testing.

amohamed11 commented 2 years ago

@makeworld-the-better-one ah good catch on the unicode, completely forgot to test for that. I tested your regex on a few examples, and it seemes to work well. I've updated the regex and amended the commit to keep things small. Let me know if there is anything else.

makew0rld commented 2 years ago

Thanks for this, works great! Will merge now. In the future, I prefer contributors avoid force-pushes / amending, so what was changed is clear. I will squash the commits after anyway.