read.read_as_markdown()

Read a document from a path or URL and convert it to Markdown.

Usage

Source

read.read_as_markdown(
    uri, html_extract_selectors=None, html_zap_selectors=None, *args, **kwargs
)

This is raghilda’s entry point for turning a source into a MarkdownDocument ready for chunking. It reads the URI (a local file or an HTTP(S) URL) and converts it to Markdown using MarkItDown, so it handles far more than Markdown files: HTML pages, PDFs, Word (.docx) documents, and the other formats MarkItDown supports are all converted to Markdown. For HTML, it keeps the <main> element and removes <nav> by default; use the selector arguments to change that.

Parameters

uri: str

Path or URL of the source to read. Supported forms include:

  • path/to/file.md (or .html, .pdf, .docx, and so on)
  • http://example.com/page
  • https://example.com/page
html_extract_selectors: Optional[list[str]] = None

CSS selectors naming the parts of an HTML page to keep. Defaults to ['main'].

html_zap_selectors: Optional[list[str]] = None
CSS selectors naming the parts of an HTML page to remove before conversion. Defaults to ['nav'].

Returns

MarkdownDocument
The converted content, with origin set to uri so the source is tracked through chunking and retrieval.

Examples

Read a local file or a web page. In both cases you get a MarkdownDocument whose .content holds the Markdown text:

from raghilda.read import read_as_markdown

# Read a local Markdown file
doc = read_as_markdown("path/to/file.md")
print(doc.content)

# Read and convert an HTML page
doc = read_as_markdown("https://example.com/page.html")
print(doc.content)