Copyright 2020 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

package frontend

import (
	
	

	
	
	
	
	
	emoji 
	
	
	
	goldmarkHtml 
	gmtext 
	
	
	
	
)
Heading holds data about a heading and nested headings within a readme. This data is used in the sidebar template to render the readme outline.
Level is the original level of the heading.
Text is the content from the readme contained within a heading.
ID corresponds to the ID attribute for a heading element and is also used in an href to the corresponding section within the readme outline. All ids are prefixed with readme- to avoid name collisions.
Children are nested headings.
parent is the heading this heading is nested within. Nil for top level headings.
Readme holds the result of processing a REAME file.
type Readme struct {
	HTML    safehtml.HTML // rendered HTML
	Outline []*Heading    // document headings
	Links   []link        // links from the "Links" section
}
ProcessReadme processes the README of unit u, if it has one. Processing includes rendering and sanitizing the HTML or Markdown, and extracting headings and links. Headings are prefixed with "readme-" and heading levels are adjusted to start at h3 in order to nest them properly within the rest of the page. The readme's original styling is preserved in the html by giving headings a css class styled identical to their original heading level. The extracted links are for display outside of the readme contents. This function is exported for use by external tools.
func ( context.Context,  *internal.Unit) ( *Readme,  error) {
	defer derrors.WrapAndReport(&, "ProcessReadme(%q, %q, %q)", .Path, .ModulePath, .Version)
	return processReadme(, .Readme, .SourceInfo)
}

func ( context.Context,  *internal.Readme,  *source.Info) ( *Readme,  error) {
	if  == nil || .Contents == "" {
		return &Readme{}, nil
	}
	if !isMarkdown(.Filepath) {
		 := template.Must(template.New("").Parse(`<pre class="readme">{{.}}</pre>`))
		,  := .ExecuteToHTML(.Contents)
		if  != nil {
			return nil, 
		}
		return &Readme{HTML: }, nil
	}
Sets priority value so that we always use our custom transformer instead of the default ones. The default values are in: https://github.com/yuin/goldmark/blob/7b90f04af43131db79ec320be0bd4744079b346f/parser/parser.go#L567
	const  = 10000
	 := &extractLinks{ctx: }
	 := &extractTOC{ctx: , removeTitle: true}
	 := goldmark.New(
WithHeadingAttribute allows us to include other attributes in heading tags. This is useful for our aria-level implementation of increasing heading rankings.
Generates an id in every heading tag. This is used in github in order to generate a link with a hash that a user would scroll to <h1 id="goldmark">goldmark</h1> => github.com/yuin/goldmark#goldmark
Include custom ASTTransformer using the readme and module info to use translateRelativeLink and translateHTML to modify the AST before it is rendered.
Extract links after we have transformed the URLs.
				util.Prioritized(, +1),
				util.Prioritized(, +1),
			),
These extensions lets users write HTML code in the README. This is fine since we process the contents using bluemonday after.
		goldmark.WithRendererOptions(goldmarkHtml.WithUnsafe(), goldmarkHtml.WithXHTML()),
		goldmark.WithExtensions(
			extension.GFM, // Support Github Flavored Markdown.
			emoji.Emoji,   // Support Github markdown emoji markup.
		),
	)
	.Renderer().AddOptions(
		renderer.WithNodeRenderers(
			util.Prioritized(newHTMLRenderer(, ), 100),
		),
	)
	 := []byte(.Contents)
	 := .Parser()
	 := gmtext.NewReader()
	 := parser.NewContext(parser.WithIDs(newIDs()))
	 := .Parse(, parser.WithContext())
	 := .Renderer()

	var  bytes.Buffer
	if  := .Render(&, , );  != nil {
		return &Readme{}, nil
	}
	return &Readme{
		HTML:    sanitizeHTML(&),
		Outline: .Headings,
		Links:   .links,
	}, nil
}
sanitizeHTML sanitizes HTML from a bytes.Buffer so that it is safe.
func ( *bytes.Buffer) safehtml.HTML {
	 := bluemonday.UGCPolicy()

	.AllowAttrs("width", "align").OnElements("img")
	.AllowAttrs("width", "align").OnElements("div")
Allow accessible headings (i.e <div role="heading" aria-level="7">).
	.AllowAttrs("width", "align", "role", "aria-level").OnElements("div")
Needed to preserve github styles heading font-sizes