github.com/google/go-github/v65@v65.0.0/github/markdown.go (about)

     1  // Copyright 2023 The go-github AUTHORS. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package github
     7  
     8  import (
     9  	"bytes"
    10  	"context"
    11  )
    12  
    13  // MarkdownService provides access to markdown-related functions in the GitHub API.
    14  type MarkdownService service
    15  
    16  // MarkdownOptions specifies optional parameters to the Render method.
    17  type MarkdownOptions struct {
    18  	// Mode identifies the rendering mode. Possible values are:
    19  	//   markdown - render a document as plain Render, just like
    20  	//   README files are rendered.
    21  	//
    22  	//   gfm - to render a document as user-content, e.g. like user
    23  	//   comments or issues are rendered. In GFM mode, hard line breaks are
    24  	//   always taken into account, and issue and user mentions are linked
    25  	//   accordingly.
    26  	//
    27  	// Default is "markdown".
    28  	Mode string
    29  
    30  	// Context identifies the repository context. Only taken into account
    31  	// when rendering as "gfm".
    32  	Context string
    33  }
    34  
    35  type markdownRenderRequest struct {
    36  	Text    *string `json:"text,omitempty"`
    37  	Mode    *string `json:"mode,omitempty"`
    38  	Context *string `json:"context,omitempty"`
    39  }
    40  
    41  // Render renders an arbitrary Render document.
    42  //
    43  // GitHub API docs: https://docs.github.com/rest/markdown/markdown#render-a-markdown-document
    44  //
    45  //meta:operation POST /markdown
    46  func (s *MarkdownService) Render(ctx context.Context, text string, opts *MarkdownOptions) (string, *Response, error) {
    47  	request := &markdownRenderRequest{Text: String(text)}
    48  	if opts != nil {
    49  		if opts.Mode != "" {
    50  			request.Mode = String(opts.Mode)
    51  		}
    52  		if opts.Context != "" {
    53  			request.Context = String(opts.Context)
    54  		}
    55  	}
    56  
    57  	req, err := s.client.NewRequest("POST", "markdown", request)
    58  	if err != nil {
    59  		return "", nil, err
    60  	}
    61  
    62  	buf := new(bytes.Buffer)
    63  	resp, err := s.client.Do(ctx, req, buf)
    64  	if err != nil {
    65  		return "", resp, err
    66  	}
    67  
    68  	return buf.String(), resp, nil
    69  }