anydoc

package
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2026 License: Apache-2.0, MIT Imports: 5 Imported by: 0

Documentation

Overview

Package anydoc converts office documents to Markdown inside the Go process, without the Python docreader service.

The conversion itself is the anydoc Rust library, linked as a static archive through cgo (see third_party/anydoc-go). That archive needs a Rust toolchain to produce, so it is opt-in: only builds tagged `anydoc` link it. Every other build compiles the stub in backend_stub.go, where Available reports false and the engine registry hides the engine with a reason the UI can show.

The exported surface is deliberately narrow — bytes in, Markdown and embedded images out — so the backend can be replaced (a WASI runtime, a future upstream Go module) without touching the parser code that calls it.

Index

Constants

View Source
const ImageDir = "images/"

ImageDir is the markdown path prefix for extracted images. The image resolver matches references by this path and swaps them for storage URLs.

Variables

View Source
var ErrResourceLimit = errors.New("document is too large for the parser")

ErrResourceLimit marks a permanent anydoc rejection: the document exceeds a parser budget (for example max_xml_nodes) and retrying the same bytes cannot succeed. Callers detect it with errors.Is. The wrapped error's text names the limit the converter reported.

View Source
var ErrUnavailable = errors.New("anydoc: binding not built into this binary")

ErrUnavailable is returned by Convert when the binding is not linked into this build. Callers that can fall back to another engine should check for it with errors.Is.

Functions

func Available

func Available() bool

Available reports whether conversions can actually run in this binary.

func FormatForFile

func FormatForFile(fileType, fileName string) (format string, ok bool)

FormatForFile resolves the anydoc format name for a file type or file name. ok is false for anything anydoc does not convert.

func PDFInvalidStructure added in v0.9.0

func PDFInvalidStructure(err error) bool

PDFInvalidStructure reports a malformed document error that another PDF engine may still read. Encryption, resource limits and I/O errors are excluded.

func PDFNeedsOCR

func PDFNeedsOCR(err error) bool

PDFNeedsOCR reports whether a Convert error means the PDF has no usable text layer. Scanned exam papers fail this way; the caller should fall back to an engine that can rasterize pages for OCR.

func SupportedFileTypes

func SupportedFileTypes() []string

SupportedFileTypes returns the extensions anydoc can convert, sorted so the engine list is stable across restarts.

func Supports

func Supports(fileType, fileName string) bool

Supports reports whether anydoc converts this file type, regardless of whether the binding is linked into this build.

func UnavailableReason

func UnavailableReason() string

UnavailableReason explains, for the UI, why Available is false. It returns "" when the binding is available.

func Version

func Version() string

Version reports the anydoc release the linked binding was built from, or "" when no binding is linked.

Types

type Asset

type Asset struct {
	// ID is the document-model asset index, as referenced by in-place image
	// sources. It is stable for a single conversion.
	ID uint64
	// Name is a generated, extension-carrying file name ("image-1.png"):
	// embedded assets have no name of their own inside the container.
	Name string
	// MediaType is the IANA media type the container declared.
	MediaType string
	// Data is the raw image bytes.
	Data []byte
	// Alt is the image's alternative text as the document author wrote it,
	// empty when the document carries none.
	Alt string
	// Section is the text of the nearest heading above the image, empty when
	// the image sits before the first heading.
	Section string
}

Asset is one image embedded in a document.

type Options

type Options struct {
	// Format names the parser explicitly ("docx", "csv", ...). Empty means
	// the format is detected from the content, which every format except
	// CSV carries a signature for.
	Format string
	// WithAssets extracts embedded images and renders them in place in the
	// markdown using anydoc's official GFM serializer (asset images are
	// rewritten to `images/image-N.ext` links first). Ignored for PDF.
	WithAssets bool
}

Options tunes a single conversion.

type Result

type Result struct {
	// Markdown is GitHub-Flavored Markdown for the whole document.
	// When WithAssets is set, embedded images appear in place as
	// `![alt](images/image-N.ext)` via anydoc's official serializer.
	Markdown string
	// Assets are the images embedded in the document, in document order.
	// Always empty for PDF, which anydoc renders straight to Markdown
	// without a document model.
	Assets []Asset
	// AssetsError explains why Assets is empty when images were asked for.
	// The document-model parse is what both extracts images and places them
	// in the markdown; when it fails, Markdown still comes from the text-only
	// renderer so the conversion succeeds and the caller decides whether to
	// log or ignore the loss.
	AssetsError error
}

Result is one converted document.

func Convert

func Convert(data []byte, opts Options) (*Result, error)

Convert turns document bytes into Markdown. It returns ErrUnavailable when the binding is not linked into this build.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL