mdconv

package
v1.9.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2026 License: AGPL-3.0 Imports: 32 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package mdconv converts HTML responses to Markdown for agents (Markdown for Agents, Site.markdown). The edge layer sends a request whose Accept names text/markdown to the agent's Markdown socket with X-Edgeweir-Markdown; Server passes it on to the origin layer it names and converts an HTML answer in a child process (edgeweir-node mdconv), or answers with what the origin layer sent when it cannot.

Index

Constants

View Source
const (
	DefaultMemory  int64 = 512 << 20
	DefaultTimeout       = 5 * time.Second
	// TransientTTL caps how long HTML answered for want of a conversion
	// slot stays cached under the Markdown variant's key.
	TransientTTL = 60
)

Defaults of the node's converter flags.

View Source
const ChildCommand = "mdconv"

ChildCommand is the hidden subcommand that converts one document.

View Source
const ContentSignal = "ai-train=yes, search=yes, ai-input=yes"

ContentSignal is the Content-Signal a converted response carries when the origin sent none (contentsignals.org; Cloudflare's default).

View Source
const ConvertedHeader = "X-Edgeweir-Markdown-Converted"

ConvertedHeader marks converted responses; it is cached with them and hidden from clients (the edge layer counts it, MinuteStats.markdown_responses).

View Source
const Header = "X-Edgeweir-Markdown"

Header is the request header the edge layer sets on Markdown requests (lua/edgeweir/markdown.lua): "up=<origin layer upstream>".

View Source
const MaxInput = 6 << 20

MaxInput is the largest HTML converted, after any gzip decoding (6 MiB, Cloudflare's limit).

View Source
const TokensHeader = "X-Markdown-Tokens"

TokensHeader carries the converted document's estimated token count to clients, as Cloudflare's Markdown for Agents does.

Variables

View Source
var ErrEmpty = errors.New("the document converts to nothing")

ErrEmpty is a document that converts to nothing (no metadata, no text, no JSON-LD): the HTML is kept.

Functions

func Convert

func Convert(src []byte, contentType, pageURL string) ([]byte, error)

Convert returns the Markdown of an HTML document: YAML frontmatter from its meta tags (title, description, image), the main content as CommonMark with tables and strikethrough, and its JSON-LD in a fenced json block (the layout of Cloudflare's Markdown for Agents). contentType is the response's Content-Type (its charset, else the document's own declaration, else UTF-8); pageURL is the request's absolute URL, against which (or the document's <base>) links and images become absolute.

func DefaultWorkers

func DefaultWorkers() int

DefaultWorkers is min(4, max(1, CPUs / 2)).

func Estimate

func Estimate(n int64) int64

Estimate returns the memory a conversion of n bytes of HTML is assumed to need: the parsed tree, its Markdown and the Go runtime (measured peaks of real pages plus margin).

func EstimateTokens

func EstimateTokens(md []byte) int

EstimateTokens returns a rough token count of Markdown (X-Markdown- Tokens): about four bytes per token for ASCII text and one token per character of other scripts (CJK and the like tokenize near one each).

func ParseHeader

func ParseHeader(v string) (string, error)

ParseHeader returns the origin layer named by "up=<upstream>".

func RunChild

func RunChild(args []string, stdin io.Reader, stdout, stderr io.Writer) int

RunChild is the child process: it limits itself (sandbox.Limit), reads the HTML from stdin and writes its Markdown to stdout. Exit status 3 means the document is not convertible (too large, or nothing to convert); nothing is written then.

Types

type Config

type Config struct {
	// Upstreams maps the origin layers' upstream names to their sockets.
	Upstreams map[string]string
	// Workers bounds concurrent conversions, Memory the sum of their
	// estimates; Timeout bounds one conversion (the child is killed).
	Workers int
	Memory  int64
	Timeout time.Duration
	// Command returns the child process for args (the agent's own binary
	// with ChildCommand) bound to ctx; tests run their own.
	Command func(ctx context.Context, args []string) *exec.Cmd
	Log     *slog.Logger
}

Config configures a Server.

type Server

type Server struct {
	// contains filtered or unexported fields
}

Server is the Markdown converter's HTTP handler.

func New

func New(cfg Config) *Server

New returns a Server.

func (*Server) Close

func (s *Server) Close()

Close releases idle connections.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

Jump to

Keyboard shortcuts

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