使用 Jekyll Hooks 修改帖子 Markdown 中的 img 标签

发布于 2025-01-15 07:51:20 字数 513 浏览 5 评论 0原文

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

在 markdown 文件中:

![Apple](images/apple.jpg)

渲染图像标签:

<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:

![Apple](images/apple.jpg)

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴梦一场 2025-01-22 07:51:20

将帖子写入磁盘之前调用的最后一个钩子是 :posts, :post_render。我们可以修改此钩子处的output,以对渲染的帖子和草稿帖子进行编辑,无论它们最初是用 Markdown 还是 HTML 编写的:

module JekyllPluginHooks
  Jekyll::Hooks.register(:posts, :post_render) do |post|
    post.output.gsub!('<img src="images/', '<img src="/cdn-cgi/image/width=80,quality=75/images/')
  end
end

术语

  • Jekyll post 包括草稿和博客文章。
  • Jekyll 文档 包含所有集合中的网页,包括草稿和博客文章。
  • Jekyll page 是不属于集合的网页,例如帖子或草稿。

为了完整性

要修改每个集合中的所有网页,而不仅仅是草稿和帖子,请使用 :documents 挂钩而不是 :posts 挂钩上面的代码示例:

module JekyllPluginHooks
  Jekyll::Hooks.register(:documents, :post_render) do |document|
    document.output.gsub!('<img src="images/', '<img src="/cdn-cgi/image/width=80,quality=75/images/')
  end
end

要同时修改不在集合中的网页(例如,/index.html)还要写:

module JekyllPluginHooks
  Jekyll::Hooks.register(:pages, :post_render) do |page|
    page.output.gsub!('<img src="images/', '<img src="/cdn-cgi/image/width=80,quality=75/images/')
  end
end

如果我们想要修改所有网页,我们可以重写上面的代码并将公共代码提取到一个名为的新方法中modify_output

module JekyllPluginHooks
  def modify_output
    Proc.new do |webpage| 
      webpage.output.gsub! \
        '<img src="images/', \
        '<img src="/cdn-cgi/image/width=80,quality=75/images/'
    end
  end

  module_function :modify_output

  Jekyll::Hooks.register(:documents, :post_render, &modify_output)
  Jekyll::Hooks.register(:pages, :post_render, &modify_output)
end

参考

The very last hook that gets called before writing posts to disk is :posts, :post_render. We can modify output at this hook to make edits to rendered posts and draft posts, regardless of whether they were originally written in Markdown or HTML:

module JekyllPluginHooks
  Jekyll::Hooks.register(:posts, :post_render) do |post|
    post.output.gsub!('<img src="images/', '<img src="/cdn-cgi/image/width=80,quality=75/images/')
  end
end

Terminology

  • A Jekyll post includes drafts and blog posts.
  • A Jekyll document includes web pages in all collections, including drafts and blog posts.
  • A Jekyll 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:

module JekyllPluginHooks
  Jekyll::Hooks.register(:documents, :post_render) do |document|
    document.output.gsub!('<img src="images/', '<img src="/cdn-cgi/image/width=80,quality=75/images/')
  end
end

To also modify web pages that are not in a collection (for example, /index.html) also write:

module JekyllPluginHooks
  Jekyll::Hooks.register(:pages, :post_render) do |page|
    page.output.gsub!('<img src="images/', '<img src="/cdn-cgi/image/width=80,quality=75/images/')
  end
end

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:

module JekyllPluginHooks
  def modify_output
    Proc.new do |webpage| 
      webpage.output.gsub! \
        '<img src="images/', \
        '<img src="/cdn-cgi/image/width=80,quality=75/images/'
    end
  end

  module_function :modify_output

  Jekyll::Hooks.register(:documents, :post_render, &modify_output)
  Jekyll::Hooks.register(:pages, :post_render, &modify_output)
end

References

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文