Skip to content

Avoid replace URL cross multiple line of text

Gary Wang requested to merge garywang/neochat:cross-line-replace into master

A room description may contain multiple line of texts and one of the line might end with a URL. The current behavior will match the text in the next line and replace it as a link, and will only replace the first link if there are more than one link in the description.

This patch fixes this issue by:

  1. avoid match cross-line (\r\n)
  2. globe match (/g)
  3. also support http

Before the patch:

图片

After the patch:

图片

There is a related issue with both the current implementation and this patch tho, if the description is blah [link](https://example.org) blah, then it will be replaced to [link](<a href="https://example.org">https://example.org)</a>. This can be resolved by using lookahead/lookbehind in regex (e.g. /(?<!\]\(|\<)(http[s]?:\/\/[^ \r\n]*)(?!\)|\>)/g) but sadly QML's js engine doesn't support it (QTBUG-98636), we might need to add a utility function and export it to QML to handle this case later.

(p.s. since this patch was write during work so I'm submitting this patch with my working email instead of my personal email)

Edited by Gary Wang

Merge request reports