markdown: Include text & url in topic_links parameter of our API.

The linkifier code now includes both the shortened text and the expanded
URL, sorted by the order of the occurrence in a topic. This list is passed
back in the `topic_links` parameter of the /messages and the /events APIs.

topic_links earlier vs now:

earlier: ['https://www.google.com', 'https://github.com/zulip/zulip/32']

now: [{'url': 'https://www.google.com', 'text': 'https://www.google/com},
      {'url': 'https://github.com/zulip/zulip/32', 'text': '#32'}]

Similarly, the topic_links local echo logic in the frontend now returns
back an object.

Fixes: #17109.
This commit is contained in:
Sumanth V Rao
2021-01-26 12:02:29 +05:30
committed by Tim Abbott
parent de1660e407
commit e12f682e2e
10 changed files with 203 additions and 60 deletions

View File

@@ -222,7 +222,7 @@ export function add_topic_links(message) {
return;
}
const topic = message.topic;
let links = [];
const links = [];
for (const linkifier of linkifier_list) {
const pattern = linkifier[0];
@@ -239,17 +239,24 @@ export function add_topic_links(message) {
link_url = link_url.replace(back_ref, matched_group);
i += 1;
}
links.push(link_url);
// We store the starting index as well, to sort the order of occurence of the links
// in the topic, similar to the logic implemeted in zerver/lib/markdown/__init__.py
links.push({url: link_url, text: match[0], index: topic.indexOf(match[0])});
}
}
// Also make raw URLs navigable
const url_re = /\b(https?:\/\/[^\s<]+[^\s"'),.:;<\]])/g; // Slightly modified from third/marked.js
const match = topic.match(url_re);
if (match) {
links = links.concat(match);
const matches = topic.match(url_re);
if (matches) {
for (const match of matches) {
links.push({url: match, text: match, index: topic.indexOf(match)});
}
}
links.sort((a, b) => a.index - b.index);
for (const match of links) {
delete match.index;
}
message.topic_links = links;
}