markdown

package
v0.5.3 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2024 License: MIT Imports: 3 Imported by: 5

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExtractCodeBlocksWithComments

func ExtractCodeBlocksWithComments(input string, withQuotes bool) []string

ExtractCodeBlocksWithComments processes a markdown string to return only code blocks. Any non-code content preceding a code block is added as a comment within the respective code block. If there are non-code contents left at the end without a following code block, they are appended as comments to the last code block.

If `withQuotes` is set to true, the output will include the enclosing ``` for each code block. Otherwise, only the inner content of the code block (and the preceding comments) is returned.

func ExtractQuotedBlocks

func ExtractQuotedBlocks(input string, withQuotes bool) []string

ExtractQuotedBlocks extracts only the code blocks from a markdown string.

**Usage**: This function takes in a markdown string and a boolean `withQuotes`. If `withQuotes` is set to true, the output will include the enclosing ``` for each code block. Otherwise, only the inner content of the code block is returned.

**Inner Workings**: This function leverages the `ExtractAllBlocks` function to first get all blocks from the markdown content. It then filters out only the code blocks and processes them based on the `withQuotes` parameter to decide on the inclusion of the enclosing ``` marks.

Types

type BlockType

type BlockType string
const (
	Normal BlockType = "Normal"
	Code   BlockType = "Code"
)

type MarkdownBlock

type MarkdownBlock struct {
	Type     BlockType
	Language string
	Content  string
}

func ExtractAllBlocks

func ExtractAllBlocks(input string) []MarkdownBlock

ExtractAllBlocks processes a given markdown string to split it into a series of blocks.

**Usage**: Call this function with a markdown string as the argument. It will return a slice of MarkdownBlock structs, each representing a block of content (either normal or code).

**Inner Workings**: The function employs a state machine approach to parse through the markdown content. It recognizes and separates out code blocks (enclosed with ```) and normal text blocks. For code blocks, it also identifies the optional language specifier.

type State

type State int
const (
	OutsideBlock State = iota
	InsideBlock
)

Jump to

Keyboard shortcuts

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