Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

composebox_typeahead: Avoid generating broken links. #30071

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kuv2707
Copy link
Collaborator

@kuv2707 kuv2707 commented May 13, 2024

The #**stream>topic** syntax generates broken links for topics containing two backticks or starting/ending with *, because of architectural flaws in the backend markdown processor. So we avoid generating the syntax for such topics and instead generate the normal link syntax in markdown.

Fixes #19873

CZO discussion

Screenshots and Screen Captures

67C2DQnjcD

chrome_yORpX1Hnrn

Self-review checklist
  • Self-reviewed the changes for clarity and maintainability
    (variable names, code reuse, readability, etc.).

Communicate decisions, questions, and potential concerns.

  • Explains differences from previous plans (e.g., issue description).
  • Highlights technical choices and bugs encountered.
  • Calls out remaining decisions and concerns.
  • Automated tests verify logic where appropriate.

Individual commits are ready for review (see commit discipline).

  • Each commit is a coherent idea.
  • Commit message(s) explain reasoning and motivation for changes.

Completed manual review and testing of the following:

  • Visual appearance of the changes.
  • Responsiveness and internationalization.
  • Strings and tooltips.
  • End-to-end functionality of buttons, interactions and flows.
  • Corner cases, error conditions, and easily imagined bugs.

@zulipbot zulipbot added size: S area: markdown (mentions) Mentions for users/groups/stream/topic/time. bug rust community request Issues of interest to the Rust community labels May 13, 2024
Copy link
Collaborator Author

@kuv2707 kuv2707 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have made a preliminary implementation and highlighted the reasons behind some technical choices.
Once we decide the correct logic to detect faulty topic names, I'll refactor the functions and put them in a sensible place.

web/src/composebox_typeahead.js Outdated Show resolved Hide resolved
web/src/composebox_typeahead.js Outdated Show resolved Hide resolved
@alya
Copy link
Contributor

alya commented May 14, 2024

@timabbott Do you want to take a look and provide some feedback here?

@alya alya added the integration review Added by maintainers when a PR may be ready for integration. label May 14, 2024
@kuv2707
Copy link
Collaborator Author

kuv2707 commented May 20, 2024

@timabbott Addressed the suggestion. Please take a look.

@@ -1180,6 +1185,31 @@ export function content_typeahead_selected(
return beginning + rest;
}

function will_produce_broken_stream_topic_link(topic_name: string): boolean {
return /(\*+)|(.*`.*`.*)/.test(topic_name);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The links for topic names with double backticks are now generated correctly, but the double backticks cause the enclosed part of the topic name to render as an inline code block.
I don't know what to do in this case since backslash escaping is disabled.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think there's no great answer for that at present. This is #18202.

As long as the link works correctly, though, that's a much less severe issue than #19873 is.

A possible workaround is to use HTML character references, as suggested here: #18202 (comment)
I'd definitely leave that as a separate later commit, though.

@kuv2707 kuv2707 force-pushed the 19873_topic_name_fix branch 2 times, most recently from a3c5613 to 8c243b1 Compare May 21, 2024 12:16
@kuv2707 kuv2707 requested a review from timabbott May 21, 2024 15:38
Copy link
Member

@gnprice gnprice left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @kuv2707! This is an old issue people have repeatedly run into, and I'm happy to see progress being made on it.

web/src/composebox_typeahead.ts Outdated Show resolved Hide resolved
@@ -1180,6 +1185,31 @@ export function content_typeahead_selected(
return beginning + rest;
}

function will_produce_broken_stream_topic_link(topic_name: string): boolean {
return /(\*+)|(.*`.*`.*)/.test(topic_name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think there's no great answer for that at present. This is #18202.

As long as the link works correctly, though, that's a much less severe issue than #19873 is.

A possible workaround is to use HTML character references, as suggested here: #18202 (comment)
I'd definitely leave that as a separate later commit, though.

web/src/composebox_typeahead.ts Outdated Show resolved Hide resolved
@kuv2707 kuv2707 force-pushed the 19873_topic_name_fix branch 2 times, most recently from 7704b04 to d15f3fd Compare June 4, 2024 12:32
@kuv2707
Copy link
Collaborator Author

kuv2707 commented Jun 4, 2024

@alya Does this PR have to go through the buddy and mentor review stages? (Since it's already reviewed by maintainers once)

@alya
Copy link
Contributor

alya commented Jun 5, 2024

I think it's OK to skip those at this stage. Has all the feedback above been addressed?

@kuv2707
Copy link
Collaborator Author

kuv2707 commented Jun 6, 2024

@alya I have addresed all the feedback.

Changes:

  • Simplified the regex.
  • Included $$ in the regex.
  • Checking both the stream name and the topic name.
  • Using HTML character references to escape backticks, dollars etc.

I have updated the PR description with the screenshots.

I've initiated a discussion about some decisions regarding corner cases here

@kuv2707
Copy link
Collaborator Author

kuv2707 commented Jun 8, 2024

@timabbott I have addressed the feedback.

@kuv2707 kuv2707 requested a review from timabbott June 8, 2024 08:17
}

function get_stream_name_from_topic_link_syntax(syntax: string): string {
const start = syntax.lastIndexOf("#**");
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this lastIndex? This function would benefit from a comment explaining how it's intended to be used / what assumptions the caller is responsible for checking about the syntax before using it.

Or maybe cleaner would be to just have it start with assert statements for those claims.

Copy link
Collaborator Author

@kuv2707 kuv2707 Jun 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both lastIndexOf and indexOf would work the same here. This is a remnant from when this piece of code wasn't separated into its own function. I have renamed it.

case "`":
return "`";
case ">":
return ">";
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this have >, when will_produce_broken_stream_topic_link does not? Seems like a comment should document this detail.

Copy link
Collaborator Author

@kuv2707 kuv2707 Jun 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had planned to include > as an invalid character for stream or topic name, but doing so does not cover all the cases where > can lead to a broken link.

  • Things work fine if we select a stream from the typeahead containing > in the name, i.e, a fallback markdown syntax is generated.
  • But if we then select a topic, such that the final syntax is something like #**stream>name>topicname**, a broken link is generated (since we can't know which > is for the syntax and which > is a part of the stream/topic name).

I am adding > in the regex for now, since it does cover some cases.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A related problem is that we can't topic_jump when a fallback stream link is generated. (initiated a discussion here).

});
}

function url_syntax(stream_name: string, topic_name: string | undefined): string {
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this called url_syntax? A function in the typeahead with this name is going to be assumed to be about something totally different.

Maybe it would also be a good idea to put all these new functions in a new topic_link_util.ts module?

Then this could be something like topic_link_util.get_fallback_markdown_link()

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Putting these in a module would be a good idea, since these functions are also required in #29302

@kuv2707
Copy link
Collaborator Author

kuv2707 commented Jun 10, 2024

@timabbott Implemented all the changes suggested. I still don't know what to do about some corner cases (CZO)

@kuv2707 kuv2707 force-pushed the 19873_topic_name_fix branch 2 times, most recently from 65e5a3a to 518d296 Compare June 10, 2024 07:09
The #**stream>topic** syntax generates broken links for
topics containing two backticks or ending with *, because of
architectural flaws in the backend markdown processor.
So we avoid generating the syntax for such topics and instead
generate the normal link syntax in markdown.

Fixes zulip#19873
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: markdown (mentions) Mentions for users/groups/stream/topic/time. bug integration review Added by maintainers when a PR may be ready for integration. rust community request Issues of interest to the Rust community size: XL
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix linking to topic names containing multiple backticks with in-message markdown
5 participants