NodeJS中html转markdown

turndown 库

const TurndownService = require('turndown');
const turndownService = new TurndownService();

const htmlContent = '<h1>Hello, World!</h1><p>This is a <em>paragraph</em>.</p>';
const markdownContent = turndownService.turndown(htmlContent);

console.log(markdownContent);

用 Turndown 的 addRule 方法添加自定义规则

const TurndownService = require("turndown");
const turndownService = new TurndownService();
// 添加处理标题的规则
turndownService.addRule("heading", {
	filter: ["h1", "h2", "h3", "h4", "h5", "h6"],
	replacement: function (content, node, options) {
		var hLevel = Number(node.nodeName.charAt(1));
		return Array(hLevel + 1).join("#") + " " + content + "\n";
	},
});
// 添加处理代码块的规则
turndownService.addRule('code', {
    filter: function (node) {
      return (node.tagName === 'PRE' && node.firstChild && node.firstChild.tagName === 'CODE' && node.firstChild.className);
    },
    replacement: function (content, node, options) {
      var lang = node.firstChild.className.match(/language-(\w+)/);
      if (lang) {
        return '\n```' + lang[1] + '\n' + node.firstChild.textContent + '\n```\n';
      }
      return '\n```\n' + node.firstChild.textContent + '\n```\n';
    }
  });

让 Turndown 识别 <code> 标签中的类名,比如 <pre><code class=“prism language-js”> )并根据类名来确定代码块的语言,你可以在处理代码块的规则中进行相应的修改。

上一篇:docker-compose搭建MongoDB


下一篇:C++设计模式|创建型 2.工厂模式