Machine-translated from Chinese.
分享一下搭建个人博客的过程,包括一些细节的处理等等,希望会有帮助。
Hexo环境配置
我主要参考的文章是这个知乎专栏。前三篇是GitHub使用相关,四六篇若不希望改变域名则非必要(穷学生不打算买域名,也没找到免费的方法QAQ)。我是从第五篇开始的,按步骤安装node.js后,在某个文件夹内搭建Hexo环境即可。
接下来是第七篇,我设置的是keep主题,与专栏里的主题设置方法大同小异。最后便是第八篇了,只需要选取一些感兴趣的功能,单独看单独设置即可。Hexo博客的功能是插件式管理的,很方便,只需安装想要的插件,然后在主题配置文件或网站配置文件(_config.yml)内设置一下即可。使用hexo new post title即可创建新文章,写好后在网站根目录运行hexo g和hexo d指令即可完成网页的部署。若有对配置文件进行更改,则最好运行hexo clean之后再运行hexo g,hexo d命令。
数学公式配置
为了更新数学笔记,数学公式是必不可少的。一种方法是直接上传pdf(需要相关插件支持),但效果不是很好,因此我打算沿用我在知乎上传笔记的方法,先将LaTeX转成Markdown再处理。这一步利用pandoc实现,可参考我的文章。接下来还需要令网站能够渲染Markdown文件中的数学公式,可参考文章,需要先卸载初始渲染引擎xxx-marked,更换为xxx-kramed,然后要取消对部分符号的转义,最后在主题配置文件中开启mathjax,并在每篇文章中设置mathjax开关即可。
值得一提的是,和LaTeX一样,Markdown也是回车默认不换行,两次回车才换行的,但Hexo博客的默认设置不是这样。参考博客,浏览“后续”部分即可。
图床配置
笔记没有图片当然不行。一种方法是直接将图片放到博客环境的文件夹里,然后利用md文件上传图片的方法,直接将链接设置为图片(相对)位置即可。这种方法简单但是会占用仓库空间。另一种方法是利用图床,我采用的是SMMS,免费且API简洁易懂。这样将图片上传到图床上,令链接为图床生成的图片链接,就可以节省仓库空间了。使用sm.ms图床时,需要注册用户,在API token页面生成令牌。
为了让博客上传更加自动化,图床上传图片的过程当然要用程序实现,在此把我的python代码贴出来:
smms.py(参考GitHub)
import requests
import json
class SMMS(object):
# init
def __init__(self, username, password):
self.username = username
self.password = password
self.token = None
self.profile = None
self.history = None
self.upload_history = None
self.url = None
self.headers = None
# user
def get_api_token(self):
data = {
'username': self.username,
'password': self.password,
}
url = root+'token'
res = requests.post(url, data=data).json()
self.token = res['data']['token']
self.headers = {'Authorization': self.token}
# user
def get_user_profile(self):
url = root+'profile'
res = requests.post(url, headers=self.headers).json()
self.profile = res['data']
print(json.dumps(res, indent=4))
# image
def clear_temporary_history(self):
data = {
'format': 'json'
}
url = root+'clear'
res = requests.get(url, data=data).json()
print(json.dumps(res, indent=4))
# image
def view_temporary_history(self):
url = root+'history'
res = requests.get(url).json()
self.history = res['data']
print(json.dumps(res, indent=4))
# image
def delete_image(self, hash):
url = root+'delete/'+hash
res = requests.get(url).json()
print(json.dumps(res, indent=4))
# image
def view_upload_history(self):
url = root+'upload_history'
res = requests.get(url, headers=self.headers).json()
self.upload_history = res['data']
print(json.dumps(res, indent=4))
# image
def upload_image(self, path):
try:
files = {'smfile': open(path, 'rb')}
url = root+'upload'
res = requests.post(url, files=files, headers=self.headers).json()
if res['success']:
self.url = res['data']['url']
return self.url
else:
return res['images']
except Exception as e:
print(e)
return 'https://i.loli.net/'
root = 'https://sm.ms/api/v2/'
smms = SMMS('username', 'password')
smms.get_api_token()
def upload(picture):
return smms.upload_image(picture)
main.py(补充自我的文章)
import re
import smms
zhihu = open("zhihu.md", "r+", encoding="utf-8")
contents = zhihu.readlines()
for i in range(len(contents)):
if re.match(':::', contents[i]):
contents[i] = ""
j = i+1
while not re.match(':::', contents[j]):
contents[j] = re.sub(r'\n', ' ', contents[j])
j = j+1
contents[j] = "\n"
if re.match(r'!.*\([\w\d\s]*\.png\)', contents[i]):
print("Uploading pictures...")
ret = re.search(r'[\w\d\s]*\.png', contents[i])
path = ret.group()
contents[i] = re.sub(path, smms.upload(path), contents[i])
print("Done.")
while re.match(r'.*}}', contents[i]):
contents[i] = re.sub(r'}}', r'} }', contents[i])
while re.match(r'.*{{', contents[i]):
contents[i] = re.sub(r'{{', r'{ {', contents[i])
while re.match(r'.*{#.*}', contents[i]):
contents[i] = re.sub(r'{#.*}', r'', contents[i])
zhihu.seek(0)
zhihu.truncate()
zhihu.writelines(contents)
zhihu.close()
这样基本的配置就已经完成了,其它自动化的部分我单开一篇文章继续说明。
Share the process of setting up a personal blog, including the handling of some details, etc. I hope it will be helpful.
Hexo environment configuration
The article I mainly refer to is this Zhihu column . The first three articles are related to the use of GitHub, and the four or six articles are not necessary if you do not want to change the domain name (poor students do not plan to buy domain names, and have not found a free method QAQ). I started from the fifth article. After installing node.js according to the steps, just set up the Hexo environment in a certain folder.
Next is the seventh article. I set up the keep theme, which is similar to the theme setting method in the column. Finally comes the eighth article. You only need to select some functions that you are interested in and look at the settings separately. The functions of Hexo blog are managed by plug-ins, which is very convenient. You only need to install the plug-ins you want, and then set them in the theme configuration file or website configuration file (_config.yml). Use hexo new post title to create a new article. After writing, run the hexo g and hexo d commands in the root directory of the website to complete the deployment of the web page. If there are changes to the configuration file, it is best to run hexo clean and then hexo g, hexo d commands.
Math formula configuration
In order to update math notes, math formulas are essential. One method is to directly upload pdf (requires relevant plug-in support), but the effect is not very good, so I plan to follow the method I used to upload notes on Zhihu, first convert LaTeX to Markdown and then process it. This step is implemented using pandoc, please refer to my article . Next, you need to enable the website to render the mathematical formulas in the Markdown file. Please refer to Article , you need to uninstall the initial rendering engine xxx-marked first, replace it with xxx-kramed, then cancel the escaping of some symbols, and finally enable mathjax in the theme configuration file and set the mathjax switch in each article.
It is worth mentioning that, like LaTeX, Markdown does not break lines by default after carriage returns, but only breaks lines after two carriage returns, but the default setting of Hexo blog is not like this. Reference Blog , just browse to the "What's Next" section.
Image hosting configuration
Of course, notes don’t work without pictures. One method is to directly put the image into the folder of the blog environment, and then use the md file to upload the image, and directly set the link to the (relative) position of the image. This method is simple but takes up warehouse space. Another method is to use the image hosting. What I use is SMMS, free and the API is simple and easy to understand. In this way, you can save warehouse space by uploading the image to the image bed and making the link the image link generated by the image bed. When using the sm.ms image bed, you need to register a user and generate a token on the API token page.
In order to make blog uploading more automated, the process of uploading images to the image bed must of course be implemented using a program. I will post my python code here:
smms.py (reference GitHub)
import requests
import json
class SMMS(object):
# init
def __init__(self, username, password):
self.username = username
self.password = password
self.token = None
self.profile = None
self.history = None
self.upload_history = None
self.url = None
self.headers = None
# user
def get_api_token(self):
data = {
'username': self.username,
'password': self.password,
}
url = root+'token'
res = requests.post(url, data=data).json()
self.token = res['data']['token']
self.headers = {'Authorization': self.token}
# user
def get_user_profile(self):
url = root+'profile'
res = requests.post(url, headers=self.headers).json()
self.profile = res['data']
print(json.dumps(res, indent=4))
# image
def clear_temporary_history(self):
data = {
'format': 'json'
}
url = root+'clear'
res = requests.get(url, data=data).json()
print(json.dumps(res, indent=4))
# image
def view_temporary_history(self):
url = root+'history'
res = requests.get(url).json()
self.history = res['data']
print(json.dumps(res, indent=4))
# image
def delete_image(self, hash):
url = root+'delete/'+hash
res = requests.get(url).json()
print(json.dumps(res, indent=4))
# image
def view_upload_history(self):
url = root+'upload_history'
res = requests.get(url, headers=self.headers).json()
self.upload_history = res['data']
print(json.dumps(res, indent=4))
# image
def upload_image(self, path):
try:
files = {'smfile': open(path, 'rb')}
url = root+'upload'
res = requests.post(url, files=files, headers=self.headers).json()
if res['success']:
self.url = res['data']['url']
return self.url
else:
return res['images']
except Exception as e:
print(e)
return 'https://i.loli.net/'
root = 'https://sm.ms/api/v2/'
smms = SMMS('username', 'password')
smms.get_api_token()
def upload(picture):
return smms.upload_image(picture)
main.py (supplemented from my article )
import re
import smms
zhihu = open("zhihu.md", "r+", encoding="utf-8")
contents = zhihu.readlines()
for i in range(len(contents)):
if re.match(':::', contents[i]):
contents[i] = ""
j = i+1
while not re.match(':::', contents[j]):
contents[j] = re.sub(r'\n', ' ', contents[j])
j = j+1
contents[j] = "\n"
if re.match(r'!.*\([\w\d\s]*\.png\)', contents[i]):
print("Uploading pictures...")
ret = re.search(r'[\w\d\s]*\.png', contents[i])
path = ret.group()
contents[i] = re.sub(path, smms.upload(path), contents[i])
print("Done.")
while re.match(r'.*}}', contents[i]):
contents[i] = re.sub(r'}}', r'} }', contents[i])
while re.match(r'.*{{', contents[i]):
contents[i] = re.sub(r'{{', r'{ {', contents[i])
while re.match(r'.*{#.*}', contents[i]):
contents[i] = re.sub(r'{#.*}', r'', contents[i])
zhihu.seek(0)
zhihu.truncate()
zhihu.writelines(contents)
zhihu.close()
In this way, the basic configuration has been completed. I will continue to explain the other automated parts in a separate article.