jekyll - 站点根目录 : Github Pages vs. `jekyll --server`

标签 jekyll github-pages

当我跑 jekyll --server我的网站位于 http://localhost:4000/ ,但是当我部署到 GitHub 项目页面时,它可以在 http://username.github.com/projectname/ 上找到。 .

这意味着我不能在引用样式表和其他资源时使用绝对 URL。例如,当使用相同的布局时,相对 URL 会中断。 index.html2012/01/01/happy-new-year.html .将样式表和其他资源添加到 GitHub 项目页面存储库的公认/好方法是什么?

交叉发布到 GitHub Issues .

最佳答案

出现这个问题是因为根目录始终是用户 url (user.github.com),在用户和项目页面中。我找到了解决方案:配置 url _config.yml 中的变量文件到github项目页面:

safe: true
...
url: "http://user.github.io/project-name"

然后,在布局中,使用绝对引用,使用 site.url变量来做到这一点:
<link rel="stylesheet" href="{{ site.url }}/css/styles.css">

并使用以下命令运行服务器:
$jekyll --server --url=http://localhost:4000

命令行选项在本地模式下覆盖配置文件中的设置。通过这些设置,我获得了指向正确位置的所有链接,它们都托管在 github 和本地模式中。

更新: Jekyll 1.0 由于 Jekyll 达到 1.0,上述选项 serverurl及其各自的命令行选项 --server--url已被弃用。新版本使用说明:

_config.yml文件,添加变量 baseurl (不带斜杠):
baseurl: "http://user.github.io/project-name"

在布局或页面中,使用绝对引用,使用 site.baseurl多变的:
<link rel="stylesheet" href="{{ site.baseurl }}/css/styles.css">

然后,运行本地服务器(baseurl 选项故意为空):
$ jekyll serve --watch --baseurl=

有关 docs 更改的更多详细信息.

关于jekyll - 站点根目录 : Github Pages vs. `jekyll --server`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14322329/

相关文章:

ruby - 在 macOS Mojave 上安装 eventmachine 时如何修复 "ERROR: Failed to build gem native extension."

jekyll 中的 markdownify 帖子标题

html - github 页面字体和 index.html 不工作

javascript - gh-pages 不会加载 CSS 或 JS

jekyll - 当 Jekyll 帖子没有图片时,如何获取该帖子的缩略图?

jekyll - Baseurl 和 url 配置对 github 上的 jekyll 不起作用

javascript - 设置 Jekyll 站点

github-pages - 从 README.md 自动链接到 GitHub 页面 URL?

html - Jekyll 拒绝 build ,尽管它之前 build 过

css - 为什么 bourbon mixins 在我的 jekyll 网站上不起作用?