パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v1.3.2

html2md

HTML を Markdown に変換するためのライブラリです。CommonMark、簡単なテーブル、カスタム変換ルールをサポートしています。

AndroidiOSWebmacOSWindowsLinux
30日間ダウンロード
7.8万
いいね数
63
Pub ポイント
140

パッケージをインストール

flutter pub add html2md

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

HTMLコンテンツをMarkdown形式に変換するFlutter/Dartパッケージで、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 件

ソースリポジトリ

このパッケージを使用しているプロジェクト