Github 블로그 md파일 자동 생성
by BenzeneKim
Github blog 작성 자동화
Github 블로그를 위한 파일 생성에는 다음과 같은 조건이 따른다.
- yyyy-mm-dd-title.markdown 형식의 제목
- 해당 파일의 metadata를 포함하는 heading
두 조건이 충족되지 않는 경우 글이 Post 형식으로 보이지 않는 문제가 발생하거나, 파일 자체가 누락되어 페이지에 나타나지 않는 등의 문제를 초래한다. 따라서 해당 문제가 발생하지 않도록 글의 생성을 자동화하는 배치파일을 작성하였다.
@echo off
:: Get the current date
for /f "tokens=1-4 delims=/- " %%a in ('wmic os get localdatetime ^| find "."') do (
set datetime=%%a
)
:: Extract year, month, and day from the datetime
set year=%datetime:~0,4%
set month=%datetime:~4,2%
set day=%datetime:~6,2%
:: Ask the user for a title, categories, and tags
set /p customTitle=Enter the custom title for the file:
set /p categories=Enter the categories (comma-separated):
set /p tags=Enter the tags (comma-separated):
:: Replace spaces in customTitle with hyphens
set customTitle=%customTitle: =-%
:: Create the filename with the format yyyy-mm-dd-customTitle.markdown
set filename=%year%-%month%-%day%-%customTitle%.markdown
echo Creating file: %filename%
echo. > %filename%
:: Get the current time
set hour=%datetime:~8,2%
set minute=%datetime:~10,2%
set second=%datetime:~12,2%
set currenttime=%hour%:%minute%:%second%
:: Write default heading content for a GitHub blog post
(
echo ---
echo layout: post
echo title: "%customTitle%"
echo date: %year%-%month%-%day% %currenttime%
echo author: BenzeneKim
echo categories: %categories%
echo tags: %tags%
echo cover: "/assets/instacode.png"
echo published: true
echo ---
) > %filename%
echo File created and content written to %filename%
pause
사용 방법
아래와 같이 경로를 _post 로 이동한 후 Batch file을 실행한다.
PS Blog_Repo_Path > cd _post
PS Blog_Repo_Path > ./generate.bat