Skip to content

Commit

Permalink
fix(parser): toc anchor for h1 links
Browse files Browse the repository at this point in the history
  • Loading branch information
xNaCly committed Apr 25, 2023
1 parent ad42943 commit a997014
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,21 +408,7 @@ func (p *Parser) code(quoteContext bool) Tag {
text: b.String(),
}
} else {
// inline code:
b := strings.Builder{}
for !p.check(scanner.BACKTICK) && !p.check(scanner.NEWLINE) {
if p.check(scanner.TEXT) {
b.WriteString(p.peek().Value)
} else {
b.WriteRune(scanner.TOKEN_SYMBOL_MAP[p.peek().Kind])
}
p.advance()
}
// skip the `
p.advance()
return CodeInline{
text: b.String(),
}
return Text{content: "`"}
}
}

Expand All @@ -435,7 +421,20 @@ func (p *Parser) paragraph() Tag {
case scanner.STRAIGHTBRACEOPEN:
children = append(children, p.link())
case scanner.BACKTICK:
children = append(children, p.code(false))
// inline code:
b := strings.Builder{}
for !p.check(scanner.BACKTICK) && !p.check(scanner.NEWLINE) {
if p.check(scanner.TEXT) {
b.WriteString(p.peek().Value)
} else {
b.WriteRune(scanner.TOKEN_SYMBOL_MAP[p.peek().Kind])
}
p.advance()
}
// skip the `
p.advance()

children = append(children, CodeInline{text: b.String()})
case scanner.STAR, scanner.UNDERSCORE:
children = append(children, p.emphasis())
case scanner.TEXT:
Expand Down Expand Up @@ -525,7 +524,7 @@ func (p *Parser) GenerateToc() string {
headingMap[4] = 0
headingMap[5] = 0
headingMap[6] = 0
b.WriteString(fmt.Sprintf("<li><a class=\"toc-h1\" ref=\"#%s\">%d</a>: %s</li>", strings.TrimSpace(v.text), headingMap[1], v.text))
b.WriteString(fmt.Sprintf("<li><a class=\"toc-h1\" href=\"#%s\">%d</a>: %s</li>", strings.TrimSpace(v.text), headingMap[1], v.text))
case 2:
headingMap[3] = 0
headingMap[4] = 0
Expand Down

0 comments on commit a997014

Please sign in to comment.