从 Markdown 文件中删除 HTML 注释

hoi*_*jui 5 html bash markdown awk pandoc

这在从 Markdown 转换为 HTML 时会派上用场,例如,如果需要防止注释出现在最终的 HTML 源代码中。

示例输入my.md

# Contract Cancellation

Dear Contractor X, due to delays in our imports, we would like to ...
<!--
    ... due to a general shortage in the Y market
    TODO make sure to verify this before we include it here
-->
best,
me <!-- ... or should i be more formal here? -->
Run Code Online (Sandbox Code Playgroud)

示例输出my-filtered.md

# Contract Cancellation

Dear Contractor X, due to delays in our imports, we would like to ...

best,
me
Run Code Online (Sandbox Code Playgroud)

在 Linux 上,我会做这样的事情:

cat my.md | remove_html_comments > my-filtered.md
Run Code Online (Sandbox Code Playgroud)

我还能够编写处理一些常见情况的 AWK 脚本,但据我所知,无论是 AWK 还是任何其他用于简单文本操作的常用工具(如sed),都无法真正胜任这项工作。人们需要使用 HTML 解析器。

如何编写合适的remove_html_comments脚本,使用什么工具?

Chr*_*ris 6

我从您的评论中看到您主要使用 Pandoc。

Pandoc 2.0 版于 2017 年 10 月 29 日发布,添加了一个新选项--strip-comments。在相关的问题提供了一些背景这一变化。

--strip-comments作为转换过程的一部分,升级到最新版本并添加到您的命令应该删除 HTML 注释。