使用 Jekyll Hooks 修改帖子 Markdown 中的 img 标签
我有一个 Jekyll 博客,有 1000 多篇帖子。现在我计划使用 Cloudflare Image Resizing 来优化网站。为了实现这一点,我需要修改从 Markdown 文件呈现的图像标签。
在 markdown 文件中:

渲染图像标签:
<img src="images/apple.jpg" alt="Apple">
我想要的样子:
<img src="/cdn-cgi/image/width=80,quality=75/images/apple.jpg" alt="Apple" >
提前致谢
I have a Jekyll blog with 1,000s of posts. Now I'm planning to use Cloudflare Image Resizing to optimize the website. To make it happen I need to modify the image tag rendered from the markdown files.
In markdown file:

Rendered image tag:
<img src="images/apple.jpg" alt="Apple">
How I want it to be:
<img src="/cdn-cgi/image/width=80,quality=75/images/apple.jpg" alt="Apple" >
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将帖子写入磁盘之前调用的最后一个钩子是
:posts, :post_render
。我们可以修改此钩子处的output
,以对渲染的帖子和草稿帖子进行编辑,无论它们最初是用 Markdown 还是 HTML 编写的:术语
post
包括草稿和博客文章。文档
包含所有集合中的网页,包括草稿和博客文章。page
是不属于集合的网页,例如帖子或草稿。为了完整性
要修改每个集合中的所有网页,而不仅仅是草稿和帖子,请使用
:documents
挂钩而不是:posts
挂钩上面的代码示例:要同时修改不在集合中的网页(例如,
/index.html
)还要写:如果我们想要修改所有网页,我们可以重写上面的代码并将公共代码提取到一个名为的新方法中
modify_output
:参考
The very last hook that gets called before writing posts to disk is
:posts, :post_render
. We can modifyoutput
at this hook to make edits to rendered posts and draft posts, regardless of whether they were originally written in Markdown or HTML:Terminology
post
includes drafts and blog posts.document
includes web pages in all collections, including drafts and blog posts.page
is a web page that does not belong to a collection, such as posts or drafts.For Completeness
To modify all web pages in every collection, not just drafts and posts, use the
:documents
hook instead of the:posts
hook in the above code example:To also modify web pages that are not in a collection (for example,
/index.html
) also write:If we want all web pages to be modified, we can rewrite the above and extract the common code to a new method called
modify_output
:References