mirror of
https://github.com/status-im/markdown.git
synced 2025-02-23 16:48:14 +00:00
Add ability to convert relative links to absolute
This commit is contained in:
parent
03a690ac55
commit
5c12499aa1
@ -21,7 +21,7 @@ func runMarkdownBlock(input string, extensions int) string {
|
|||||||
htmlFlags := 0
|
htmlFlags := 0
|
||||||
htmlFlags |= HTML_USE_XHTML
|
htmlFlags |= HTML_USE_XHTML
|
||||||
|
|
||||||
renderer := HtmlRenderer(htmlFlags, "", "")
|
renderer := HtmlRenderer(htmlFlags, "", "", "")
|
||||||
|
|
||||||
return string(Markdown([]byte(input), renderer, extensions))
|
return string(Markdown([]byte(input), renderer, extensions))
|
||||||
}
|
}
|
||||||
|
19
html.go
19
html.go
@ -41,6 +41,7 @@ const (
|
|||||||
HTML_USE_SMARTYPANTS // enable smart punctuation substitutions
|
HTML_USE_SMARTYPANTS // enable smart punctuation substitutions
|
||||||
HTML_SMARTYPANTS_FRACTIONS // enable smart fractions (with HTML_USE_SMARTYPANTS)
|
HTML_SMARTYPANTS_FRACTIONS // enable smart fractions (with HTML_USE_SMARTYPANTS)
|
||||||
HTML_SMARTYPANTS_LATEX_DASHES // enable LaTeX-style dashes (with HTML_USE_SMARTYPANTS)
|
HTML_SMARTYPANTS_LATEX_DASHES // enable LaTeX-style dashes (with HTML_USE_SMARTYPANTS)
|
||||||
|
HTML_ABSOLUTE_LINKS // convert all links to absolute links
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -62,6 +63,7 @@ type Html struct {
|
|||||||
closeTag string // how to end singleton tags: either " />\n" or ">\n"
|
closeTag string // how to end singleton tags: either " />\n" or ">\n"
|
||||||
title string // document title
|
title string // document title
|
||||||
css string // optional css file url (used with HTML_COMPLETE_PAGE)
|
css string // optional css file url (used with HTML_COMPLETE_PAGE)
|
||||||
|
absolutePrefix string
|
||||||
|
|
||||||
// table of contents data
|
// table of contents data
|
||||||
tocMarker int
|
tocMarker int
|
||||||
@ -84,7 +86,7 @@ const (
|
|||||||
// title is the title of the document, and css is a URL for the document's
|
// title is the title of the document, and css is a URL for the document's
|
||||||
// stylesheet.
|
// stylesheet.
|
||||||
// title and css are only used when HTML_COMPLETE_PAGE is selected.
|
// title and css are only used when HTML_COMPLETE_PAGE is selected.
|
||||||
func HtmlRenderer(flags int, title string, css string) Renderer {
|
func HtmlRenderer(flags int, title string, css string, absolutePrefix string) Renderer {
|
||||||
// configure the rendering engine
|
// configure the rendering engine
|
||||||
closeTag := htmlClose
|
closeTag := htmlClose
|
||||||
if flags&HTML_USE_XHTML != 0 {
|
if flags&HTML_USE_XHTML != 0 {
|
||||||
@ -96,6 +98,7 @@ func HtmlRenderer(flags int, title string, css string) Renderer {
|
|||||||
closeTag: closeTag,
|
closeTag: closeTag,
|
||||||
title: title,
|
title: title,
|
||||||
css: css,
|
css: css,
|
||||||
|
absolutePrefix: absolutePrefix,
|
||||||
|
|
||||||
headerCount: 0,
|
headerCount: 0,
|
||||||
currentLevel: 0,
|
currentLevel: 0,
|
||||||
@ -410,7 +413,10 @@ func (options *Html) AutoLink(out *bytes.Buffer, link []byte, kind int) {
|
|||||||
out.WriteString("<a href=\"")
|
out.WriteString("<a href=\"")
|
||||||
if kind == LINK_TYPE_EMAIL {
|
if kind == LINK_TYPE_EMAIL {
|
||||||
out.WriteString("mailto:")
|
out.WriteString("mailto:")
|
||||||
|
} else {
|
||||||
|
options.maybeWriteAbsolutePrefix(out, link)
|
||||||
}
|
}
|
||||||
|
|
||||||
entityEscapeWithSkip(out, link, skipRanges)
|
entityEscapeWithSkip(out, link, skipRanges)
|
||||||
|
|
||||||
if options.flags&HTML_NOFOLLOW_LINKS != 0 && !isRelativeLink(link) {
|
if options.flags&HTML_NOFOLLOW_LINKS != 0 && !isRelativeLink(link) {
|
||||||
@ -459,12 +465,22 @@ func (options *Html) Emphasis(out *bytes.Buffer, text []byte) {
|
|||||||
out.WriteString("</em>")
|
out.WriteString("</em>")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (options *Html) maybeWriteAbsolutePrefix(out *bytes.Buffer, link []byte) {
|
||||||
|
if options.flags&HTML_ABSOLUTE_LINKS != 0 && isRelativeLink(link) {
|
||||||
|
out.WriteString(options.absolutePrefix)
|
||||||
|
if link[0] != '/' {
|
||||||
|
out.WriteByte('/')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (options *Html) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
|
func (options *Html) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) {
|
||||||
if options.flags&HTML_SKIP_IMAGES != 0 {
|
if options.flags&HTML_SKIP_IMAGES != 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
out.WriteString("<img src=\"")
|
out.WriteString("<img src=\"")
|
||||||
|
options.maybeWriteAbsolutePrefix(out, link)
|
||||||
attrEscape(out, link)
|
attrEscape(out, link)
|
||||||
out.WriteString("\" alt=\"")
|
out.WriteString("\" alt=\"")
|
||||||
if len(alt) > 0 {
|
if len(alt) > 0 {
|
||||||
@ -503,6 +519,7 @@ func (options *Html) Link(out *bytes.Buffer, link []byte, title []byte, content
|
|||||||
}
|
}
|
||||||
|
|
||||||
out.WriteString("<a href=\"")
|
out.WriteString("<a href=\"")
|
||||||
|
options.maybeWriteAbsolutePrefix(out, link)
|
||||||
attrEscape(out, link)
|
attrEscape(out, link)
|
||||||
if len(title) > 0 {
|
if len(title) > 0 {
|
||||||
out.WriteString("\" title=\"")
|
out.WriteString("\" title=\"")
|
||||||
|
@ -23,7 +23,7 @@ func runMarkdownInline(input string, extensions, htmlFlags int) string {
|
|||||||
|
|
||||||
htmlFlags |= HTML_USE_XHTML
|
htmlFlags |= HTML_USE_XHTML
|
||||||
|
|
||||||
renderer := HtmlRenderer(htmlFlags, "", "")
|
renderer := HtmlRenderer(htmlFlags, "", "", "")
|
||||||
|
|
||||||
return string(Markdown([]byte(input), renderer, extensions))
|
return string(Markdown([]byte(input), renderer, extensions))
|
||||||
}
|
}
|
||||||
|
@ -202,7 +202,7 @@ type parser struct {
|
|||||||
func MarkdownBasic(input []byte) []byte {
|
func MarkdownBasic(input []byte) []byte {
|
||||||
// set up the HTML renderer
|
// set up the HTML renderer
|
||||||
htmlFlags := HTML_USE_XHTML
|
htmlFlags := HTML_USE_XHTML
|
||||||
renderer := HtmlRenderer(htmlFlags, "", "")
|
renderer := HtmlRenderer(htmlFlags, "", "", "")
|
||||||
|
|
||||||
// set up the parser
|
// set up the parser
|
||||||
extensions := 0
|
extensions := 0
|
||||||
@ -237,7 +237,7 @@ func MarkdownCommon(input []byte) []byte {
|
|||||||
htmlFlags |= HTML_SMARTYPANTS_FRACTIONS
|
htmlFlags |= HTML_SMARTYPANTS_FRACTIONS
|
||||||
htmlFlags |= HTML_SMARTYPANTS_LATEX_DASHES
|
htmlFlags |= HTML_SMARTYPANTS_LATEX_DASHES
|
||||||
htmlFlags |= HTML_SANITIZE_OUTPUT
|
htmlFlags |= HTML_SANITIZE_OUTPUT
|
||||||
renderer := HtmlRenderer(htmlFlags, "", "")
|
renderer := HtmlRenderer(htmlFlags, "", "", "")
|
||||||
|
|
||||||
// set up the parser
|
// set up the parser
|
||||||
extensions := 0
|
extensions := 0
|
||||||
@ -332,7 +332,7 @@ func firstPass(p *parser, input []byte) []byte {
|
|||||||
// when last line was none blank and a fenced code block comes after
|
// when last line was none blank and a fenced code block comes after
|
||||||
if beg >= lastFencedCodeBlockEnd {
|
if beg >= lastFencedCodeBlockEnd {
|
||||||
// tmp var so we don't modify beyond bounds of `input`
|
// tmp var so we don't modify beyond bounds of `input`
|
||||||
var tmp = make([]byte, len(input[beg:]), len(input[beg:]) + 1)
|
var tmp = make([]byte, len(input[beg:]), len(input[beg:])+1)
|
||||||
copy(tmp, input[beg:])
|
copy(tmp, input[beg:])
|
||||||
if i := p.fencedCode(&out, append(tmp, '\n'), false); i > 0 {
|
if i := p.fencedCode(&out, append(tmp, '\n'), false); i > 0 {
|
||||||
if !lastLineWasBlank {
|
if !lastLineWasBlank {
|
||||||
|
@ -20,7 +20,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func runMarkdownReference(input string, flag int) string {
|
func runMarkdownReference(input string, flag int) string {
|
||||||
renderer := HtmlRenderer(0, "", "")
|
renderer := HtmlRenderer(0, "", "", "")
|
||||||
return string(Markdown([]byte(input), renderer, flag))
|
return string(Markdown([]byte(input), renderer, flag))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user