修改blogdown分类RSS模板:解决标题错误复数化问题
title: Adventures in Shinylive
author: Tobias Fellinger
date: '2024-01-27'
slug: adventures-in-shinylive
categories:
- R
tags: - R
- shiny
- web-r
- shinylive
This will be a blogpost about porting a shiny app to shinylive.
## caving分类文章示例 ```yaml --- title: example caving post author: Tobias Fellinger date: '2024-01-27' slug: example-caving-post categories: - caving tags: - caving --- this is a test
配置文件config.yaml内容
baseurl: / languageCode: en-US title: "Tobias Fellinger's Blog" theme: hugo-lithium googleAnalytics: '' disqusShortname: '' ignoreFiles: - \.Rmd$ - \.Rmarkdown$ - _cache$ permalinks: post: /:year/:month/:day/:slug/ menu: main: - name: About url: /about/ - name: R url: /r/ - name: Caving url: /caving/ params: MathJaxCDN: //cdnjs.cloudflare.com/ajax/libs MathJaxVersion: 2.7.5 description: A website built through Hugo and blogdown. favicon: favicon.ico highlightjsCDN: //cdnjs.cloudflare.com/ajax/libs highlightjsLang: - r - yaml highlightjsTheme: github highlightjsVersion: 9.12.0 logo: alt: Logo height: 50 url: logo.png width: 50 markup: defaultMarkdownHandler: goldmark goldmark: renderer: unsafe: yes highlight: codeFences: no
解决方案
1. 无法通过配置文件直接修改
Hugo对分类RSS标题的复数化处理是模板层面的逻辑,无法通过config.yaml直接调整,必须自定义RSS模板。
2. 正确的模板路径
你需要创建分类/标签通用的术语页面RSS模板,路径为:
layouts/_default/terms.rss.xml
这个路径符合Hugo的模板查找优先级,会被分类页面的RSS生成逻辑识别,之前尝试的路径不符合规则所以未生效。
3. 模板修改步骤
- 复制主题自带的RSS模板(位置:
themes/hugo-lithium/layouts/_default/rss.xml)到上述路径。 - 找到模板中设置标题的代码段,原代码类似:
这里的<title>{{ .Title }} on {{ .Site.Title }}</title>.Title会自动对分类名做复数化处理,替换为以下任意一种格式:- 若要实现「R posts on ...」:
<title>{{ .Name }} posts on {{ .Site.Title }}</title> - 若要实现「Posts in R on ...」:
<title>Posts in {{ .Name }} on {{ .Site.Title }}</title>
.Name会获取分类的原始名称,不会被复数化。 - 若要实现「R posts on ...」:
4. 验证修改
修改完成后,重启blogdown::serve_site(),访问对应的分类RSS地址(如http://localhost:4321/caving/index.xml),即可看到更新后的标题。
内容的提问来源于stack exchange,提问作者snaut
相关产品推荐
相关产品推荐

