forked from forgejo/forgejo
Rework special link parsing in the post-processing of markup (#3354)
* Get rid of autolink * autolink in markdown * Replace email addresses with mailto links * better handling of links * Remove autolink.js from footer * Refactor entire html.go * fix some bugs * Make tests green, move what we can to html_internal_test, various other changes to processor logic * Make markdown tests work again This is just a description to allow me to force push in order to restart the drone build. * Fix failing markdown tests in routers/api/v1/misc * Add license headers, log errors, future-proof <body> * fix formatting
This commit is contained in:
parent
769ab1e424
commit
535445c32e
12 changed files with 1029 additions and 1025 deletions
|
@ -22,17 +22,20 @@ type Renderer struct {
|
|||
IsWiki bool
|
||||
}
|
||||
|
||||
var byteMailto = []byte("mailto:")
|
||||
|
||||
// Link defines how formal links should be processed to produce corresponding HTML elements.
|
||||
func (r *Renderer) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
|
||||
if len(link) > 0 && !markup.IsLink(link) {
|
||||
if link[0] != '#' {
|
||||
lnk := string(link)
|
||||
if r.IsWiki {
|
||||
lnk = util.URLJoin("wiki", lnk)
|
||||
}
|
||||
mLink := util.URLJoin(r.URLPrefix, lnk)
|
||||
link = []byte(mLink)
|
||||
// special case: this is not a link, a hash link or a mailto:, so it's a
|
||||
// relative URL
|
||||
if len(link) > 0 && !markup.IsLink(link) &&
|
||||
link[0] != '#' && !bytes.HasPrefix(link, byteMailto) {
|
||||
lnk := string(link)
|
||||
if r.IsWiki {
|
||||
lnk = util.URLJoin("wiki", lnk)
|
||||
}
|
||||
mLink := util.URLJoin(r.URLPrefix, lnk)
|
||||
link = []byte(mLink)
|
||||
}
|
||||
|
||||
r.Renderer.Link(out, link, title, content)
|
||||
|
@ -124,30 +127,33 @@ func (r *Renderer) Image(out *bytes.Buffer, link []byte, title []byte, alt []byt
|
|||
out.WriteString("</a>")
|
||||
}
|
||||
|
||||
const (
|
||||
blackfridayExtensions = 0 |
|
||||
blackfriday.EXTENSION_NO_INTRA_EMPHASIS |
|
||||
blackfriday.EXTENSION_TABLES |
|
||||
blackfriday.EXTENSION_FENCED_CODE |
|
||||
blackfriday.EXTENSION_STRIKETHROUGH |
|
||||
blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK
|
||||
blackfridayHTMLFlags = 0 |
|
||||
blackfriday.HTML_SKIP_STYLE |
|
||||
blackfriday.HTML_OMIT_CONTENTS |
|
||||
blackfriday.HTML_USE_SMARTYPANTS
|
||||
)
|
||||
|
||||
// RenderRaw renders Markdown to HTML without handling special links.
|
||||
func RenderRaw(body []byte, urlPrefix string, wikiMarkdown bool) []byte {
|
||||
htmlFlags := 0
|
||||
htmlFlags |= blackfriday.HTML_SKIP_STYLE
|
||||
htmlFlags |= blackfriday.HTML_OMIT_CONTENTS
|
||||
renderer := &Renderer{
|
||||
Renderer: blackfriday.HtmlRenderer(htmlFlags, "", ""),
|
||||
Renderer: blackfriday.HtmlRenderer(blackfridayHTMLFlags, "", ""),
|
||||
URLPrefix: urlPrefix,
|
||||
IsWiki: wikiMarkdown,
|
||||
}
|
||||
|
||||
// set up the parser
|
||||
extensions := 0
|
||||
extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS
|
||||
extensions |= blackfriday.EXTENSION_TABLES
|
||||
extensions |= blackfriday.EXTENSION_FENCED_CODE
|
||||
extensions |= blackfriday.EXTENSION_STRIKETHROUGH
|
||||
extensions |= blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK
|
||||
|
||||
exts := blackfridayExtensions
|
||||
if setting.Markdown.EnableHardLineBreak {
|
||||
extensions |= blackfriday.EXTENSION_HARD_LINE_BREAK
|
||||
exts |= blackfriday.EXTENSION_HARD_LINE_BREAK
|
||||
}
|
||||
|
||||
body = blackfriday.Markdown(body, renderer, extensions)
|
||||
body = blackfriday.Markdown(body, renderer, exts)
|
||||
return body
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue