When you're ready to use Jekyll to write articles, you certainly don't want to go through the hassle of manually creating files, changing file extensions, and adding yml syntax at the beginning of the text, right?
So you might think of creating a new post in Jekyll . To simplify all of this, wouldn't it be logical to write a script?
Don’t programmers all write code just to be lazy? At this time, Rake can come in handy.
How to use Jekyll to automatically generate articles?
In view of Jekyll's strict requirements for md title and yaml format, it is too cumbersome to manually write yaml every time, so using scripts to output content is a good choice.

First, check to see if rake is installed:
gem list rake
If it is not installed, install it:
gem install rake
Next, create a Rakefile and place it in the root directory of jekyll.
Then, copy the following code into the Rakefile:
require 'rake'
require 'yaml'
SOURCE = "."
CONFIG = {
'posts' => File.join(SOURCE, "_posts"),
'post_ext' => "md",
}
# Usage: rake post title="A Title"
desc "Begin a new post in #{CONFIG['posts']}"
task :post do
abort("rake aborted: '#{CONFIG['posts']}' directory not found.") unless FileTest.directory?(CONFIG['posts'])
title = ENV["title"] || "new-post"
slug = title.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
filename = File.join(CONFIG['posts'], "#{Time.now.strftime('%Y-%m-%d')}-#{slug}.#{CONFIG['post_ext']}")
if File.exist?(filename)
abort("rake aborted!") if ask("#{filename} already exists. Do you want to overwrite?", ['y', 'n']) == 'n'
end
puts "Creating new post: #{filename}"
open(filename, 'w') do |post|
post.puts "---"
post.puts "layout: post"
post.puts "title: \"#{title.gsub(/-/,' ')}\""
post.puts "category: "
post.puts "tags: []"
post.puts "---"
end
end # task :post
- This is just a simplified version.
Finally, enter at the command line:
rake post title="article name"
With a whoosh, you will be there _post In the folder, I saw a new article with the file name年-月-日-文章标题.md.
Hopefully, the article "How to Use Jekyll to Automatically Generate Article Creation Times and Easily Increase Blog Update Frequency?" shared on Chen Weiliang's blog ( https://www.chenweiliang.com/ ) will be helpful to you.
Feel free to share this article's link: https://www.chenweiliang.com/cwl-31597.html
