vuepress / core

Vue-Powered Static Site Generator
https://vuepress.vuejs.org
MIT License
2.17k stars 923 forks source link

[Feature request] 设置 base 选项的时候,站内绝对路径不需要改为相对路径 #653

Closed jzz4012650 closed 2 years ago

jzz4012650 commented 2 years ago

Feature request

Description

关联讨论 #652

设置了 base 选项的时候,希望能够自动将站内绝对路径转为 <router-link>,而现在会转为 <a>,导致需要将绝对路径改为相对路径;(关联文档

Proposed Solution

base/foo 的情况为例:

以上提议基于站内跳转比较频繁,同域跨站跳转相对小众的情况考虑;

PS:Vue-Press 是个优秀的静态站点生成工具,插件化设计尤其棒,感谢开发组同学们的付出 ❤️ !

Mister-Hope commented 2 years ago

Just inspired by #694, maybe we could add a hack here make the following different?

Probably in core or as a new plugin?

E.g.: with base /demo/:

@meteorlxy is this ok for you?

Mister-Hope commented 2 years ago

@jzz4012650 I am disagree your "Proposed Solution" . A docs may deployed under server domain.

For vuepress2 docs, it's available at:

Also ,this becomes problematic even when you deploy in one place: you are stoping others to simply mirror it.

KevinBrother commented 2 years ago

Just inspired by #694, maybe we could add a hack here make the following different?

Probably in core or as a new plugin?

  • /abc.html pointing to base root as expected
  • $abc.html poiting to host root

E.g.: with base /demo/:

  • /abc.html is /demo/abc.html
  • $abc.html is /abc.html

@meteorlxy is this ok for you?

Does it supported now?

Mister-Hope commented 2 years ago

I will share some other advantages when using relative links.

  1. It benifits when your user are browsering source markdown files, relative links works in editors and git provider like github, but an absolute link or hacky links (e.g.: starting with $ as I mentioned above) will be broken in that case.
  2. It's not recommanded to change your doc structure very often, as users may add them as bookmark, or reference theme in other places. It's allways good to choose a good link wisely at the beginning.

    And even if you change the url, you would better keep the old link available, just see what vue is doing at here. If you make sure the old link is available, there would be no broken links even if you forgot some.

But on the other hand, I do agree that it's good to have something I mention above to provide users more choise.

Mister-Hope commented 2 years ago

@meteorlxy I am planning to work on this, and I want to make sure that if this is ok to you:

Starting with base: '/base/' as an exmple.

All markdown links should be resolved as <RouterLink /> so:

[Home](/README.md)
[Guide](/guide.md)

should be

<RouterLlink to="/">Home</RouterLink>
<RouterLlink to="/guide.html">Guide</RouterLink>

which is acutally:

<a href="/base/">Home</a>
<a href="/base/guide.html">Guide</a>

And when user want some thing without base, they should use a link not ending with .md, and we will keep them as is:

<!-- /index.html should be the same--> 
[Home](/)
[Guide](/guide.html)

should be

<!-- or /index.html when setting /index.html --> 
<a href="/">Home</a>
<a href=/guide.html">Guide</a>
ascott18 commented 2 years ago

For anyone else running into this while the linked PR is not yet merged, you can solve this with a simple inline plugin that hacks markdown-it to prepend the base URL:

const baseUrl = "/SomeBaseUrl"
export default {
  base: `${baseUrl}/`,
  plugins: [
    {
      // Workaround for https://github.com/vuepress/vuepress-next/issues/653
      // Prepend absolute URLs with base URL.
      name: "baseify-link",
      extendsMarkdown(md) {
        const old = md.normalizeLink;
        md.normalizeLink = (url) => {
          url = old(url);
          if (url.startsWith("/") && !url.startsWith(baseUrl)) {
            url = baseUrl + url
          }
          return url;
        }
      }
    },
  ]
}
ascott18 commented 2 years ago

@meteorlxy the fix in the linked commit doesn't work for .md links that include a fragment. E.g. it works for /some/file.md, but does not work for /some/file.md#heading.

Would you like me to open a new issue, or do you want to reopen this one to track this?

meteorlxy commented 2 years ago

@ascott18 It's better to open another bug report for that. The original issue was not in English, which is bad for reference. And, it was for the feature, not for the following bug.


Fixed in 252f4acb2f7b69b8c920aafac362d44027f9ae49