Skip to content

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
anotherhadi committed Sep 3, 2024
1 parent d08cf4e commit 59bdc02
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions get.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/lithammer/fuzzysearch/fuzzy"
)

// Returns the value of the specified front matter key or the default value if the key does not exist
func (md MarkdownFile) GetFrontMatter(key string, defaultValue interface{}) (value interface{}) {
if md.FrontMatter == nil {
return defaultValue
Expand Down
1 change: 1 addition & 0 deletions markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ type MarkdownFile struct {
Sections []Section
}

// New creates a new MarkdownFile object
func New(path string) MarkdownFile {
return MarkdownFile{Path: path}
}
1 change: 1 addition & 0 deletions read.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func (md *MarkdownFile) readFrontMatter(filename string) (err error) {
return
}

// Read reads the markdown file and parses it into sections (also reads the Front Matter)
func (md *MarkdownFile) Read() (err error) {
directory := filepath.Dir(md.Path)
filename := filepath.Base(md.Path)
Expand Down
6 changes: 5 additions & 1 deletion write.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
"gopkg.in/yaml.v3"
)

// AddSection adds a new section to the end of the markdown file
func (md *MarkdownFile) AddSection(line string) {
sectionType := getSectionType(line)
trimmedLine := strings.TrimSpace(line)
text := trimmedLine[strings.Index(trimmedLine, " ")+1:]
md.Sections = append(md.Sections, Section{SectionType: sectionType, Text: text, originalText: line})
}

// AddSectionAtIndex adds a new section at the specified index
func (md *MarkdownFile) AddSectionAtIndex(line string, index int) {
sectionType := getSectionType(line)
trimmedLine := strings.TrimSpace(line)
Expand All @@ -23,18 +25,20 @@ func (md *MarkdownFile) AddSectionAtIndex(line string, index int) {
md.Sections = append(md.Sections[:index], append([]Section{section}, md.Sections[index:]...)...)
}

// AddLine adds a new line to the end of the specified section
func (s *Section) AddLine(text string) {
lineType := getLineType(text)
s.Lines = append(s.Lines, Line{Text: text, LineType: lineType, originalText: text})
}

// AddLineAtIndex adds a new line at the specified index in the specified section
func (s *Section) AddLineAtIndex(text string, index int) {
lineType := getLineType(text)
line := Line{Text: text, LineType: lineType, originalText: text}
s.Lines = append(s.Lines[:index], append([]Line{line}, s.Lines[index:]...)...)
}

// If string is provided, write to that file. Otherwise, write to the original file
// If a string is provided, write to that file. Otherwise, write to the original file
func (md *MarkdownFile) Write(str ...string) (err error) {
newPath := ""
if len(str) > 0 {
Expand Down

0 comments on commit 59bdc02

Please sign in to comment.