apache / incubator-answer

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.
https://answer.apache.org
Apache License 2.0
11.93k stars 872 forks source link

fix: Chinese character tag bug #970

Closed byerer closed 1 month ago

byerer commented 1 month ago

961

Adopted the method used for handling UrlTitle to process slugname.

LinkinStars commented 1 month ago

https://xxxxx/tags/中文 -> https://xxxxx/tags/%25E4%25B8%25AD%25E6%2596%2587

The reason for this issue is as follows. When the frontend jumps, it performs a URL encoding operation on the tag name, and then the frontend directly calls the API using the encoded information. Therefore, the solution may be to decode the tag in the frontend before calling the API. It is possible that the backend does not need to make the modifications for this issue, only the frontend may need to be modified. Let's discuss it together with @shuashuai .

byerer commented 1 month ago

Initially, I also thought it was a front-end issue, because the encodeURIComponent function was used, so I deleted encodeURICompontent in the functions related to the tag slugName.

For example, in ui/src/router/pathFactory.ts

const tagLanding = (slugName: string) => {
  const r = slugName ? `/tags/${encodeURIComponent(slugName)}` : '/tags';
  return r;
};

Also in ui/src/components/Tag/index.tsx

const Index: FC<IProps> = ({
  ...
  href ||= pathFactory.tagLanding(encodeURIComponent(data.slug_name));
  ...
})

This will lead to new issues; if the slugName contains '/' or some special characters, it will result in URI errors.

Thus, I decided to resolve this issue using a method similar to the one used for handling UrlTitle.

I'm not sure if my understanding is correct, and I would be happy to discuss this issue with you.

shuashuai commented 1 month ago

Initially, I also thought it was a front-end issue, because the encodeURIComponent function was used, so I deleted encodeURICompontent in the functions related to the tag slugName.

For example, in ui/src/router/pathFactory.ts

const tagLanding = (slugName: string) => {
  const r = slugName ? `/tags/${encodeURIComponent(slugName)}` : '/tags';
  return r;
};

Also in ui/src/components/Tag/index.tsx

const Index: FC<IProps> = ({
  ...
  href ||= pathFactory.tagLanding(encodeURIComponent(data.slug_name));
  ...
})

This will lead to new issues; if the slugName contains '/' or some special characters, it will result in URI errors.

Thus, I decided to resolve this issue using a method similar to the one used for handling UrlTitle.

I'm not sure if my understanding is correct, and I would be happy to discuss this issue with you.

Tags are different from titles. tags are created without any checksums, so in order to prevent special characters from damaging the integrity of the URL, encodeURIComponent is used here to escape.

this was done with the following main considerations in mind:

  1. make the display of the tag on the page look the same as when it was created.
  2. no need to consider the compatibility of some of its old data.

like this: image image

The reason for using the encodeURIComponent method is to ensure that the URL conforms to the requirements, and there is no such restriction in the API, so we just need to restore the escaped parameters in the API inputs.

The APIs involved are

  1. /answer/api/v1/tag?name=xxxx
  2. /answer/api/v1/question/page?page_size=20&page=1&order=active&tag=xxxx

If you have other questions, you can continue the discussion.