src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/mods/md/md.go (about)

     1  // Package md exposes functionality from src.elv.sh/pkg/md.
     2  package md
     3  
     4  import (
     5  	"src.elv.sh/pkg/elvdoc"
     6  	"src.elv.sh/pkg/eval"
     7  	"src.elv.sh/pkg/md"
     8  	"src.elv.sh/pkg/sys"
     9  )
    10  
    11  // Ns is the namespace for the md: module.
    12  var Ns = eval.BuildNsNamed("md").
    13  	AddGoFns(map[string]any{
    14  		"show": show,
    15  	}).Ns()
    16  
    17  type showOpts struct {
    18  	Width int
    19  }
    20  
    21  func (*showOpts) SetDefaultOptions() {}
    22  
    23  func show(fm *eval.Frame, opts showOpts, markdown string) error {
    24  	width := opts.Width
    25  	if width <= 0 {
    26  		_, width = sys.WinSize(fm.Port(1).File)
    27  		if width <= 0 {
    28  			width = 80
    29  		}
    30  	}
    31  	codec := &md.TTYCodec{
    32  		Width:              width,
    33  		HighlightCodeBlock: elvdoc.HighlightCodeBlock,
    34  	}
    35  	_, err := fm.ByteOutput().WriteString(md.RenderString(markdown, codec))
    36  	return err
    37  }