Skip to content

Commit

Permalink
Use hash headers only.
Browse files Browse the repository at this point in the history
Ported from planetscale@d4f14cc

cc @planetscale

Signed-off-by: Bartlomiej Plotka <bwplotka@gmail.com>
  • Loading branch information
bwplotka authored and Kunde21 committed Oct 28, 2020
1 parent b92ae22 commit cfdc058
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 69 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
markdownfmt
===========

Fork of github.com/shurcooL/markdownfmt that targets goldmark to render from parsed markdown AST.
Fork of github.com/shurcooL/markdownfmt that targets:

* Goldmark to render from parsed markdown AST.
* Hash-based headers only.



[![Build Status](https://travis-ci.org/Kunde21/markdownfmt.svg?branch=master)](https://travis-ci.org/Kunde21/markdownfmt) [![GoDoc](https://godoc.org/github.com/Kunde21/markdownfmt?status.svg)](https://godoc.org/github.com/Kunde21/markdownfmt)

Expand Down
20 changes: 2 additions & 18 deletions markdown/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,8 @@ func (_ *MarkdownFmtRenderer) stringWidth(s string) int {
func (mr *MarkdownFmtRenderer) header(node *ast.Heading, text []byte) {
mr.spaceBeforeParagraph(node)

if node.Level >= 3 {
mr.buf.Write(bytes.Repeat([]byte{'#'}, node.Level))
mr.buf.WriteByte(' ')
}
mr.buf.Write(bytes.Repeat([]byte{'#'}, node.Level))
mr.buf.WriteByte(' ')

newBuf := bytes.NewBuffer(nil)

Expand All @@ -172,21 +170,7 @@ func (mr *MarkdownFmtRenderer) header(node *ast.Heading, text []byte) {
fmt.Fprintf(newBuf, " {#%s}", id)
}

slen := mr.stringWidth(newBuf.String())

mr.buf.Write(newBuf.Bytes())

switch node.Level {
case 1:
mr.buf.WriteByte('\n')
mr.buf.Write(mr.leader())
mr.buf.Write(bytes.Repeat([]byte{'='}, slen))
case 2:
mr.buf.WriteByte('\n')
mr.buf.Write(mr.leader())
mr.buf.Write(bytes.Repeat([]byte{'-'}, slen))
}

mr.buf.WriteString("\n")
}

Expand Down
9 changes: 3 additions & 6 deletions markdown/testfiles/example1.output.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
Title
=====
# Title

This is a new paragraph. I wonder if I have too many spaces. What about new paragraph. But the next one...

Expand All @@ -8,11 +7,9 @@ Is really new.
1. Item one.
2. Item TWO.

Title
=====
# Title

Subtitle
--------
## Subtitle

How about `this` and other stuff like *italic*, *underline italic*, **bold**, **bold spaces**.

Expand Down
39 changes: 13 additions & 26 deletions markdown/testfiles/headers.same.md
Original file line number Diff line number Diff line change
@@ -1,52 +1,39 @@
Header
======
# Header

Hea *de* r
==========
# Hea *de* r

Hea `de` r
==========
# Hea `de` r

Header
------
## Header

text

Header
======
# Header

Header
------
## Header

### Header

text

Header
======
# Header

Header
======
# Header

Header {#custom}
================
# Header {#custom}

### Header {#custom}

> quote
>
> Header
> ======
> # Header
>
> Header
> ------
> ## Header
Paragraph

> Header
> ======
> # Header
>
> Header {#custom}
> ----------------
> ## Header {#custom}
>
> ### Header {#custom}
12 changes: 4 additions & 8 deletions markdown/testfiles/reference.same.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
An h1 header
============
# An h1 header

Paragraphs are separated by a blank line.

Expand All @@ -24,8 +23,7 @@ Trying items.
Last paragraph here.

An h2 header
------------
## An h2 header

- Paragraph right away.
- **Big item**: Right away after header.
Expand Down Expand Up @@ -101,8 +99,7 @@ The outer pipes (|) are optional, and you don't need to make the raw Markdown li
| *Still* | `renders` | **nicely** |
| 1 | 2 | 3 |

Nested Lists
============
# Nested Lists

### Codeblock within list

Expand Down Expand Up @@ -147,8 +144,7 @@ Para

- Item 3

Line Breaks
===========
# Line Breaks

Some text with two trailing spaces for linebreak.
More text immediately after.
Expand Down
6 changes: 2 additions & 4 deletions markdown/testfiles/things-inside-blocks.same.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,11 @@ List can have things inside
>
> > why not
6. Header
======
6. # Header

7. Text can have

Header
======
# Header

or

Expand Down
9 changes: 3 additions & 6 deletions markdown/testfiles/widechar.output.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
タイトル
========
# タイトル

サブタイトル
------------
## サブタイトル

aaa/あああ
----------
## aaa/あああ

s

0 comments on commit cfdc058

Please sign in to comment.