搜索插件、代码仓与 APP 分类

插件详情v1.3.2

html2md

一个用于将 HTML 转换为 Markdown 的库。它支持 CommonMark、简单表格和自定义转换规则。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
7.8万
喜欢数
63
Pub 分数
140

安装插件

flutter pub add html2md

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个 Flutter/Dart 包,可将 HTML 内容转换为 Markdown 格式,支持 CommonMark 标准、基本表格渲染和可自定义的转换规则。它非常适合需要将富 HTML 文本转换为简洁易读的 Markdown 的应用,例如内容编辑器、文档工具或博客平台。该库兼容所有主要平台,包括 Android、iOS、Web、macOS、Windows 和 Linux,使其在跨平台开发中具有高度灵活性。其可扩展性允许开发者为特定用例定义自定义解析逻辑。

适合场景

  • 将HTML邮件转换为Markdown
  • 从HTML源渲染博客文章
  • 使用 Markdown 输出构建内容编辑器
  • 将用户生成的 HTML 转换为结构化 Markdown

主要特点

  • 支持 CommonMark 规范
  • 处理简单表格
  • 可自定义的转换规则
  • 跨平台兼容性
  • 轻量且易于集成

适合对象

  • 内容管理系统
  • 文档生成器
  • 基于 Markdown 的笔记应用
  • 处理用户提交的 HTML 的应用

使用建议

  • 对复杂 HTML 结构的支持有限
  • 自定义规则需要额外的实现
  • 可能无法保留所有HTML样式属性
  • 未针对非常大的 HTML 文档进行优化

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

English | 简体中文

html2md

Pub Build status

A library for converting HTML to Markdown in Dart. It supports CommonMark, simple table and custom converting rules.

Usage

A simple usage example:

import 'package:html2md/html2md.dart' as html2md;

main() {
    var html = '<h1>HTML2MD Demo</h1>';
    print(html2md.convert(html));
}

In flutter, you can use the flutter_markdown for rendering:

import 'package:html2md/html2md.dart' as html2md;
import 'package:flutter_markdown/flutter_markdown.dart';

build(BuildContext context) {
    // Caution!
    // This is not best practice, you should not call convert in build
    // and the converted markdown should put in state.
    var html = '<h1>HTML2MD Demo</h1>';
    var markdown = html2md.convert(html);
    return Markdown(data: markdown);
}

Config

You can config convert style by passing styleOptions to convert, elements that should be ignored also can be set with ignore. If you want to customize element converting, use custom rules!

html2md.convert(html,
    styleOptions: {'headingStyle': 'atx'},
    ignore: ['script'],
    rules: [Rule('custom')]);

The default and available style options:

Name Default Options
headingStyle "setext" "setext", "atx"
hr "* * *" "* * *", "- - -", "_ _ _"
bulletListMarker "*" "*", "-", "_"
codeBlockStyle "indented" "indented", "fenced"
fence "```" "```", "~~~"
emDelimiter "_" "_", "*"
strongDelimiter "**" "**", "__"
linkStyle "inlined" "inlined", "referenced"
linkReferenceStyle "full" "full", "collapsed", "shortcut"

Table support

Basic table converting is supported!

Html table source:

<table>
    <tr>
        <th>First Header</th>
        <th>First Header</th> 
    </tr>
    <tr>
        <td>Content Cell</td>
        <td>Content Cell</td>
    </tr>
    <tr>
        <td>Content Cell</td>
        <td>Content Cell</td>
    </tr>
</table>

The converted markdown source:

| First Header  | Second Header |
| ----- | ----- |
| Content Cell  | Content Cell  |
| Content Cell  | Content Cell  |

The converted markdown table:

First Header Second Header
Content Cell Content Cell
Content Cell Content Cell

Custom Rules

Want to customize element converting? Write your rules!

Rule fields explaination

final String name; // unique rule name
final List<String>? filters; // simple element name filters, e.g. ['aside']
final FilterFn? filterFn; // function for building complex element filter logic
final Replacement? replacement; // function for doing the replacing
final Append? append; // function for appending content

Rule example - Convert the onebox section of discourse post to a link

<aside class="onebox">
  <header class="source">
      <img src="https://discoursesite/uploads/default/original/1X/test.png" class="site-icon" width="32" height="32">
      <a href="https://events.google.com/io/program/content?4=topic_flutter&amp;lng=zh-CN" target="_blank" rel="noopener">Google I/O 2021</a>
  </header>
</aside>
Rule(
  'discourse-onebox',
  filterFn: (node) {
    // Find aside with onebox class
    if (node.nodeName == 'aside' &&
        node.className.contains('onebox')) {
        return true;
    }
    return false;
  },
  replacement: (content, node) {
    // find the first a element under header
    var header = node.firstChild;
    var link = header!
        .childNodes()
        .firstWhere((element) => element.nodeName == 'a');
    var href = link.getAttribute('href');
    if (href != null && href.isNotEmpty) {
      return '[$href]($href)'; // build the link
    }
    return '';
  },
)

Test

dart run test

Features and bugs

Please file feature requests and bugs at the issue tracker.

Inspiration

html2md is basicly a simplify version of Node's turndown

License

Created from templates made available by Stagehand under a BSD-style license.

所属分类

依赖

依赖清单 2 项

源码仓库

使用此插件的项目