markdown

package module
v0.0.0-...-3270ffc Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2023 License: MIT Imports: 10 Imported by: 0

README

markdown

This project mainly re-uses github.com/yuin/goldmark and focuses on extracting pieces of code from markdown files.

The goal is to select and run code snippets from a snippet collection maintained in markdown.

Documentation

Overview

Package markdown implements Code snippet/section extraction from markdown sources. As it uses github.com/yuin/goldmark it is compliant with CommonMark 0.30.

It can deal with all 3 different types of code sections defined in CommonMark:

Check the example to see what data is collected for each Code in CodeSections.

Index

Constants

This section is empty.

Variables

View Source
var Log = slog.Default()

Log is the logger used within markdown and can be overwritten by package users to achieve alternative logging behavior such as different outputs or a different level. Uses log/slog for structured logs.

Functions

This section is empty.

Types

type Code

type Code struct {
	// Description is the closest text to the code snippet which describes the snippet.
	Description string `json:"description" yaml:"description"`
	// [Kind] provides the type of markdown code section.
	Kind Kind `json:"kind" yaml:"kind"`
	// [Position] in the source file, includes the Description source.
	Position Position `json:"pos" yaml:"pos"`
	// Language contains the language defined for the code section.
	// If no language was defined it is "".
	Language string `json:"language,omitempty" yaml:"language,omitempty"`
	// Text contains the complete text of the code section.
	// This can be multiple lines and will always end with "\n".
	Text string `json:"code" yaml:"code"`
}

Code describes a Code section extracted from a markdown source.

func (Code) String

func (c Code) String() string

fmt.Stringer implementation.

type CodeSections

type CodeSections []*Code

CodeSections contain multiple Code sections.

func ExtractCodeSections

func ExtractCodeSections(source []byte) (CodeSections, error)

ExtractCodeSections parses and walks the provided markdown source to extract all Code sections within.

func (CodeSections) String

func (cs CodeSections) String() string

fmt.Stringer implementation.

type Kind

type Kind int

A Kind is used to identify the type of Code section. See package doc for references to the CommonMark spec. The methods are managed by github.com/alvaroloes/enumer.

const (
	BlockWithLang Kind = iota // code block with language
	Block                     // code block without language
	Inline                    // inline code
)

func KindString

func KindString(s string) (Kind, error)

KindString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.

func KindValues

func KindValues() []Kind

KindValues returns all values of the enum

func (Kind) IsAKind

func (i Kind) IsAKind() bool

IsAKind returns "true" if the value is listed in the enum definition. "false" otherwise

func (Kind) MarshalJSON

func (i Kind) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface for Kind

func (Kind) MarshalYAML

func (i Kind) MarshalYAML() (interface{}, error)

MarshalYAML implements a YAML Marshaler for Kind

func (Kind) String

func (i Kind) String() string

func (*Kind) UnmarshalJSON

func (i *Kind) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface for Kind

func (*Kind) UnmarshalYAML

func (i *Kind) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML implements a YAML Unmarshaler for Kind

type Position

type Position struct {
	Start int `json:"start_line" yaml:"start_line"`
	End   int `json:"end_line" yaml:"end_line"`
}

A Position contains the Start and End line of a Code section within the markdown source.

Directories

Path Synopsis
cmd
mdcode
mdcode is a reference implementation for a consumer of gitlab.com/tploss/markdown.
mdcode is a reference implementation for a consumer of gitlab.com/tploss/markdown.

Jump to

Keyboard shortcuts

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