A Go module for extracting content under specific headings from markdown documents.
- 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
go get github.com/subhash/mdextractpackage 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
}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)
}extractor:=mdextract.New(markdown)
headings:=extractor.GetAllHeadings()
for_, heading:=rangeheadings {
fmt.Println(heading)
}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.Creates a new Extractor from a markdown string.
Creates a new Extractor from a buffered scanner (useful for reading from files or streams).
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
Returns all headings found in the document.
Run the test suite:
go testRun with verbose output:
go test -vRun benchmarks:
go test -bench=.MIT