github.com/brandur/modulir@v0.0.0-20240305213423-94ee82929cbd/modules/mmarkdown/mmarkdown.go (about)

     1  package mmarkdown
     2  
     3  import (
     4  	"os"
     5  
     6  	"golang.org/x/xerrors"
     7  	"gopkg.in/russross/blackfriday.v2"
     8  
     9  	"github.com/brandur/modulir"
    10  )
    11  
    12  //////////////////////////////////////////////////////////////////////////////
    13  //
    14  //
    15  //
    16  // Public
    17  //
    18  //
    19  //
    20  //////////////////////////////////////////////////////////////////////////////
    21  
    22  // Render is a shortcut for rendering some source data to Markdown via Black
    23  // Friday.
    24  func Render(c *modulir.Context, data []byte) []byte {
    25  	return blackfriday.Run(data)
    26  }
    27  
    28  // RenderFile is a shortcut for rendering a source file to Markdown in a target
    29  // file via Black Friday.
    30  func RenderFile(c *modulir.Context, source, target string) error {
    31  	inData, err := os.ReadFile(source)
    32  	if err != nil {
    33  		return xerrors.Errorf("error reading file: %w", err)
    34  	}
    35  
    36  	outData := Render(c, inData)
    37  
    38  	err = os.WriteFile(target, outData, 0o600)
    39  	if err != nil {
    40  		return xerrors.Errorf("error writing file: %w", err)
    41  	}
    42  
    43  	c.Log.Debugf("mmarkdown: Rendered '%s' to '%s'", source, target)
    44  	return nil
    45  }