kyb*_*kyb 10 html markdown pandoc
我通常会README.md像人们为 等创建的github那样Pandoc生成
<title></title>
Run Code Online (Sandbox Code Playgroud)
我想看到文件中的第一个标头。所以如果我有.md
# My README
text
## Second header
Run Code Online (Sandbox Code Playgroud)
Pandoc 应该生成
<title>My README</title>
Run Code Online (Sandbox Code Playgroud)
从第一个 1-# 标头生成会很好。所以
### Preface
# My README
text
## Second header
Run Code Online (Sandbox Code Playgroud)
仍然应该是
<title>My README</title>
Run Code Online (Sandbox Code Playgroud)
无论如何,我想避免.md使用元信息来扩展我的内容,这不是简单 Markdown 标准的一部分。
pandoc -s test.md -o test.html --元数据标题=标题名称
Pandoc 有一个选项:--shift-heading-level-by. 该选项可用于提升或降级文档中的所有标题,并且移动到“顶级之上”的第一个标题将用作标题。
对于默认的 GitHub README.md,以下内容最有效:
pandoc --shift-heading-level-by=-1 --standalone --from=gfm ...
Run Code Online (Sandbox Code Playgroud)
格式说明符gfm代表“GitHub Flavored Markdown”。
这种方法的缺点是二级标题作为<h1>元素输出,这可能是不受欢迎的。在这种情况下,最通用的解决方案是使用Lua 过滤器:
function Pandoc (doc)
doc.blocks:walk {
Header = function (h)
-- use top-level heading as title, unless the doc
-- already has a title
if h.level == 1 and not doc.meta.title then
doc.meta.title = h.content
return {} -- remove this heading from the body
end
end
}
return doc
end
Run Code Online (Sandbox Code Playgroud)
可以通过将其保存到文件readme-title.lua并通过 传递给 pandoc 来使用上面的内容--lua-filter=readme-title.lua。