jekyll 깃허브 블로그에 파일의 마지막 수정 날짜 자동으로 넣는 방법
전/후 비교

전에는 last update를 수동으로 써주고 있었다.

후에는 자동으로 깃허브 커밋내역을 읽어서 마지막 수정날짜를 수정해준 모습이다.
Last Modified At Plugin 사용법
source : jekyll-last-modified-at plugin
Setting Up
- Gemfile 내용 추가
group :jekyll_plugins do
gem "jekyll-last-modified-at"
end
-
_config.yml 파일에 내용 추가
plugins: - jekyll-last-modified-at # Optional. The default date format, used if none is specified in the tag. last-modified-at: date-format: '%d-%b-%y' #(like "04-Jan-14").
Usage
- layout 안에 넣는 기본방법
{% last_modified_at %}
- 1번에 Date format 적용 시킨 방법
{% last_modified_at %Y:%B:%A:%d:%S:%R %}
- jekyll object 메소드 호출을 통한 방법
{{ page.last_modified_at }}
- 3번에 Dateformat 적용 시킨 방법
{{ page.last_modified_at | date: '%Y:%B:%A:%d:%S:%R' }}
- 내가 사용한 방법
나는 post file의 페이지 안이 아니라 post file을 모아서 볼 수 있는 카드 레이아웃에서 나오길 바랬다. 그래서 원하는 레이아웃 안에다가 아래의 이미지 대로 넣어줬다.

Plugin이 Github Deploy시 적용 안되는 오류
해당 플러그인이 깃허브에 배포를 하니, 실행이 되지 않았다. 찾아보니, 깃허브에서 지원하는 플러그인이 아니라 적용되지 않는다고 한다. 그럼에도 불구하고 last_modified_at을 넣고 싶어서 방법을 찾았다.
<script>
window.addEventListener("load", function () {
if (document.getElementById("last-modified")) {
fetch("https://api.github.com/repos/moeun2/moeun2.github.io/commits?path=_posts/2022-05-30-jekyll-last-modified-at.md")
.then((response) => {
return response.json();
})
.then((commits) => {
var modified = commits[0]["commit"]["committer"]["date"].slice(0, 10);
return modified;
})
.then((modified) => {
var date = modified.split("-");
var year = date[0];
var month = console.log(date[0]);
document.getElementById("last-modified").textContent = "last_modified_at : " + modified;
});
}
});
</script>
---
<span class="post-metadata text-muted" id="last-modified"></span>
해당 Javascript코드는 Github API를 이용하여 해당 포스트의 깃허브 커밋내역을 가져와서 마지막 수정일을 가져온다.

💙You need to log in to GitHub to write comments.💙