From 9dec7e9e8bb854ef86ee7fa5485b9fa1192136b1 Mon Sep 17 00:00:00 2001 From: yuin Date: Wed, 20 Nov 2019 01:10:18 +0900 Subject: [PATCH] Fixes #31 --- _test/options.txt | 9 +++++++++ parser/atx_heading.go | 10 ++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/_test/options.txt b/_test/options.txt index df4351a..e24e360 100644 --- a/_test/options.txt +++ b/_test/options.txt @@ -28,3 +28,12 @@

Title7

Title8

//= = = = = = = = = = = = = = = = = = = = = = = =// + +2 +//- - - - - - - - -// +# +# FOO +//- - - - - - - - -// +

+

FOO

+//= = = = = = = = = = = = = = = = = = = = = = = =// diff --git a/parser/atx_heading.go b/parser/atx_heading.go index dee28af..04278ab 100644 --- a/parser/atx_heading.go +++ b/parser/atx_heading.go @@ -186,15 +186,21 @@ func (b *atxHeadingParser) CanAcceptIndentedLine() bool { var attrAutoHeadingIDPrefix = []byte("heading") func generateAutoHeadingID(node *ast.Heading, reader text.Reader, pc Context) { + var line []byte lastIndex := node.Lines().Len() - 1 - lastLine := node.Lines().At(lastIndex) - line := lastLine.Value(reader.Source()) + if lastIndex > -1 { + lastLine := node.Lines().At(lastIndex) + line = lastLine.Value(reader.Source()) + } headingID := pc.IDs().Generate(line, attrAutoHeadingIDPrefix) node.SetAttribute(attrNameID, headingID) } func parseLastLineAttributes(node ast.Node, reader text.Reader, pc Context) { lastIndex := node.Lines().Len() - 1 + if lastIndex < 0 { // empty headings + return + } lastLine := node.Lines().At(lastIndex) line := lastLine.Value(reader.Source()) lr := text.NewReader(line)