時間:2023-05-30 04:15:01 | 來源:網(wǎng)站運(yùn)營
時間:2023-05-30 04:15:01 來源:網(wǎng)站運(yùn)營
Github Pages + jekyll 全面介紹極簡搭建個人網(wǎng)站和博客:本文,將會全面介紹一下如何使用Github Pages + jekyll搭建個人站點(diǎn)。所謂極簡的意思,就是不用使用git和本地構(gòu)建jekll服務(wù),直接在Github網(wǎng)站上編輯設(shè)置即可,但會涉及到j(luò)ekll的一些配置和編程控制。可以參看我的網(wǎng)站模板:https://scottcgi.github.io 使用了github內(nèi)置的主題修改而成。主題目錄如下:
username.github.io
,其中username必須是Github賬戶的用戶名(我的是scottcgi),http://github.io是固定的,這個地址將會成為個人站點(diǎn)的網(wǎng)站地址。另外,我們可以勾選Initialize this repository with a README,讓倉庫自動創(chuàng)建一個README.md文件,我們用它來做站點(diǎn)的首頁(當(dāng)然也可以不創(chuàng)建,后面自行創(chuàng)建,或是建立index.html也行)。username.github.io
的子站點(diǎn),比如訪問地址會是:username.github.io/aaa.github.io
??梢?,username.github.io
是Github默認(rèn)分配給你的域名,同名倉庫即代表著默認(rèn)網(wǎng)站內(nèi)容。而username.github.io/倉庫名稱
,是用來訪問你的其它倉庫的地址。username.github.io
名稱。Setting
頁面下有Github Pages的設(shè)置選項(xiàng)。綠色表示部署成功,每次修改倉庫內(nèi)容,都會出發(fā)Github jekyll重新編譯部署,需要1-2分鐘的時間,更新才能體現(xiàn)。如果有編譯錯誤,包括js,css,html,markdown語法問題,都會顯示紅色以及錯誤頁面和行號,同時會發(fā)郵件通知。其中,Source
有以下幾個選項(xiàng):gh-pages branch
是項(xiàng)目新建一個分支命名為這個,使用這個分支來做站點(diǎn)內(nèi)容。master branch
是使用主分支也是默認(rèn)的,來作為站點(diǎn)內(nèi)容。master branch/docs folder
是使用主分支的docs文件夾來作為站點(diǎn)內(nèi)容。None
就是禁用Github Pages。username.github.io
只能使用主分支,其它倉庫項(xiàng)目可以選擇其它兩個。接下來Choose a theme是Github提供的內(nèi)置的網(wǎng)站主題,選擇即可應(yīng)用無需其它設(shè)置。Custom domain是自定義域名,本文暫不討論。Code
頁面僅有兩個文件。_config.yml
是jekyll的全局配置文件,現(xiàn)在里面只有一句話,theme: jekyll-theme-modernist
。我們可以手動修改這個theme主題配置,網(wǎng)站就會應(yīng)用不同的主題。_layouts
(存放頁面模板,md或html文件的內(nèi)容會填充模板)_sass
(存放樣式表)_includes
(可以復(fù)用在其它頁面被include的html頁面)_posts
(博客文章頁面)assets
(原生的資源文件)js
css
image
_config.yml
(全局配置文件)index.html, index.md, README.md
(首頁index.html優(yōu)先級最高,如果沒有index,默認(rèn)啟用README.md文件)自定義文件和目錄
{{ variable }}
被嵌入在頁面中,會在靜態(tài)頁面生成的時候被替換成具體的數(shù)值。常用的全局變量對象有:site
和 page
。這兩個對象有很多默認(rèn)自帶的屬性,比如:{{ site.time }}
,{{ page.url }}
。更多的默認(rèn)值參看:https://jekyllrb.com/docs/variables。site
對象對應(yīng)的就是網(wǎng)站范圍,自定義變量放在_config.yml
中,比如title:自定義標(biāo)題
使用{{ site.title }}
訪問。page
對象對應(yīng)的是單個頁面,自定義變量放在每個頁面的最開頭,比如:---myNum:100 myStr:我是字符串---
使用{{ page.myNum }}
和 {{ page.myStr }}
訪問。{% if site.title == 'Awesome Shoes' %} These shoes are awesome! {% endif %} {% if site.name == 'kevin' %} Hey Kevin! {% elsif site.name == 'anonymous' %} Hey Anonymous! {% else %} Hi Stranger! {% endif %}
{% for product in collection.products %} {{ product.title }} {% endfor %}
{{ "Hello World!" | downcase }}
轉(zhuǎn)換字符串為小寫。更多內(nèi)置函數(shù)詳見:https://jekyllrb.com/docs/liquid/filterssite
對象上,比如我們可以把導(dǎo)航配置到_config.yml中:nav:- name: Home link: /- name: About link: /about.html// 以下嵌入頁面,page.url以 "/" 開頭<nav> {% for item in site.nav %} <a href="{{ item.link }}" {% if page.url == item.link %} style="color: red;" {% endif %} > {{ item.name }} </a> {% endfor %}</nav>
當(dāng)然,我們也可以把一些數(shù)據(jù)單獨(dú)放入一個yml
文件,然后放在固定的數(shù)據(jù)文件夾_data
下,比如_data/navigation.yml
,這樣訪問這個文件的數(shù)據(jù)對象就是site.data.navigation
。_layouts
文件夾存放的是頁面模板,默認(rèn)需要一個default.html
,什么意思?就是說,layout提供一個頁面的布局框架,這是固定的模式,包括樣式、結(jié)構(gòu)、布局、腳本控制等等。然后,我們在用其它md或html文件去動態(tài)填充這個框架,這樣就形成了一個完整的頁面。比如我的default.html
頁面如下:<!doctype html><html lang="{{ page.lang | default: site.lang }}"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> {% seo %} <link rel="icon" href="https://avatars0.githubusercontent.com/u/1797320" type="image/x-icon" title="scottcgi"> <link rel="stylesheet" href="{{ '/assets/css/style.css?v=' | append: site.github.build_revision | relative_url }}"> <script src="{{ '/assets/js/scale.fix.js' | relative_url }}"></script> <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"> </head> <body> <div class="wrapper"> <header {% unless site.description or site.github.project_tagline %} class="without-description" {% endunless %}> <h1>{{ site.title | default: site.github.repository_name }}</h1> <p>{{ page.description | default: site.description }}</p> <ul> {% for item in site.nav %} {% if page.url == item.link %} <li style="background-color:#069"> <a href="{{ item.link }}"> <strong>{{ item.name }}</strong> </a> </li> {% else %} <li><a href="{{ item.link }}">{{ item.name }}</a></li> {% endif %} {% endfor %} </ul> </header> <section> {{ content }} </section> </div> <footer> <p>{{ site.copyright | default: "copyright not found in _config.yml" }}</p> </footer> </body></html>
{% seo %}
是jekyll的一個插件提供的seo優(yōu)化,詳情在這里:https://github.com/jekyll/jekyll-seo-tag{{ content }}
這個變量是內(nèi)置的,會用我們的md或html頁面填充這部分內(nèi)容。_includes
的頁面,還有就是來自 {{ content }}
對應(yīng)的頁面。{{ content }}
來填充模板,而是使用_includes
的頁面來代替{{ content }}
,但這樣不夠靈活,因?yàn)槭褂?{{ content }}
,我們可以在每個頁面單獨(dú)設(shè)置對應(yīng)的layout
模板。---layout: defaulttitle: About---# About pageThis page tells you a little bit about me.
layout: default
就是告訴jekyll這個頁面使用哪個模板,即這個頁面會放入哪個模板的{{ content }}
。當(dāng)然,我們可以在_layouts
文件夾下提供多個不同的模板,然后根據(jù)需要不同的頁面使用不同的layout
。_posts
目錄下:---layout: post--- 這是一篇博客文章。
post
的模板頁面到_layouts
文件夾下面。---layout: default---<h1>{{ page.title }}</h1><p>{{ page.date | date_to_string }} - {{ page.author }}</p>{{ content }}
可見,模板頁面本身也可以使用模板,這里post
使用了default
模板,而這里{{ content }}
就會填充_posts
下面編寫的頁面(如果頁面使用了layout: post
模板)。blog.html
頁面:---layout: defaulttitle: Blog---<h1>Latest Posts</h1><ul> {% for post in site.posts %} <li> <h2><a href="{{ post.url }}">{{ post.title }}</a></h2> <p>{{ post.excerpt }}</p> </li> {% endfor %}</ul>
site.posts
jekyll會自動生成_posts
目錄的對象。post.url
jekyll會自動會設(shè)置在_posts
目錄下的頁面url。post.title
默認(rèn)是md文件名稱,但也可以在文章頁面自定義title: 我的文章自定義名稱
。post.excerpt
默認(rèn)是文章第一段的摘要文字。關(guān)鍵詞:
客戶&案例
營銷資訊
關(guān)于我們
微信公眾號
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。