New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

ohmyreader

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ohmyreader

A powerful web content extractor that converts articles to clean markdown

latest
Source
npmnpm
Version
0.1.1
Version published
Weekly downloads
2
-33.33%
Maintainers
1
Weekly downloads
 
Created
Source

OhMyReader

一个强大的网页内容提取库,支持多种格式和编码,可以将网页内容转换为结构化的 Markdown 格式。

特性

  • 🚀 自动选择最佳提取器
  • 📝 转换为干净的 Markdown 格式
  • 🔍 智能提取元数据(标题、作者、日期)
  • 🌐 支持多种编码(UTF-8、GBK 等)
  • ⚡ 异步操作,支持并发
  • 🛡️ 内置内容验证
  • 🔄 自动重试机制

安装

npm install ohmyreader
# 或
bun add ohmyreader

基础使用

import { extractContent } from 'ohmyreader';

// 简单用法
const result = await extractContent('https://example.com/article');

// 或使用链式调用
import { Reader } from 'ohmyreader';

const result = await new Reader()
  .from('https://example.com/article')
  .extract();

高级用法

链式配置

import { Reader } from 'ohmyreader';

const result = await new Reader()
  .from('https://example.com/article')
  .withValidation({
    enabled: true,
    minLength: 100,
    maxNavRatio: 0.3
  })
  .withRequestOptions({
    timeout: 5000,
    retries: 3,
    headers: {
      'User-Agent': 'Custom User Agent'
    }
  })
  .withExtractOptions({
    preferParser: true,
    includeComments: true,
    includeMeta: true
  })
  .extract();

传统选项方式

const result = await extractContent('https://example.com/article', {
  validate: {
    enabled: true,
    minLength: 100,
    maxNavRatio: 0.3
  },
  request: {
    timeout: 5000,
    retries: 3,
    headers: {
      'User-Agent': 'Custom User Agent'
    }
  },
  extract: {
    preferParser: true,
    includeComments: true,
    includeMeta: true
  }
});

配置选项

interface ExtractOptions {
  // 内容验证选项
  validate?: {
    enabled?: boolean;        // 是否启用内容验证
    minLength?: number;       // 最小内容长度
    maxNavRatio?: number;     // 最大导航内容比例
  };
  
  // 网络请求选项
  request?: {
    timeout?: number;         // 超时时间(毫秒)
    retries?: number;         // 重试次数
    headers?: HeadersInit;    // 自定义请求头
  };
  
  // 提取选项
  extract?: {
    includeComments?: boolean;  // 是否包含评论
    preferParser?: boolean;     // 是否优先使用 Parser
    includeMeta?: boolean;      // 是否包含元数据
  };
}

返回结果类型

interface ExtractResult {
  title: string;          // 文章标题
  author: string;         // 作者
  publishDate: string;    // 发布日期
  markdown: string;       // Markdown 格式的正文
  excerpt: string;        // 摘要
  leadImageUrl?: string | null;  // 主图 URL
  domain?: string;        // 域名
  wordCount?: number;     // 字数统计
  url: string;           // 原始 URL
}

错误处理

try {
  const result = await new Reader()
    .from('https://example.com/article')
    .extract();
} catch (error) {
  if (error.message.includes('URL未设置')) {
    // 处理 URL 错误
  } else if (error.message.includes('提取失败')) {
    // 处理提取失败
  } else if (error.message.includes('网络错误')) {
    // 处理网络错误
  }
}

注意事项

  • 某些网站可能需要登录或有反爬虫措施
  • 建议设置适当的请求间隔
  • 注意遵守目标网站的使用条款

贡献

欢迎提交 Issue 和 Pull Request!

许可证

MIT

Keywords

readability

FAQs

Package last updated on 12 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts