@web-readable/core
v0.1.5
Published
Fast article extraction and HTML-to-Markdown conversion powered by Rust.
Readme
web-readable
Rust 製の Web 本文抽出器です。HTML5 セマンティクス、Defuddle 由来の selector ベース除去、再抽出、HTML 標準化、Markdown 変換、サイト別 extractor を 1 つの抽出パイプラインで処理します。
API
公開入口は extract と extract_from_url の2つです。Markdown が必要な場合も別 API ではなく ExtractOptions.markdown を使います。
use web_readable::{extract, ExtractOptions};
let result = extract(
html,
&ExtractOptions {
markdown: true,
decode_embeds_as_urls: true,
..ExtractOptions::default()
},
)?;
println!("{}", result.content_html);
println!("{}", result.content_markdown.unwrap_or_default());主な結果:
title,content_html,content_markdowntext_content,length,word_countscore,quality_score,parse_time_msmetadata(author、description、canonical URL、domain、image、favicon、schema.org 等)debug(有効化時の再抽出回数と診断情報)
Node.js / npm
npm install @web-readable/coreconst { extract, extract_from_url } = require('@web-readable/core')
const result = extract('<article><h1>タイトル</h1><p>本文です。</p></article>', {
markdown: true,
debug: true,
})
console.log(result.contentHtml)
console.log(result.contentMarkdown)
console.log(result.metadata.author)URL の抽出も同じ戻り値を返します。Node.js では CDP 接続先を dynamicOptions で指定します。
const result = await extract_from_url(
'https://example.com/article',
{ cdpEndpoint: 'ws://127.0.0.1:9222/devtools/browser/<id>' },
{ markdown: true },
)主なオプション
ExtractOptions には次の制御があります。
markdown,decode_embeds_as_urlsstandardizecontent_selectorremove_exact_selectors,remove_partial_selectorsremove_hidden_elements,remove_low_scoring,remove_small_imagesinclude_imagesmin_candidate_text,min_output_text,sibling_score_ratiomerge_paginated_content,max_paginated_pagesdebug,language
動的ページ
dynamic はデフォルトで有効です。既存 Chromium / Chrome の CDP endpoint からレンダリング済み HTML を取得できます。Lightpanda endpoint は自動検出されます。
[dependencies]
web_readable = "0.1.4"use web_readable::{extract_from_url, DynamicOptions, ExtractOptions};
let result = extract_from_url(
"https://example.com/article",
&DynamicOptions::new("ws://127.0.0.1:9222/devtools/browser/<id>"),
&ExtractOptions { markdown: true, ..ExtractOptions::default() },
).await?;merge_paginated_content を有効にすると、同一オリジンの rel="next"、次へ、続き リンクを最大 max_paginated_pages まで結合します。
サイト別 extractor
共通パイプラインの前にサイト別 extractor を実行できます。現在は Wikipedia を実装しています。
.mw-parser-outputを本文ルートとして選択- navbox、references、編集 UI、カテゴリ等を除去
- 共通サニタイズ、メタデータ、Markdown 変換を再利用
追加 extractor は src/site_extractors.rs の SiteExtractor trait と registry に実装します。
HTML 標準化
抽出後に以下を標準化します。
- title と重複する最初の H1 の除去
- H1 → H2
language-*/lang-*のコード言語保持- callout / alert の blockquote 化
- footnote 用 ID と安全な属性の保持
- カスタム要素の unwrap と危険 URL の除去
Markdown 変換層は変換だけを担当し、HTML 標準化は抽出パイプライン側で行います。
CLI
# HTML ファイルを Markdown 化
cargo run --bin web_readable -- page.html --markdown
# stdin
cat page.html | cargo run --bin web_readable -- - --markdown
# URL(CDP)
cargo run --bin web_readable -- \
https://example.com/article --cdp 127.0.0.1:9222 --markdown
# JSON、frontmatter、property、golden 比較
cargo run --bin web_readable -- page.html --json
cargo run --bin web_readable -- page.html --markdown --frontmatter --output article.md
cargo run --bin web_readable -- page.html --property title
cargo run --bin web_readable -- page.html --markdown --expected expected.md主な CLI オプションは --markdown、--json、--frontmatter、--cdp、--output、--expected、--property、--content-selector、--no-images、--min-output-text、--debug です。
テスト
cargo test