Skip to content

Latest commit

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

mdextract

A Go module for extracting content under specific headings from markdown documents.

Features

  • Extract content under any markdown heading (# through ######)
  • Support for both string and stream input
  • Case-insensitive heading matching
  • Preserves formatting, code blocks, lists, and other markdown elements
  • Stops extraction at next heading of same or higher level
  • List all headings in a document

Installation

go get github.com/subhash/mdextract

Usage

Basic Example

package main
import (
"fmt""log""github.com/subhash/mdextract"
)
funcmain() {
markdown:=`# My Document## IntroductionThis is the introduction section.It has multiple paragraphs.## Features- Feature 1- Feature 2- Feature 3## ConclusionFinal thoughts here.`extractor:=mdextract.New(markdown)
// Extract content under "## Features"content, err:=extractor.GetContent("## Features")
iferr!=nil {
log.Fatal(err)
}
fmt.Println(content)
// Output:// - Feature 1// - Feature 2// - Feature 3
}

Extract from Stream

package main
import (
"bufio""fmt""log""os""github.com/subhash/mdextract"
)
funcmain() {
file, err:=os.Open("document.md")
iferr!=nil {
log.Fatal(err)
}
deferfile.Close()
scanner:=bufio.NewScanner(file)
extractor:=mdextract.NewFromStream(scanner)
content, err:=extractor.GetContent("## Installation")
iferr!=nil {
log.Fatal(err)
}
fmt.Println(content)
}

Get All Headings

extractor:=mdextract.New(markdown)
headings:=extractor.GetAllHeadings()
for_, heading:=rangeheadings {
fmt.Println(heading)
}

Nested Headings

When extracting content under a heading, all lower-level headings are included until a heading of the same or higher level is encountered:

markdown:=`## Section 1Content before subsection.### Subsection 1.1Subsection content.### Subsection 1.2More subsection content.## Section 2Different section.`extractor:=mdextract.New(markdown)
content, _:=extractor.GetContent("## Section 1")
fmt.Println(content)
// Output:// Content before subsection.// // ### Subsection 1.1// // Subsection content.// // ### Subsection 1.2// // More subsection content.

API

New(markdown string) *Extractor

Creates a new Extractor from a markdown string.

NewFromStream(scanner *bufio.Scanner) *Extractor

Creates a new Extractor from a buffered scanner (useful for reading from files or streams).

GetContent(heading string) (string, error)

Extracts content under a specific heading until the next heading of the same or higher level.

  • heading: The heading to search for (e.g., "## Section Name")
  • Returns: The content without the heading itself, or an error if the heading is not found
  • Heading matching is case-insensitive
  • Content extraction stops at the next heading of equal or higher level

GetAllHeadings() []string

Returns all headings found in the document.

Testing

Run the test suite:

go test

Run with verbose output:

go test -v

Run benchmarks:

go test -bench=.

License

MIT

About

Extract parts of Markdown content

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages