Documentation ¶
Overview ¶
Package html implements HTML renderer of parsed markdown document.
Configuring and customizing a renderer ¶
A renderer can be configured with multiple options:
import "github.com/gomarkdown/markdown/html" flags := html.CommonFlags | html.CompletePage | html.HrefTargetBlank opts := html.RendererOptions{ Title: "A custom title", Flags: flags, } renderer := html.NewRenderer(opts)
You can also re-use most of the logic and customize rendering of selected nodes by providing node render hook. This is most useful for rendering nodes that allow for design choices, like links or code blocks.
import ( "github.com/gomarkdown/markdown/html" "github.com/gomarkdown/markdown/ast" ) // a very dummy render hook that will output "code_replacements" instead of // <code>${content}</code> emitted by html.Renderer func renderHookCodeBlock(w io.Writer, node ast.Node, entering bool) (ast.WalkStatus, bool) { _, ok := node.(*ast.CodeBlock) if !ok { return ast.GoToNext, false } io.WriteString(w, "code_replacement") return ast.GoToNext, true } opts := html.RendererOptions{ RenderNodeHook: renderHookCodeBlock, } renderer := html.NewRenderer(opts)
Index ¶
- Variables
- func AddAbsPrefix(link []byte, prefix string) []byte
- func BlockAttrs(node ast.Node) []string
- func EscLink(w io.Writer, text []byte)
- func Escape(w io.Writer, text []byte)
- func EscapeHTML(w io.Writer, d []byte)
- func FootnoteItem(prefix string, slug []byte) string
- func FootnoteRef(prefix string, node *ast.Link) string
- func FootnoteReturnLink(prefix, returnLink string, slug []byte) string
- func HeadingCloseTagFromLevel(level int) string
- func HeadingOpenTagFromLevel(level int) string
- func IsList(node ast.Node) bool
- func IsListItem(node ast.Node) bool
- func IsListItemTerm(node ast.Node) bool
- func IsListTight(node ast.Node) bool
- func ListItemOpenCR(listItem *ast.ListItem) bool
- func SkipParagraphTags(para *ast.Paragraph) bool
- func Slugify(in []byte) []byte
- func TagWithAttributes(name string, attrs []string) string
- type Flags
- type RenderNodeFunc
- type Renderer
- func (r *Renderer) CR(w io.Writer)
- func (r *Renderer) Callout(w io.Writer, node *ast.Callout)
- func (r *Renderer) Caption(w io.Writer, caption *ast.Caption, entering bool)
- func (r *Renderer) CaptionFigure(w io.Writer, figure *ast.CaptionFigure, entering bool)
- func (r *Renderer) Citation(w io.Writer, node *ast.Citation)
- func (r *Renderer) Code(w io.Writer, node *ast.Code)
- func (r *Renderer) CodeBlock(w io.Writer, codeBlock *ast.CodeBlock)
- func (r *Renderer) DocumentMatter(w io.Writer, node *ast.DocumentMatter, entering bool)
- func (r *Renderer) EnsureUniqueHeadingID(id string) string
- func (r *Renderer) EscapeHTMLCallouts(w io.Writer, d []byte)
- func (r *Renderer) HTMLBlock(w io.Writer, node *ast.HTMLBlock)
- func (r *Renderer) HTMLSpan(w io.Writer, span *ast.HTMLSpan)
- func (r *Renderer) HardBreak(w io.Writer, node *ast.Hardbreak)
- func (r *Renderer) Heading(w io.Writer, hdr *ast.Heading, entering bool)
- func (r *Renderer) HeadingEnter(w io.Writer, hdr *ast.Heading)
- func (r *Renderer) HeadingExit(w io.Writer, hdr *ast.Heading)
- func (r *Renderer) HorizontalRule(w io.Writer, node *ast.HorizontalRule)
- func (r *Renderer) Image(w io.Writer, node *ast.Image, entering bool)
- func (r *Renderer) Index(w io.Writer, node *ast.Index)
- func (r *Renderer) Link(w io.Writer, link *ast.Link, entering bool)
- func (r *Renderer) List(w io.Writer, list *ast.List, entering bool)
- func (r *Renderer) ListItem(w io.Writer, listItem *ast.ListItem, entering bool)
- func (r *Renderer) MakeUniqueHeadingID(hdr *ast.Heading) string
- func (r *Renderer) NonBlockingSpace(w io.Writer, node *ast.NonBlockingSpace)
- func (r *Renderer) Out(w io.Writer, d []byte)
- func (r *Renderer) OutHRTag(w io.Writer, attrs []string)
- func (r *Renderer) OutOneOf(w io.Writer, outFirst bool, first string, second string)
- func (r *Renderer) OutOneOfCr(w io.Writer, outFirst bool, first string, second string)
- func (r *Renderer) OutTag(w io.Writer, name string, attrs []string)
- func (r *Renderer) Outs(w io.Writer, s string)
- func (r *Renderer) Paragraph(w io.Writer, para *ast.Paragraph, entering bool)
- func (r *Renderer) RenderFooter(w io.Writer, _ ast.Node)
- func (r *Renderer) RenderHeader(w io.Writer, ast ast.Node)
- func (r *Renderer) RenderNode(w io.Writer, node ast.Node, entering bool) ast.WalkStatus
- func (r *Renderer) TableBody(w io.Writer, node *ast.TableBody, entering bool)
- func (r *Renderer) TableCell(w io.Writer, tableCell *ast.TableCell, entering bool)
- func (r *Renderer) Text(w io.Writer, text *ast.Text)
- type RendererOptions
- type SPRenderer
Constants ¶
This section is empty.
Variables ¶
var Escaper = [256][]byte{ '&': []byte("&"), '<': []byte("<"), '>': []byte(">"), '"': []byte("""), }
Escaper defines how to escape HTML special characters
var IDTag = "id"
IDTag is the tag used for tag identification, it defaults to "id", some renderers may wish to override this and use e.g. "anchor".
Functions ¶
func AddAbsPrefix ¶
func BlockAttrs ¶
BlockAttrs takes a node and checks if it has block level attributes set. If so it will return a slice each containing a "key=value(s)" string.
func EscapeHTML ¶
EscapeHTML writes html-escaped d to w. It escapes &, <, > and " characters.
func FootnoteItem ¶
func FootnoteReturnLink ¶
func HeadingOpenTagFromLevel ¶
func IsListItem ¶
func IsListItemTerm ¶
func IsListTight ¶
func ListItemOpenCR ¶
func SkipParagraphTags ¶
func TagWithAttributes ¶
TagWithAttributes creates a HTML tag with a given name and attributes
Types ¶
type Flags ¶
type Flags int
Flags control optional behavior of HTML renderer.
const ( FlagsNone Flags = 0 SkipHTML Flags = 1 << iota // Skip preformatted HTML blocks SkipImages // Skip embedded images SkipLinks // Skip all links Safelink // Only link to trusted protocols NofollowLinks // Only link with rel="nofollow" NoreferrerLinks // Only link with rel="noreferrer" NoopenerLinks // Only link with rel="noopener" HrefTargetBlank // Add a blank target CompletePage // Generate a complete HTML page UseXHTML // Generate XHTML output instead of HTML FootnoteReturnLinks // Generate a link at the end of a footnote to return to the source FootnoteNoHRTag // Do not output an HR after starting a footnote list. Smartypants // Enable smart punctuation substitutions SmartypantsFractions // Enable smart fractions (with Smartypants) SmartypantsDashes // Enable smart dashes (with Smartypants) SmartypantsLatexDashes // Enable LaTeX-style dashes (with Smartypants) SmartypantsAngledQuotes // Enable angled double quotes (with Smartypants) for double quotes rendering SmartypantsQuotesNBSP // Enable « French guillemets » (with Smartypants) TOC // Generate a table of contents LazyLoadImages // Include loading="lazy" with images CommonFlags Flags = Smartypants | SmartypantsFractions | SmartypantsDashes | SmartypantsLatexDashes )
HTML renderer configuration options.
type RenderNodeFunc ¶
RenderNodeFunc allows reusing most of Renderer logic and replacing rendering of some nodes. If it returns false, Renderer.RenderNode will execute its logic. If it returns true, Renderer.RenderNode will skip rendering this node and will return WalkStatus
type Renderer ¶
type Renderer struct { Opts RendererOptions // if > 0, will strip html tags in Out and Outs DisableTags int // IsSafeURLOverride allows overriding the default URL matcher. URL is // safe if the overriding function returns true. Can be used to extend // the default list of safe URLs. IsSafeURLOverride func(url []byte) bool // contains filtered or unexported fields }
Renderer implements Renderer interface for HTML output.
Do not create this directly, instead use the NewRenderer function.
func NewRenderer ¶
func NewRenderer(opts RendererOptions) *Renderer
NewRenderer creates and configures an Renderer object, which satisfies the Renderer interface.
func (*Renderer) CaptionFigure ¶
CaptionFigure writes ast.CaptionFigure node
func (*Renderer) DocumentMatter ¶
DocumentMatter writes ast.DocumentMatter
func (*Renderer) EnsureUniqueHeadingID ¶
func (*Renderer) EscapeHTMLCallouts ¶
EscapeHTMLCallouts writes html-escaped d to w. It escapes &, <, > and " characters, *but* expands callouts <<N>> with the callout HTML, i.e. by calling r.callout() with a newly created ast.Callout node.
func (*Renderer) HorizontalRule ¶
func (r *Renderer) HorizontalRule(w io.Writer, node *ast.HorizontalRule)
HorizontalRule writes ast.HorizontalRule node
func (*Renderer) MakeUniqueHeadingID ¶
func (*Renderer) NonBlockingSpace ¶
func (r *Renderer) NonBlockingSpace(w io.Writer, node *ast.NonBlockingSpace)
NonBlockingSpace writes ast.NonBlockingSpace node
func (*Renderer) OutOneOfCr ¶
OutOneOfCr writes CR + first or second + CR depending on outFirst
func (*Renderer) RenderFooter ¶
RenderFooter writes HTML document footer.
func (*Renderer) RenderHeader ¶
RenderHeader writes HTML document preamble and TOC if requested.
func (*Renderer) RenderNode ¶
RenderNode renders a markdown node to HTML
type RendererOptions ¶
type RendererOptions struct { // Prepend this text to each relative URL. AbsolutePrefix string // Add this text to each footnote anchor, to ensure uniqueness. FootnoteAnchorPrefix string // Show this text inside the <a> tag for a footnote return link, if the // FootnoteReturnLinks flag is enabled. If blank, the string // <sup>[return]</sup> is used. FootnoteReturnLinkContents string // CitationFormatString defines how a citation is rendered. If blank, the string // <sup>[%s]</sup> is used. Where %s will be substituted with the citation target. CitationFormatString string // If set, add this text to the front of each Heading ID, to ensure uniqueness. HeadingIDPrefix string // If set, add this text to the back of each Heading ID, to ensure uniqueness. HeadingIDSuffix string // can over-write <p> for paragraph tag ParagraphTag string Title string // Document title (used if CompletePage is set) CSS string // Optional CSS file URL (used if CompletePage is set) Icon string // Optional icon file URL (used if CompletePage is set) Head []byte // Optional head data injected in the <head> section (used if CompletePage is set) Flags Flags // Flags allow customizing this renderer's behavior // if set, called at the start of RenderNode(). Allows replacing // rendering of some nodes RenderNodeHook RenderNodeFunc // Comments is a list of comments the renderer should detect when // parsing code blocks and detecting callouts. Comments [][]byte // Generator is a meta tag that is inserted in the generated HTML so show what rendered it. It should not include the closing tag. // Defaults (note content quote is not closed) to ` <meta name="GENERATOR" content="github.com/gomarkdown/markdown markdown processor for Go` Generator string }
RendererOptions is a collection of supplementary parameters tweaking the behavior of various parts of HTML renderer.
type SPRenderer ¶
type SPRenderer struct {
// contains filtered or unexported fields
}
SPRenderer is a struct containing state of a Smartypants renderer.
func NewSmartypantsRenderer ¶
func NewSmartypantsRenderer(flags Flags) *SPRenderer
NewSmartypantsRenderer constructs a Smartypants renderer object.