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
- Variables
- func Convert(src []byte, contentType, pageURL string) ([]byte, error)
- func DefaultWorkers() int
- func Estimate(n int64) int64
- func EstimateTokens(md []byte) int
- func ParseHeader(v string) (string, error)
- func RunChild(args []string, stdin io.Reader, stdout, stderr io.Writer) int
- type Config
- type Server
Constants ¶
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.
const ChildCommand = "mdconv"
ChildCommand is the hidden subcommand that converts one document.
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).
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).
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>".
const MaxInput = 6 << 20
MaxInput is the largest HTML converted, after any gzip decoding (6 MiB, Cloudflare's limit).
const TokensHeader = "X-Markdown-Tokens"
TokensHeader carries the converted document's estimated token count to clients, as Cloudflare's Markdown for Agents does.
Variables ¶
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 ¶
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 Estimate ¶
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 ¶
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 ¶
ParseHeader returns the origin layer named by "up=<upstream>".
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.