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

fix Edit on GitHub link leading to wrong URL #4880

Conversation

DominusKelvin
Copy link
Contributor

From the below video, its seen that most pages are throwing 404 when the Edit on GitHub link is clicked

before.mp4

This PR resolves the issue by moving the call to the permalink plugin after githubLinks and layouts middleware has been called. Below is the result.

after.mp4

@DominusKelvin
Copy link
Contributor Author

Seems this needs a little more work to solve as I discovered broken links. Closing it for now.

@DominusKelvin DominusKelvin reopened this Oct 15, 2022
@DominusKelvin
Copy link
Contributor Author

After a couple of effort to make the "Edit in GitHub" work when generating the page, I noticed a couple of things:

  • We can't call githubLinks early without losing the ability to add it to the footer (which is compiled later on in the layouts() plugin
  • The current implementation was buggy when converting back to .md because the permalinks plugin turns learn-and-code.html to learn-and-code/index.html. When we convert this to .md we have learn-and-code/index.md which does not exist on GitHub.

I'd like to see a better solution to this without doing the client-side implementation.

Ideally, if there is a way to get the Files object from metalsmith before they are transformed by both markdown and permalinks, that should do the trick(preferably calling after the layouts plugin has done its job so we can have access to the footer)

@DominusKelvin
Copy link
Contributor Author

I'm open to any pointers on how to make this work without the client-side JavaScript :)

Thanks.

@MaledongGit
Copy link
Contributor

MaledongGit commented Oct 16, 2022

@DominusKelvin:Thanks for understanding it, that's why my first version was using both client js and server codes.

But now, you've also met the two problems I've met before. So IMO:

We can't call githubLinks early without losing the ability to add it to the footer (which is compiled later on in the layouts() plugin

You're right! We should keep it where it is now.

The current implementation was buggy when converting back to .md because the permalinks plugin turns learn-and-code.html to learn-and-code/index.html. When we convert this to .md we have learn-and-code/index.md which does not exist on GitHub.

We don't know whether we have index.md or something else. So we should have a check whether we really need "index.md" or not by checking the local path, something like this below:

function githubLinks(options) {
  return (files, m, next) => {
    Object.keys(files).forEach((path) => {
      if (!isEditableReg.test(path)) {
        return;
      }

      const file = files[path];
      path = path.replace('.html', '.md').replace(/\\/g, '/');

      const currentUrl = `locale/${options.locale}/${path}`;
      if (!fs.existsSync(currentUrl)) {
        path = path.replace("/index.md","");
        path = path + ".md";
        // console.log(`Current Url is: ${currentUrl}`);
        // console.log(`Path is : ${path}`);
      }

      const url = `https://github.com/nodejs/nodejs.org/edit/main/locale/${options.locale}/${path}`;
    ......
}

Notice I've added the currentUrl for us to compare whether the file really exists or not....Maybe this is a simple solution to it.

@DominusKelvin
Copy link
Contributor Author

Thanks, @MaledongGit. I'm implementing your suggestions now...

@MaledongGit
Copy link
Contributor

Thanks, @MaledongGit. I'm implementing your suggestions now...

Welcome if any better and simple solution here……

@DominusKelvin
Copy link
Contributor Author

Yeah, it is better @MaledongGit. I'm thinking if there is a way to future-proof the relative path ../../ but I'm not sure. Since __dirname won't work in this case without a relative path being added.

@DominusKelvin
Copy link
Contributor Author

DominusKelvin commented Oct 16, 2022

But I think it is fine since we are using the same(../../) in some other instances in the codebase. Updating this PR now...

@DominusKelvin
Copy link
Contributor Author

@MaledongGit. The code works but I don't think fs.existsSync(currentUrl) is catching some cases because paths with index.md is still being replaced

@DominusKelvin
Copy link
Contributor Author

Found the issue: It has to do with how .existsSync() works with relative path. Using path.resolve(__dirname, ../../locale/${options.locale}/${path}) fixed it.

@DominusKelvin
Copy link
Contributor Author

@MaledongGit I made a push that should have a complete solution now that works and doesn't require client-side code :)

Copy link
Contributor

@MaledongGit MaledongGit left a comment

Choose a reason for hiding this comment

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

LGTM fot it.

@MaledongGit
Copy link
Contributor

MaledongGit commented Oct 17, 2022

@DominusKelvin:Anyway, thank you for your help on it :)

@MaledongGit MaledongGit merged commit 1807f6a into nodejs:main Oct 17, 2022
3 checks passed
@DominusKelvin
Copy link
Contributor Author

You are welcome @MaledongGit and thanks for leading me on the right path to fix it :)

@yhoungdev
Copy link

@DominusKelvin i just learnt something really awesome from this PR

@DominusKelvin
Copy link
Contributor Author

@DominusKelvin i just learnt something really awesome from this PR

I'm glad it was useful to you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants