Involved Source Filesblock.go
Package blackfriday is a markdown processor.
It translates plain text with simple formatting rules into an AST, which can
then be further processed to HTML (provided by Blackfriday itself) or other
formats (provided by the community).
The simplest way to invoke Blackfriday is to call the Run function. It will
take a text input and produce a text output in HTML (or other format).
A slightly more sophisticated way to use Blackfriday is to create a Markdown
processor and to call Parse, which returns a syntax tree for the input
document. You can leverage Blackfriday's parsing for content extraction from
markdown documents. You can assign a custom renderer and set various options
to the Markdown processor.
If you're interested in calling Blackfriday from command line, see
https://github.com/russross/blackfriday-tool.
esc.gohtml.goinline.gomarkdown.gonode.gosmartypants.go
Package-Level Type Names (total 27, in which 21 are exported)
CodeBlockData contains fields relevant to a CodeBlock node type.
FenceCharbyteFenceLengthintFenceOffsetint
// This holds the info string
// Specifies whether it's a fenced code block or an indented one
HeadingData contains fields relevant to a Heading node type.
// This might hold heading ID, if present
// Specifies whether it's a title block
// This holds the heading level number
HTMLRenderer is a type that implements the Renderer interface for HTML output.
Do not create this directly, instead use the NewHTMLRenderer function.
HTMLRendererParametersHTMLRendererParameters
Prepend this text to each relative URL.
// Optional CSS file URL (used if CompletePage is set)
// Flags allow customizing this renderer's behavior
Add this text to each footnote anchor, to ensure uniqueness.
Show this text inside the <a> tag for a footnote return link, if the
HTML_FOOTNOTE_RETURN_LINKS flag is enabled. If blank, the string
<sup>[return]</sup> is used.
If set, add this text to the front of each Heading ID, to ensure
uniqueness.
If set, add this text to the back of each Heading ID, to ensure uniqueness.
Increase heading levels: if the offset is 1, <h1> becomes <h2> etc.
Negative offset is also valid.
Resulting levels are clipped between 1 and 6.
// Optional icon file URL (used if CompletePage is set)
// Document title (used if CompletePage is set)
// how to end singleton tags: either " />" or ">"
disableTagsint
Track heading IDs to prevent ID collision in a single generation.
lastOutputLenintsr*SPRenderer
RenderFooter writes HTML document footer.
RenderHeader writes HTML document preamble and TOC if requested.
RenderNode is a default renderer of a single node of a syntax tree. For
block nodes it will be called twice: first time with entering=true, second
time with entering=false, so that it could know when it's working on an open
tag and when on close. It writes the result to w.
The return value is a way to tell the calling walker to adjust its walk
pattern: e.g. it can terminate the traversal by returning Terminate. Or it
can ask the walker to skip a subtree of this node by returning SkipChildren.
The typical behavior is to return GoToNext, which asks for the usual
traversal to the next node.
(*T) addAbsPrefix(link []byte) []byte(*T) cr(w io.Writer)(*T) ensureUniqueHeadingID(id string) string(*T) out(w io.Writer, text []byte)(*T) outHRTag(w io.Writer)(*T) tag(w io.Writer, name []byte, attrs []string)(*T) writeDocumentHeader(w io.Writer)(*T) writeTOC(w io.Writer, ast *Node)
*T : Renderer
func NewHTMLRenderer(params HTMLRendererParameters) *HTMLRenderer
HTMLRendererParameters is a collection of supplementary parameters tweaking
the behavior of various parts of HTML renderer.
Prepend this text to each relative URL.
// Optional CSS file URL (used if CompletePage is set)
// Flags allow customizing this renderer's behavior
Add this text to each footnote anchor, to ensure uniqueness.
Show this text inside the <a> tag for a footnote return link, if the
HTML_FOOTNOTE_RETURN_LINKS flag is enabled. If blank, the string
<sup>[return]</sup> is used.
If set, add this text to the front of each Heading ID, to ensure
uniqueness.
If set, add this text to the back of each Heading ID, to ensure uniqueness.
Increase heading levels: if the offset is 1, <h1> becomes <h2> etc.
Negative offset is also valid.
Resulting levels are clipped between 1 and 6.
// Optional icon file URL (used if CompletePage is set)
// Document title (used if CompletePage is set)
func NewHTMLRenderer(params HTMLRendererParameters) *HTMLRenderer
LinkData contains fields relevant to a Link node type.
// Destination is what goes into a href
// If it's a footnote, this is a direct link to the footnote Node. Otherwise nil.
// NoteID contains a serial number of a footnote, zero if it's not a footnote
// Title is the tooltip thing that goes in a title attribute
ListData contains fields relevant to a List and Item node type.
// '*', '+' or '-' in bullet lists
// '.' or ')' after the number in ordered lists
// This is a list of footnotes
ListFlagsListType
// If not nil, turns this list item into a footnote item and triggers different rendering
// Skip <p>s around list item data if true
NodeVisitor is a callback to be called when traversing the syntax tree.
Called twice for every node: once with entering=true when the branch is
first visited, then with entering=false after all the children are done.
func (*Node).Walk(visitor NodeVisitor)
Reference represents the details of a link.
See the documentation in Options for more details on use-case.
Link is usually the URL the reference points to.
Text is the optional text to override the ref with if the syntax used was
[refid][]
Title is the alternate text describing the link in more detail.
ReferenceOverrideFunc is expected to be called with a reference string and
return either a valid Reference type that the reference string maps to or
nil. If overridden is false, the default reference logic will be executed.
See the documentation in Options for more details on use-case.
func WithRefOverride(o ReferenceOverrideFunc) Option
Renderer is the rendering interface. This is mostly of interest if you are
implementing a new rendering format.
Only an HTML implementation is provided in this repository, see the README
for external implementations.
RenderFooter is a symmetric counterpart of RenderHeader.
RenderHeader is a method that allows the renderer to produce some
content preceding the main body of the output document. The header is
understood in the broad sense here. For example, the default HTML
renderer will write not only the HTML document preamble, but also the
table of contents if it was requested.
The method will be passed an entire document tree, in case a particular
implementation needs to inspect it to produce output.
The output should be written to the supplied writer w. If your
implementation has no header to write, supply an empty implementation.
RenderNode is the main rendering method. It will be called once for
every leaf node and twice for every non-leaf node (first with
entering=true, then with entering=false). The method should write its
rendition of the node to the supplied writer w.
*HTMLRenderer
func WithRenderer(r Renderer) Option
TableCellData contains fields relevant to a TableCell node type.
// This holds the value for align attribute
// This tells if it's under the header row
reference holds all information necessary for a reference-style links or
footnotes.
Consider this markdown with reference-style links:
[link][ref]
[ref]: /url/ "tooltip title"
It will be ultimately converted to this HTML:
<p><a href=\"/url/\" title=\"title\">link</a></p>
And a reference structure will be populated as follows:
p.refs["ref"] = &reference{
link: "/url/",
title: "tooltip title",
}
Alternatively, reference can contain information about a footnote. Consider
this markdown:
Text needing a footnote.[^a]
[^a]: This is the note
A reference structure will be populated as follows:
p.refs["a"] = &reference{
link: "a",
title: "This is the note",
noteID: <some positive int>,
}
TODO: As you can see, it begs for splitting into two dedicated structures
for refs and for footnotes.
// a link to the Item node within a list of footnotes
hasBlockboollink[]byte
// 0 if not a footnote ref
// only gets populated by refOverride feature with Reference.Text
title[]byte(*T) String() string
*T : expvar.Var
*T : fmt.Stringer
*T : context.stringer
*T : runtime.stringer
func (*Markdown).getRef(refid string) (ref *reference, found bool)
Package-Level Functions (total 85, in which 9 are exported)
New constructs a Markdown processor. You can use the same With* functions as
for Run() to customize parser's behavior and the renderer.
NewHTMLRenderer creates and configures an HTMLRenderer object, which
satisfies the Renderer interface.
NewNode allocates a node of a specified type.
NewSmartypantsRenderer constructs a Smartypants renderer object.
Run is the main entry point to Blackfriday. It parses and renders a
block of markdown-encoded text.
The simplest invocation of Run takes one argument, input:
output := Run(input)
This will parse the input with CommonExtensions enabled and render it with
the default HTMLRenderer (with CommonHTMLFlags).
Variadic arguments opts can customize the default behavior. Since Markdown
type does not contain exported fields, you can not use it directly. Instead,
use the With* functions. For example, this will call the most basic
functionality, with no extensions:
output := Run(input, WithNoExtensions())
You can use any number of With* arguments, even contradicting ones. They
will be applied in order of appearance and the latter will override the
former:
output := Run(input, WithNoExtensions(), WithExtensions(exts),
WithRenderer(yourRenderer))
WithExtensions allows you to pick some of the many extensions provided by
Blackfriday. You can bitwise OR them.
WithNoExtensions turns off all extensions and custom behavior.
WithRefOverride sets an optional function callback that is called every
time a reference is resolved.
In Markdown, the link reference syntax can be made to resolve a link to
a reference instead of an inline URL, in one of the following ways:
* [link text][refid]
* [refid][]
Usually, the refid is defined at the bottom of the Markdown document. If
this override function is provided, the refid is passed to the override
function first, before consulting the defined refids at the bottom. If
the override function indicates an override did not occur, the refids at
the bottom will be used to fill in the link details.
WithRenderer allows you to override the default renderer.
hasPrefixCaseInsensitive is a custom implementation of
strings.HasPrefix(strings.ToLower(s), prefix)
we rolled our own because ToLower pulls in a huge machinery of lowercasing
anything from Unicode and that's very slow. Since this func will only be
used on ASCII protocol prefixes, we can take shortcuts.
isFenceLine checks if there's a fence line (e.g., ``` or ``` go) at the beginning of data,
and returns the end index if so, or 0 otherwise. It also returns the marker found.
If info is not nil, it gets set to the syntax specified in the fence line.
Test if a character is a horizontal whitespace character.
look for the address part of a mail autolink and '>'
this is less strict than the original markdown e-mail address matching
Test if a character is a punctuation symbol.
Taken from a private function in regexp in the stdlib.
Check whether or not data starts with a reference link.
If so, it is parsed and stored in the list of references
(in the render struct).
Returns the number of bytes to skip to move past it,
or zero if the first line is not a reference.
The first bit of this logic is the same as Parser.listItem, but the rest
is much simpler. This function simply finds the entire block and shifts it
over by one tab if it is indeed a block (just returns the line if it's not).
blockEnd is the end of the section in the input buffer, and contents is the
extracted text that was shifted over one tab. It will need to be rendered at
the end of the document.
https://www.w3.org/TR/html5/syntax.html#character-references
highest unicode code point in 17 planes (2^20): 1,114,112d =
7 dec digits or 6 hex digits
named entity references can be 2-31 characters with stuff like <
at one end and ∳ at the other. There
are also sometimes numbers at the end, although this isn't inherent
in the specification; there are never numbers anywhere else in
current character references, though; see ¾ and ▒, etc.
https://www.w3.org/TR/html5/syntax.html#named-character-references
entity := "&" (named group | number ref) ";"
named group := [a-zA-Z]{2,31}[0-9]{0,2}
number ref := "#" (dec ref | hex ref)
dec ref := [0-9]{1,7}
hex ref := ("x" | "X") [0-9a-fA-F]{1,6}
Package-Level Constants (total 104, in which 77 are exported)
These are the supported markdown parsing extensions.
OR these values together to select multiple extensions.
These are the supported markdown parsing extensions.
OR these values together to select multiple extensions.
These are the supported markdown parsing extensions.
OR these values together to select multiple extensions.
Constants for identifying different types of nodes. See NodeType.
Constants for identifying different types of nodes. See NodeType.
Constants for identifying different types of nodes. See NodeType.
These are the supported markdown parsing extensions.
OR these values together to select multiple extensions.
These are the supported markdown parsing extensions.
OR these values together to select multiple extensions.
HTML renderer configuration options.
These are the supported markdown parsing extensions.
OR these values together to select multiple extensions.
Constants for identifying different types of nodes. See NodeType.
Constants for identifying different types of nodes. See NodeType.
Constants for identifying different types of nodes. See NodeType.
These are the supported markdown parsing extensions.
OR these values together to select multiple extensions.
HTML renderer configuration options.
These are the supported markdown parsing extensions.
OR these values together to select multiple extensions.
GoToNext is the default traversal of every node.
Constants for identifying different types of nodes. See NodeType.
These are the supported markdown parsing extensions.
OR these values together to select multiple extensions.
Constants for identifying different types of nodes. See NodeType.
These are the supported markdown parsing extensions.
OR these values together to select multiple extensions.
Constants for identifying different types of nodes. See NodeType.
HTML renderer configuration options.
Constants for identifying different types of nodes. See NodeType.
HTML renderer configuration options.
Constants for identifying different types of nodes. See NodeType.
Constants for identifying different types of nodes. See NodeType.
Constants for identifying different types of nodes. See NodeType.
These are the supported markdown parsing extensions.
OR these values together to select multiple extensions.
Constants for identifying different types of nodes. See NodeType.
Constants for identifying different types of nodes. See NodeType.
These are the possible flag values for the ListItem renderer.
Multiple flag values may be ORed together.
These are mostly of interest if you are writing a new output format.
These are the possible flag values for the ListItem renderer.
Multiple flag values may be ORed together.
These are mostly of interest if you are writing a new output format.
These are the possible flag values for the ListItem renderer.
Multiple flag values may be ORed together.
These are mostly of interest if you are writing a new output format.
These are the possible flag values for the ListItem renderer.
Multiple flag values may be ORed together.
These are mostly of interest if you are writing a new output format.
These are the possible flag values for the ListItem renderer.
Multiple flag values may be ORed together.
These are mostly of interest if you are writing a new output format.
These are the possible flag values for the ListItem renderer.
Multiple flag values may be ORed together.
These are mostly of interest if you are writing a new output format.
These are the supported markdown parsing extensions.
OR these values together to select multiple extensions.
These are the supported markdown parsing extensions.
OR these values together to select multiple extensions.
HTML renderer configuration options.
These are the supported markdown parsing extensions.
OR these values together to select multiple extensions.
HTML renderer configuration options.
HTML renderer configuration options.
Constants for identifying different types of nodes. See NodeType.
HTML renderer configuration options.
SkipChildren tells walker to skip all children of current node.
HTML renderer configuration options.
HTML renderer configuration options.
HTML renderer configuration options.
HTML renderer configuration options.
HTML renderer configuration options.
HTML renderer configuration options.
HTML renderer configuration options.
HTML renderer configuration options.
HTML renderer configuration options.
Constants for identifying different types of nodes. See NodeType.
These are the supported markdown parsing extensions.
OR these values together to select multiple extensions.
These are the supported markdown parsing extensions.
OR these values together to select multiple extensions.
Constants for identifying different types of nodes. See NodeType.
Constants for identifying different types of nodes. See NodeType.
These are the possible flag values for the table cell renderer.
Only a single one of these values will be used; they are not ORed together.
These are mostly of interest if you are writing a new output format.
These are the possible flag values for the table cell renderer.
Only a single one of these values will be used; they are not ORed together.
These are mostly of interest if you are writing a new output format.
These are the possible flag values for the table cell renderer.
Only a single one of these values will be used; they are not ORed together.
These are mostly of interest if you are writing a new output format.
Constants for identifying different types of nodes. See NodeType.
Constants for identifying different types of nodes. See NodeType.
Constants for identifying different types of nodes. See NodeType.
Constants for identifying different types of nodes. See NodeType.
These are the supported markdown parsing extensions.
OR these values together to select multiple extensions.
The size of a tab stop.
The size of a tab stop.
These are the supported markdown parsing extensions.
OR these values together to select multiple extensions.
Terminate tells walker to terminate the traversal.
Constants for identifying different types of nodes. See NodeType.
These are the supported markdown parsing extensions.
OR these values together to select multiple extensions.
HTML renderer configuration options.
HTML renderer configuration options.
Version string of the package. Appears in the rendered document when
CompletePage flag is on.
The pages are generated with Goldsv0.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.