在 ruby 中是否有某种方法可以编辑 markdown 文件顶部的 YAML Frontmatter,就像 Jekyll 和 Middleman 中使用的那样?
类似于:
def update_yaml
#magic that changes A: 1 to A: 2 in Frontmatter block
end
然后我的 Markdown 文件将从
---
A: 1
---
# Title
Words. More words. This is the words part of the file.
到
---
A: 2
---
# Title
Words. More words. This is the words part of the file.
似乎唯一的选择是解析整个文件,然后重写整个文件,只更改所需的部分,但我希望有更好的东西。
最佳答案
最近我遇到了同样的问题,作为替代方案,您可以使用python-frontmatter
。它很容易使用。下面是更改 yaml 变量值的代码:
import frontmatter
import io
with io.open('File.md', 'r') as f:
post = frontmatter.load(f)
post['A'] = 2
# Save the file.
newfile = io.open(fname, 'w', encoding='utf8')
frontmatter.dump(post, newfile)
newfile.close()
更多示例可以访问this page
关于ruby - 在 markdown 文件中编辑 YAML Frontmatter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36948807/