markdown

package module
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2024 License: MIT Imports: 11 Imported by: 3

README

Markdown reader/writer

Latest Release GoDoc GoReportCard

This is a simple markdown reader/writer in golang that can reads markdown files and writes to markdown files.

Features

  • Read markdown files
  • Write markdown files
  • Parse front matter of markdown files (parse yaml front matter with gopkg.in/yaml.v3)

How to use

To lazy to write a documentation, you can found examples in the markdown_test.go file.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Line

type Line struct {
	Text     string // The original text, with prefix
	LineType LineType
	// contains filtered or unexported fields
}

type LineType

type LineType string
var (
	Normal       LineType = ""
	Code         LineType = "```"
	Image        LineType = "!["
	List         LineType = "- "
	Quote        LineType = "> "
	Table        LineType = "| "
	Task         LineType = "- [%] "
	NumberedList LineType = "%. "
)

type MarkdownFile

type MarkdownFile struct {
	Path        string
	Title       string // First "H1" section
	FrontMatter map[interface{}]interface{}
	Sections    []Section
}

func New

func New(path string) MarkdownFile

New creates a new MarkdownFile object

func (*MarkdownFile) AddSection

func (md *MarkdownFile) AddSection(line string)

AddSection adds a new section to the end of the markdown file

func (*MarkdownFile) AddSectionAtIndex

func (md *MarkdownFile) AddSectionAtIndex(line string, index int)

AddSectionAtIndex adds a new section at the specified index

func (MarkdownFile) GetFrontMatter

func (md MarkdownFile) GetFrontMatter(key string, defaultValue interface{}) (value interface{})

Returns the value of the specified front matter key or the default value if the key does not exist

func (*MarkdownFile) GetSection

func (md *MarkdownFile) GetSection(sectionType SectionType, text string) (section *Section)

GetSection returns the first section of the specified type with the specified text

func (*MarkdownFile) Read

func (md *MarkdownFile) Read() (err error)

Read reads the markdown file and parses it into sections (also reads the Front Matter)

func (*MarkdownFile) SearchSection

func (md *MarkdownFile) SearchSection(searchString string) (sections []*Section)

SearchSection returns a list of sections that contain the search string (with fuzzy search)

func (*MarkdownFile) SearchSectionWithType

func (md *MarkdownFile) SearchSectionWithType(searchString string, sectionType SectionType) (sections []*Section)

SearchSection returns a list of sections with specified section type that contain the search string (with fuzzy search)

func (*MarkdownFile) Write

func (md *MarkdownFile) Write(str ...string) (err error)

If a string is provided, write to that file. Otherwise, write to the original file

type Section

type Section struct {
	SectionType SectionType
	Text        string // The text after the first space (Without the "#..")
	Lines       []Line
	// contains filtered or unexported fields
}

A "section" is a part of the markdown file that starts with any of the H1-H6 headers A NullSection is a section that has no "section" (It's the first part of the file if no title at the beginning)

func (*Section) AddLine

func (s *Section) AddLine(text string)

AddLine adds a new line to the end of the specified section

func (*Section) AddLineAtIndex

func (s *Section) AddLineAtIndex(text string, index int)

AddLineAtIndex adds a new line at the specified index in the specified section

type SectionType

type SectionType string

Sections

var (
	H1          SectionType = "#"
	H2          SectionType = "##"
	H3          SectionType = "###"
	H4          SectionType = "####"
	H5          SectionType = "#####"
	H6          SectionType = "######"
	NullSection SectionType = ""
)

Jump to

Keyboard shortcuts

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