一开始我用 Hugo 做中文网站时打算在文章里用中文标签,但是链接里面的中文会被转换为人类读不懂的字符。比如:/zh-cn/tags/博客/ 会变成 /zh-cn/tags/%E5%8D%9A%E5%AE%A2/。我不喜欢这样,当时搜索一番也没找到办法,就用英文标签了。现在终于知道怎么改了,最终改为链接里面还是用英文,但是页面就显示中文。
下面是针对 hugo-theme-jane(ef8a126)主题的更改方法,方法是通用的,但是不同主题修改的部分可能不同。开始前先厘清两个概念:标签名(tag name)、标签标题(tag title)。在文章前页(front matter)写的是标签名,用于链接;在 _index.md 文件定义的是 title 是标签标题,它会显示于页面。
#!/usr/bin/env python importosimportsysdefcreate_tag_title(directory,tag_name,tag_title):# Construct the path to the tag directorytag_dir=os.path.join(directory,'tags',tag_name)# Make the directories if they don't existos.makedirs(tag_dir,exist_ok=True)# Define the path for the _index.md fileindex_file_path=os.path.join(tag_dir,'_index.md')# Define the content of the _index.md fileindex_content=f"""---
title: {tag_title}---
"""# Write the content to the _index.md filewithopen(index_file_path,'w')asindex_file:index_file.write(index_content)print(f"Created {index_file_path} with title: {tag_title}")if__name__=='__main__':# Ensure the correct number of arguments are providediflen(sys.argv)!=4:print("Usage: create-tag-title directory tag-name tag-title")sys.exit(1)# Read the arguments from the command linedirectory=sys.argv[1]tag_name=sys.argv[2]tag_title=sys.argv[3]# Call the function to create the tag title filecreate_tag_title(directory,tag_name,tag_title)
自己写提交信息还是太繁琐,让 ChatGPT 把这部分也加上。
我说的话:
Besides creating the _index.md, I want to stage content/ directory and commit. Example:
#!/usr/bin/env pythonimportosimportsysimportsubprocessdefcreate_tag_title(directory,tag_name,tag_title):# Construct the path to the tag directorytag_dir=os.path.join(directory,'tags',tag_name)# Make the directories if they don't existos.makedirs(tag_dir,exist_ok=True)# Define the path for the _index.md fileindex_file_path=os.path.join(tag_dir,'_index.md')# Define the content of the _index.md fileindex_content=f"""---
title: {tag_title}---
"""# Write the content to the _index.md filewithopen(index_file_path,'w')asindex_file:index_file.write(index_content)print(f"Created {index_file_path} with title: {tag_title}")# Stage the content directorytry:subprocess.run(['git','add',directory],check=True)print(f"Staged {directory}")# Commit the changescommit_message=f'tag-title: {tag_name} -> {tag_title}'subprocess.run(['git','commit','-m',commit_message],check=True)print(f"Committed with message: '{commit_message}'")exceptsubprocess.CalledProcessErrorase:print(f"Error during git operation: {e}")if__name__=='__main__':# Ensure the correct number of arguments are providediflen(sys.argv)!=4:print("Usage: create-tag-title directory tag-name tag-title")sys.exit(1)# Read the arguments from the command linedirectory=sys.argv[1]tag_name=sys.argv[2]tag_title=sys.argv[3]# Call the function to create the tag title filecreate_tag_title(directory,tag_name,tag_title)
Disqus 评论。如果评论未加载,Disqus 可能被你的互联网服务提供商屏蔽。