docsifyjs / docsify

🃏 A magical documentation site generator.
https://docsify.js.org
MIT License
27.59k stars 5.67k forks source link

Relative path link with ':ignore' dosen't work #1929

Open aKuad opened 1 year ago

aKuad commented 1 year ago

Bug Report

As far as I know, there are no same issues on here, no same questions on StackOverflow. Sorry if already exist same one.

Steps to reproduce

  1. File tree will be like this:
docs/
  |- index.html
  |- page/
        |- any_doc.md
        |- any_file.pdf
  1. Add a docsify config to index.html:
relativePath: true
  1. Write any_doc.md:
[Click to view PDF](./any_file.pdf ':ignore')
  1. Execute docsify:
docsify serve docs/
  1. Make any PDF file as docs/page/any_file.pdf

  2. Access to http://localhost:3000/#/page/any_doc

What is current behaviour

The link follows http://localhost:3000/#/page/./any_file.pdf

It looks ':ignore' (no compile option) and relative path resolving are not works.

What is the expected behaviour

The link follows http://localhost:3000/page/any_file.pdf

Other relevant information

Please create a reproducible sandbox

Edit 307qqv236

Mention the docsify version in which this bug was not present (if any)


Supplement

I tried specifying absolute path like this:

[Click to view PDF](/page/any_file.pdf ':ignore')

It works on local environment. But on GitHub pages, not works.

On local environment:

follows: http://localhost:3000/page/any_file.pdf

On GitHub pages (github.io):

follows: https://{user_name}.github.io/page/any_file.pdf

should be: https://{user_name}.github.io/{repo_name}/page/any_file.pdf

aKuad commented 1 year ago

I think, this part has some probrem...

In /src/core/render/compiler/link.js, Line 21: (+ my guessing note)

if (
  !isAbsolutePath(href) &&  // -> true
  !compilerClass._matchNotCompileLink(href) && // -> true
  !config.ignore            // -> false
) {
  /* in this case: this part will not run */
  if (href === compilerClass.config.homepage) {
    href = 'README';
  }
  href = router.toURL(href, null, router.getCurrentPath()); // Relative path resolving to absolute path?
} else {
  /* in this case: this part will run */
  if (!isAbsolutePath(href) && href.slice(0, 2) === './') {
    //     -> true                  -> true
    /* in this case: this part will run */
    href =
      document.URL.replace(/\/(?!.*\/).*/, '/').replace('#/./', '') + href;
        // Remove from last '/' to end of URL, and reemove '#/./' (for compile ignored link) ?
  }
  // for 'mailto:' link process?
  // ...
}

About this part:

    href =
      document.URL.replace(/\/(?!.*\/).*/, '/').replace('#/./', '') + href;
        // Remove string from last '/', remove '#/./' (for compile ignored link) ?

If current document URL: http://localhost:3000/#/page/any_doc If wrote link: ./any_file.pdf

document.URL
// -> http://localhost:3000/#/page/any_doc
document.URL.replace(/\/(?!.*\/).*/, '/').replace('#/./', '')
// -> http://localhost:3000/#/page/
href
// -> ./any_file.pdf
document.URL.replace(/\/(?!.*\/).*/, '/').replace('#/./', '') + href
// -> http://localhost:3000/#/page/./any_file.pdf
// (but should be: http://localhost:3000/page/any_file.pdf)
trusktr commented 11 months ago

I think we should be letting the browser handle URLs natively. F.e. maybe we can use new URL('./relative/file.foo', base) where base could be the site base path like http://localhost:3000/#/page/any_doc. So that we avoid doing relative path logic if the browser already can.

Does URL support having # in the base? If not, then that would prevent the idea from being feasible. Need to try it