Source File
inline.go
Belonging Package
github.com/russross/blackfriday/v2
htmlEntityRe = regexp.MustCompile(`&([a-zA-Z]{2,31}[0-9]{0,2}|#([0-9]{1,7}|[xX][0-9a-fA-F]{1,6}));`)
)
if .nesting >= .maxNesting || len() == 0 {
return
}
.nesting++
, := 0, 0
for < len() {
:= .inlineCallback[[]]
if != nil {
++
.AppendChild(text([:]))
if != nil {
.AppendChild()
= +
=
}
} else {
++
}
}
if < len() {
if [-1] == '\n' {
--
}
.AppendChild(text([:]))
}
.nesting--
}
if == '~' || isspace([1]) {
return 0, nil
}
, := helperEmphasis(, [1:], )
if == 0 {
return 0, nil
}
return + 1,
}
if len() > 3 && [1] == && [2] != {
if isspace([2]) {
return 0, nil
}
, := helperDoubleEmphasis(, [2:], )
if == 0 {
return 0, nil
}
return + 2,
}
if len() > 4 && [1] == && [2] == && [3] != {
if == '~' || isspace([3]) {
return 0, nil
}
, := helperTripleEmphasis(, , 3, )
if == 0 {
return 0, nil
}
return + 3,
}
return 0, nil
}
func ( *Markdown, []byte, int) (int, *Node) {
= [:]
:= 0
for < len() && [] == '`' {
++
}
, := 0, 0
for = ; < len() && < ; ++ {
if [] == '`' {
++
} else {
= 0
}
}
:=
for < && [] == ' ' {
++
}
:= -
for > && [-1] == ' ' {
--
}
func ( *Markdown, []byte, int) (int, *Node) {
if .extensions&HardLineBreak != 0 {
return 1, NewNode(Hardbreak)
}
return 0, nil
}
type linkType int
const (
linkNormal linkType = iota
linkImg
linkDeferredFootnote
linkInlineFootnote
)
func ( []byte, int, linkType) bool {
if == linkDeferredFootnote {
return false
}
return < len()-1 && [] == '[' && [+1] != '^'
}
func ( *Markdown, []byte, int) (int, *Node) {
if < len()-1 && [+1] == '[' {
return link(, , )
}
return 0, nil
}
func ( *Markdown, []byte, int) (int, *Node) {
if < len()-1 && [+1] == '[' {
return link(, , )
}
return 0, nil
}
if .insideLink && ( > 0 && [-1] == '[' || len()-1 > && [+1] == '^') {
return 0, nil
}
var linkType
case .extensions&Footnotes != 0 && len()-1 > && [+1] == '^':
case >= 0 && [] == '!':
= linkImg
case .extensions&Footnotes != 0:
if >= 0 && [] == '^' {
= linkInlineFootnote
++
} else if len()-1 > && [+1] == '^' {
= linkDeferredFootnote
default:
= linkNormal
}
= [:]
var (
= 1
int
, , []byte
= false
)
if == linkDeferredFootnote {
++
}
switch {
= - 1
for > && isspace([]) {
--
}
if [] != '\'' && [] != '"' {
, = 0, 0
=
}
}
for > && isspace([-1]) {
--
}
if [] == '<' {
++
}
if [-1] == '>' {
--
}
if > {
= [:]
}
if > {
= [:]
}
++
case isReferenceStyleLink(, , ):
var []byte
:= false
default:
var []byte
= len(.notes) + 1
var []byte
if len() > 0 {
if len() < 16 {
= make([]byte, len())
} else {
= make([]byte, 16)
}
copy(, slugify())
} else {
= append([]byte("footnote-"), []byte(strconv.Itoa())...)
}
:= &reference{
noteID: ,
hasBlock: false,
link: ,
title: ,
footnote: ,
}
.notes = append(.notes, )
= .link
= .title
= + 1
}
var []byte
if == linkNormal || == linkImg {
if len() > 0 {
var bytes.Buffer
unescapeText(&, )
= .Bytes()
}
if len() == 0 || ( == linkNormal && <= 1) {
return 0, nil
}
}
var *Node
switch {
case linkNormal:
= NewNode(Link)
.Destination = normalizeURI()
.Title =
if len() > 0 {
.AppendChild(text())
:= .insideLink
.insideLink = true
.inline(, [1:])
.insideLink =
}
case linkImg:
= NewNode(Image)
.Destination =
.Title =
.AppendChild(text([1:]))
++
case linkInlineFootnote, linkDeferredFootnote:
= NewNode(Link)
.Destination =
.Title =
.NoteID =
.Footnote =
if == linkInlineFootnote {
++
}
default:
return 0, nil
}
return ,
}
func ( *Markdown) ( []byte) int {
if len() < 5 {
return 0
}
if [0] != '<' || [1] != '!' || [2] != '-' || [3] != '-' {
return 0
}
for < len() && !([-2] == '-' && [-1] == '-' && [] == '>') {
++
type autolinkType int
const (
notAutolink autolinkType = iota
normalAutolink
emailAutolink
)
func ( *Markdown, []byte, int) (int, *Node) {
= [:]
, := tagLength()
if := .inlineHTMLComment(); > 0 {
=
}
if > 2 {
if != notAutolink {
var bytes.Buffer
unescapeText(&, [1:+1-2])
if .Len() > 0 {
:= .Bytes()
:= NewNode(Link)
.Destination =
if == emailAutolink {
.Destination = append([]byte("mailto:"), ...)
}
.AppendChild(text(stripMailto()))
return ,
}
} else {
:= NewNode(HTMLSpan)
.Literal = [:]
return ,
}
}
return , nil
}
var escapeChars = []byte("\\`*_{}[]()#+-.!:|&<>~")
func ( *Markdown, []byte, int) (int, *Node) {
= [:]
if len() > 1 {
if .extensions&BackslashLineBreak != 0 && [1] == '\n' {
return 2, NewNode(Hardbreak)
}
if bytes.IndexByte(escapeChars, [1]) < 0 {
return 0, nil
}
return 2, text([1:2])
}
return 2, nil
}
func ( *bytes.Buffer, []byte) {
:= 0
for < len() {
:=
for < len() && [] != '\\' {
++
}
if > {
.Write([:])
}
if +1 >= len() {
break
}
.WriteByte([+1])
+= 2
}
}
func (, []byte) bool {
if len() < len() {
return false
}
:= byte('a' - 'A')
for , := range {
if != [] && != []+ {
return false
}
}
return true
}
var protocolPrefixes = [][]byte{
[]byte("http://"),
[]byte("https://"),
[]byte("ftp://"),
[]byte("file://"),
[]byte("mailto:"),
}
const shortestPrefix = 6 // len("ftp://"), the shortest of the above
if .insideLink || len() < +shortestPrefix {
return 0, nil
}
for , := range protocolPrefixes {
:= + 8 // 8 is the len() of the longest prefix
if > len() {
= len()
}
if hasPrefixCaseInsensitive([:], ) {
return autoLink(, , )
}
}
return 0, nil
}
:= 0
for - > 0 && <= 7 && isletter([--1]) {
++
}
if > 6 { // longest supported protocol is "mailto" which has 6 letters
return 0, nil
}
:=
= [-:]
if !isSafeLink() {
return 0, nil
}
:= 0
for < len() && !isEndOfLink([]) {
++
}
if ([-1] == '.' || [-1] == ',') && [-2] != '\\' {
--
}
if [-1] == ';' && [-2] != '\\' && !linkEndsWithEntity(, ) {
--
}
var byte
switch [-1] {
case '"':
= '"'
case '\'':
= '\''
case ')':
= '('
case ']':
= '['
case '}':
= '{'
default:
= 0
}
if != 0 {
:= - + - 2
:= 1
for >= 0 && [] != '\n' && != 0 {
if [] == [-1] {
++
}
if [] == {
--
}
--
}
if == 0 {
--
}
}
var bytes.Buffer
unescapeText(&, [:])
if .Len() > 0 {
:= NewNode(Link)
.Destination = .Bytes()
.AppendChild(text(.Bytes()))
return ,
}
return , nil
}
func ( byte) bool {
return isspace() || == '<'
}
var validUris = [][]byte{[]byte("http://"), []byte("https://"), []byte("ftp://"), []byte("mailto://")}
var validPaths = [][]byte{[]byte("/"), []byte("./"), []byte("../")}
func ( []byte) bool {
for , := range validPaths {
if len() >= len() && bytes.Equal([:len()], ) {
if len() == len() {
return true
} else if isalnum([len()]) {
return true
}
}
}
func ( []byte) ( autolinkType, int) {
var , int
if len() < 3 {
return notAutolink, 0
}
if [0] != '<' {
return notAutolink, 0
}
if [1] == '/' {
= 2
} else {
= 1
}
if !isalnum([]) {
return notAutolink, 0
}
for < len() && (isalnum([]) || [] == '.' || [] == '+' || [] == '-') {
++
}
if > 1 && < len() && [] == '@' {
if = isMailtoAutoLink([:]); != 0 {
return emailAutolink, +
}
}
if > 2 && < len() && [] == ':' {
= normalAutolink
++
}
switch {
case >= len():
= notAutolink
case != notAutolink:
=
for < len() {
if [] == '\\' {
+= 2
} else if [] == '>' || [] == '\'' || [] == '"' || isspace([]) {
break
} else {
++
}
}
if >= len() {
return , 0
}
if > && [] == '>' {
return , + 1
}
= notAutolink
}
+= bytes.IndexByte([:], '>')
if < 0 {
return , 0
}
return , + 1
}
if != 0 && [-1] == '\\' {
++
continue
}
if [] == {
return
}
:= 0
++
for < len() && [] != ']' {
if == 0 && [] == {
=
}
++
}
++
for < len() && ([] == ' ' || [] == '\n') {
++
}
if >= len() {
return
}
if [] != '[' && [] != '(' { // not a link
if > 0 {
return
}
continue
}
:= []
++
for < len() && [] != {
if == 0 && [] == {
return
}
++
}
if >= len() {
return
}
++
}
}
return 0
}
func ( *Markdown, []byte, byte) (int, *Node) {
:= 0
if len() > 1 && [0] == && [1] == {
= 1
}
for < len() {
:= helperFindEmphChar([:], )
if == 0 {
return 0, nil
}
+=
if >= len() {
return 0, nil
}
if +1 < len() && [+1] == {
++
continue
}
if [] == && !isspace([-1]) {
if .extensions&NoIntraEmphasis != 0 {
if !(+1 == len() || isspace([+1]) || ispunct([+1])) {
continue
}
}
:= NewNode(Emph)
.inline(, [:])
return + 1,
}
}
return 0, nil
}
func ( *Markdown, []byte, byte) (int, *Node) {
:= 0
for < len() {
:= helperFindEmphChar([:], )
if == 0 {
return 0, nil
}
+=
if +1 < len() && [] == && [+1] == && > 0 && !isspace([-1]) {
:= Strong
if == '~' {
= Del
}
:= NewNode()
.inline(, [:])
return + 2,
}
++
}
return 0, nil
}
func ( *Markdown, []byte, int, byte) (int, *Node) {
:= 0
:=
= [:]
for < len() {
:= helperFindEmphChar([:], )
if == 0 {
return 0, nil
}
+=
if [] != || isspace([-1]) {
continue
}
switch {
, := helperEmphasis(, [-2:], )
if == 0 {
return 0, nil
}
return - 2,
![]() |
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. |