搜尋套件、儲存庫與 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 項

原始碼儲存庫

使用此套件的專案