Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

option to select between atx and settext header style #57

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ Usage
usage: markdownfmt [flags] [path ...]
-d=false: display diffs instead of rewriting files
-l=false: list files whose formatting differs from markdownfmt's
-w=false: write result to (source) file instead of stdout
-s=false: use atx header style, if true use settext
-w=false: write result to (source) file instead of
```

Editor Plugins
Expand Down
9 changes: 8 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
module github.com/shurcooL/markdownfmt

require github.com/russross/blackfriday v1.5.3-0.20190124082335-a477dd164691
go 1.14

require (
github.com/mattn/go-runewidth v0.0.9
github.com/russross/blackfriday v1.5.3-0.20190124082335-a477dd164691
github.com/shurcooL/go v0.0.0-20191216061654-b114cc39af9f
golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59
)
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/russross/blackfriday v1.5.3-0.20190124082335-a477dd164691 h1:auJkuUc4uOuZNoH9jGLvqVaDLiuCOh/LY+Qw5NBFo4I=
github.com/russross/blackfriday v1.5.3-0.20190124082335-a477dd164691/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/shurcooL/go v0.0.0-20191216061654-b114cc39af9f h1:gvOuVMIvQ0Ca/afBH/YBfp5f2RWb92DbAI5EjwoFLuc=
github.com/shurcooL/go v0.0.0-20191216061654-b114cc39af9f/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59 h1:3zb4D3T4G8jdExgVU/95+vQXfpEPiMdCaZgmGVxjNHM=
golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
10 changes: 6 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import (

var (
// Main operation modes.
list = flag.Bool("l", false, "list files whose formatting differs from markdownfmt's")
write = flag.Bool("w", false, "write result to (source) file instead of stdout")
doDiff = flag.Bool("d", false, "display diffs instead of rewriting files")
list = flag.Bool("l", false, "list files whose formatting differs from markdownfmt's")
write = flag.Bool("w", false, "write result to (source) file instead of stdout")
doDiff = flag.Bool("d", false, "display diffs instead of rewriting files")
useSetTextHeaders = flag.Bool("s", false, "use settext style headers instead of atx ones")

exitCode = 0
)
Expand Down Expand Up @@ -61,7 +62,8 @@ func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error
return terminal.IsTerminal(int(os.Stdout.Fd())) && os.Getenv("TERM") != "dumb"
}
res, err := markdown.Process(filename, src, &markdown.Options{
Terminal: !*list && !*write && !*doDiff && isTerminal(),
Terminal: !*list && !*write && !*doDiff && isTerminal(),
UseSetTextHeaders: *useSetTextHeaders,
})
if err != nil {
return err
Expand Down
45 changes: 27 additions & 18 deletions markdown/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"io/ioutil"
"strings"

"github.com/mattn/go-runewidth"
runewidth "github.com/mattn/go-runewidth"
"github.com/russross/blackfriday"
"github.com/shurcooL/go/indentwriter"
)
Expand Down Expand Up @@ -102,24 +102,32 @@ func (*markdownRenderer) TitleBlock(out *bytes.Buffer, text []byte) {
func (mr *markdownRenderer) Header(out *bytes.Buffer, text func() bool, level int, id string) {
marker := out.Len()
doubleSpace(out)
if mr.opt.UseSetTextHeaders == true {
if level >= 3 {
fmt.Fprint(out, strings.Repeat("#", level), " ")
}

if level >= 3 {
fmt.Fprint(out, strings.Repeat("#", level), " ")
}

textMarker := out.Len()
if !text() {
out.Truncate(marker)
return
}
textMarker := out.Len()
if !text() {
out.Truncate(marker)
return
}

switch level {
case 1:
len := mr.stringWidth(out.String()[textMarker:])
fmt.Fprint(out, "\n", strings.Repeat("=", len))
case 2:
len := mr.stringWidth(out.String()[textMarker:])
fmt.Fprint(out, "\n", strings.Repeat("-", len))
switch level {
case 1:
len := mr.stringWidth(out.String()[textMarker:])
fmt.Fprint(out, "\n", strings.Repeat("=", len))
case 2:
len := mr.stringWidth(out.String()[textMarker:])
fmt.Fprint(out, "\n", strings.Repeat("-", len))
}
} else {
fmt.Fprint(out, strings.Repeat("#", level), " ")
// textMarker := out.Len()
if !text() {
out.Truncate(marker)
return
}
}
out.WriteString("\n")
}
Expand Down Expand Up @@ -470,7 +478,8 @@ func NewRenderer(opt *Options) blackfriday.Renderer {
// Options specifies options for formatting.
type Options struct {
// Terminal specifies if ANSI escape codes are emitted for styling.
Terminal bool
Terminal bool
UseSetTextHeaders bool
}

// Process formats Markdown.
Expand Down