Source File
goldmark.go
Belonging Package
golang.org/x/pkgsite/internal/frontend
package frontend
import (
)
func ( *astTransformer) ( *ast.Document, text.Reader, parser.Context) {
_ = ast.Walk(, func( ast.Node, bool) (ast.WalkStatus, error) {
if ! {
return ast.WalkContinue, nil
}
switch v := .(type) {
case *ast.Image:
if := translateLink(string(.Destination), .info, true, .readme); != "" {
.Destination = []byte()
}
case *ast.Link:
if := translateLink(string(.Destination), .info, false, .readme); != "" {
.Destination = []byte()
}
}
return ast.WalkContinue, nil
})
}
func ( *source.Info, *internal.Readme, ...html.Option) renderer.NodeRenderer {
:= &htmlRenderer{
info: ,
readme: ,
Config: html.NewConfig(),
firstHeading: true,
offset: 0,
}
for , := range {
.SetHTMLOption(&.Config)
}
return
}
func ( *htmlRenderer) ( renderer.NodeRendererFuncRegisterer) {
.Register(ast.KindHeading, .renderHeading)
.Register(ast.KindHTMLBlock, .renderHTMLBlock)
.Register(ast.KindRawHTML, .renderRawHTML)
}
func ( *htmlRenderer) ( util.BufWriter, []byte, ast.Node, bool) (ast.WalkStatus, error) {
:= .(*ast.Heading)
.offset = 3 - .Level
.firstHeading = false
}
:= .Level + .offset
if {
if .Level > 6 {
_, _ = .WriteString(fmt.Sprintf(`<div class="h%d" role="heading" aria-level="%d"`, , .Level))
} else {
_, _ = .WriteString(fmt.Sprintf(`<h%d class="h%d"`, , .Level))
}
if .Attributes() != nil {
html.RenderAttributes(, , html.HeadingAttributeFilter)
}
_ = .WriteByte('>')
} else {
if .Level > 6 {
_, _ = .WriteString("</div>\n")
} else {
_, _ = .WriteString(fmt.Sprintf("</h%d>\n", ))
}
}
return ast.WalkContinue, nil
}
func ( *htmlRenderer) ( util.BufWriter, []byte, ast.Node, bool) (ast.WalkStatus, error) {
:= .(*ast.HTMLBlock)
if {
if .Unsafe {
:= .Lines().Len()
for := 0; < ; ++ {
:= .Lines().At()
, := translateHTML(.Value(), .info, .readme)
if != nil {
return ast.WalkStop,
}
_, _ = .Write()
}
} else {
_, _ = .WriteString("<!-- raw HTML omitted -->\n")
}
} else {
if .HasClosure() {
if .Unsafe {
:= .ClosureLine
_, _ = .Write(.Value())
} else {
_, _ = .WriteString("<!-- raw HTML omitted -->\n")
}
}
}
return ast.WalkContinue, nil
}
func ( *htmlRenderer) ( util.BufWriter, []byte, ast.Node, bool) (ast.WalkStatus, error) {
if ! {
return ast.WalkSkipChildren, nil
}
if .Unsafe {
:= .(*ast.RawHTML)
for := 0; < .Segments.Len(); ++ {
:= .Segments.At()
, := translateHTML(.Value(), .info, .readme)
if != nil {
return ast.WalkStop,
}
_, _ = .Write()
}
return ast.WalkSkipChildren, nil
}
_, _ = .WriteString("<!-- raw HTML omitted -->")
return ast.WalkSkipChildren, nil
}
:= regexp.MustCompile(`(<[^<>]+>|\[\!\[[^\]]+]\([^\)]+\)\]\([^\)]+\))`)
:= .ReplaceAllString(string(), "")
:= func( rune) bool {
return !unicode.IsLetter() && !unicode.IsNumber()
}
= strings.Join(strings.FieldsFunc(, ), "-")
= strings.ToLower()
if len() == 0 {
if == ast.KindHeading {
= "heading"
} else {
= "id"
}
}
:=
for := 1; ; ++ {
if , := .values[]; ! {
.values[] = true
break
}
= fmt.Sprintf("%s-%d", , )
}
return []byte("readme-" + )
}
const linkHeadingText = "Links"
var linkHeadingBytes = []byte(linkHeadingText) // for faster comparison to node contents
if .inLinksHeading {
return ast.WalkStop, nil
}
if bytes.Equal(.Text(.Source()), linkHeadingBytes) {
.inLinksHeading = true
}
if !.inLinksHeading {
return ast.WalkSkipChildren, nil
if , := .FirstChild().(*ast.TextBlock); {
.links = append(.links, link{
Href: string(.Destination),
Body: string(.Text(.Source())),
})
}
}
return ast.WalkSkipChildren, nil
}
return ast.WalkContinue, nil
})
if != nil {
log.Errorf(.ctx, "extractLinks.Transform: %v", )
}
}
type extractTOC struct {
ctx context.Context
Headings []*Heading
removeTitle bool // omit title from TOC
}
if .Len() == 0 {
for := .FirstChild(); != nil; = .NextSibling() {
.Write(.Text(.Source()))
}
}
:= .(*ast.Heading)
:= &Heading{
Level: .Level,
Text: .String(),
}
if , := .AttributeString("id"); {
.ID = string(.([]byte))
}
= append(, )
return ast.WalkSkipChildren, nil
}
return ast.WalkContinue, nil
})
if != nil {
log.Errorf(.ctx, "extractTOC.Transform: %v", )
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |