网站根目录:Github Pages与`jekyll --server` [英] Site root: Github Pages vs. `jekyll --server`
问题描述
当我运行jekyll --server
时,我的网站可以在http://localhost:4000/
上访问,但是当我部署到GitHub Project Pages时,它的站点可以在http://username.github.com/projectname/
上访问.
When I run jekyll --server
my site is available at http://localhost:4000/
, but when I deploy to GitHub Project Pages, it's available at http://username.github.com/projectname/
.
这意味着在引用样式表和其他资源时,我不能使用绝对URL.当例如使用相同的布局时,相对网址就会中断. index.html
和2012/01/01/happy-new-year.html
.将样式表和其他资源添加到GitHub Project页面存储库中的一种可接受的/好的方法是什么?
This means that I cannot use absolute URLs when referring to stylesheets and other resources. Relative URLs break when the same layout is used by e.g. index.html
and 2012/01/01/happy-new-year.html
. What is the-accepted/a-good way of adding stylesheets and other resources to a GitHub Project Pages repository?
交叉发布到 GitHub问题. >
推荐答案
之所以会出现此问题,是因为在用户页面和项目页面中,根目录始终是用户url(user.github.com).我找到了解决方案:将_config.yml
文件中的url
变量配置到github项目页面:
This problem arises because the root directory is always the user url (user.github.com), in both user and project pages. I found a solution to this: Configure the url
variable in the _config.yml
file to the github project page:
safe: true
...
url: "http://user.github.io/project-name"
然后在布局中使用绝对引用,并使用site.url
变量来做到这一点:
then, in the layouts, use absolute references, using the site.url
variable to do that:
<link rel="stylesheet" href="{{ site.url }}/css/styles.css">
并使用以下命令运行服务器:
and run the server using:
$jekyll --server --url=http://localhost:4000
在本地模式下,命令行选项将覆盖配置文件中的设置.通过这些设置,我将所有指向正确位置的链接都托管在github和本地模式下.
The command line option overwrite the setting in the configuration file in local mode. With those settings, I get all the links pointing to the right place, both hosted in github and in local mode.
更新:Jekyll 1.0 自Jekyll达到1.0以来,已弃用了上述选项server
和url
及其相应的命令行选项--server
和--url
.新版本的说明:
UPDATE: Jekyll 1.0 Since Jekyll reached 1.0, the aforemetioned options server
and url
and its respective command line options --server
and --url
have been deprecated. Instructions for the new version:
在_config.yml
文件中,添加变量baseurl
(不带斜杠):
In the _config.yml
file, add the variable baseurl
(without trailing slash):
baseurl: "http://user.github.io/project-name"
在布局或页面中,使用site.baseurl
变量使用绝对引用:
In the layouts or pages, use absolute references, using the site.baseurl
variable:
<link rel="stylesheet" href="{{ site.baseurl }}/css/styles.css">
然后运行本地服务器(baseurl选项故意为空):
and then, run the local server (the baseurl option is empty on purpose):
$ jekyll serve --watch --baseurl=
有关文档中的更改的更多详细信息.
More details about the changes in the docs.
这篇关于网站根目录:Github Pages与`jekyll --server`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!