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
- Variables
- func Available() bool
- func FormatForFile(fileType, fileName string) (format string, ok bool)
- func PDFNeedsOCR(err error) bool
- func SupportedFileTypes() []string
- func Supports(fileType, fileName string) bool
- func UnavailableReason() string
- func Version() string
- type Asset
- type Options
- type Result
Constants ¶
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 ¶
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 ¶
FormatForFile resolves the anydoc format name for a file type or file name. ok is false for anything anydoc does not convert.
func PDFNeedsOCR ¶
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 ¶
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.
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
// `` 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.