question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I am serving my site in different locales, some that there should be /a.html and /zh/a.html

my issue is in some links, different urls loads the same discussion.

e.g:

they both load a discussion https://github.com/vuepress-theme-hope/giscus-discussions/discussions/45

which it’s title is v2/zh/guide/get-started/intro

I think nobody would expect this as the default behavior with pathname, so I insist this is a bug and should be fixed.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:3
  • Comments:20 (7 by maintainers)

github_iconTop GitHub Comments

6reactions
laymonagecommented, Jul 23, 2022

I’ve merged it and it’s live!

To use strict title matching, add data-strict="1" to the <script> tag. However, you’ll need to migrate any existing discussions before using this option. More information can be found on the docs. The giscus-component library will be updated soon to support this option.

In short, you need to edit your discussions’ body to include the SHA-1 hash of the title. You can calculate the hash using any SHA-1 calculator, such as this one.


For example, for the following discussion:

The title is Welcome to giscus!, so the hash is cad60a29d1b50cbeb42ec2ff630fc508afb1d2e3. You can’t see it in the discussion body because I’ve included the hash as an HTML comment, something like:

<!-- sha1: cad60a29d1b50cbeb42ec2ff630fc508afb1d2e3 -->

Proof: image

For now, you need to edit your existing discussions to include the hash. New discussions created by giscus will automatically include the hash (even if you don’t enable the option). Note that you don’t need to follow the same format: as long as the hash is there, giscus can find it. For example, if you just write cad60a29d1b50cbeb42ec2ff630fc508afb1d2e3 in the discussion body, that works too.

Note that the strict matching is strict, like, really strict. That means if somehow the search term used by giscus contains a different character, even a whitespace, compared to the actual title you used to calculate the hash on GitHub, it won’t match.

I don’t think it’s worth making an automatic migration tool, as I believe most users only have a handful amount of discussions and it’s still reasonable to edit them by hand. If you have so many discussions, feel free to change my mind, maybe I can help write a small script.

4reactions
laymonagecommented, Jul 18, 2022

Hey all, thanks for the suggestions! I’ve had a solution in mind for this, I mentioned it in an earlier comment (https://github.com/giscus/giscus/issues/508#issuecomment-1109459156):

Another solution that I previously thought about is using a hash of the current search term and put it in the body of the discussion. Then, use that as the basis for the search instead of the title. This would definitely require migrating the existing discussions for users, but using a hash is less prone to fuzzy search issues like this.

I think we could make an optional toggle to enable strict matching that works for all existing mapping options. When the toggle is enabled, giscus will search the discussion body (instead of title) for the hash of whatever was originally used as the discussion title. The hash can be computed using the built-in digest function in JS Web Crypto API, e.g. with SHA-1 algorithm:

async function digestMessage(message) {
  const msgUint8 = new TextEncoder().encode(message);
  const hashBuffer = await crypto.subtle.digest('SHA-1', msgUint8);
  const hashArray = Array.from(new Uint8Array(hashBuffer));
  const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
  return hashHex;
}

To migrate, users need to edit the discussions so that the body contains the hash. It should also be possible to write a migration tool that handles this (log in, point to the repo and category, then automatically edit all discussions through the API), but this can be postponed for later.

The truth is, I’ve been busy preparing to move to another country next month, so I don’t have much time to work on this. If the solution turns out to be feasible, maybe I’ll put something together this weekend. Thanks for your patience!

Read more comments on GitHub >

github_iconTop Results From Across the Web

trying to use pattern matching with window.location.pathname ...
This is what I've been trying, which, clearly isn't working.... var path = window.location.pathname; if(path).match( ...
Read more >
matchPath - React Router v5
The first argument is the pathname you want to match. If you're using this on the server with Node.js, it would be req.path...
Read more >
Function PATHNAME-MATCH-P - CLHS
Description: pathname-match-p returns true if pathname matches wildcard, otherwise nil. The matching rules are implementation-defined but should be consistent ...
Read more >
URL.pathname - Web APIs - MDN Web Docs
The pathname property of the URL interface represents a location in a hierarchical structure. It is a string constructed from a list of...
Read more >
pathname-match - npm
Strip a url to only match the `pathname`.. Latest version: 1.2.0, last published: 6 years ago. Start using pathname-match in your project by ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found