2014-04-10 14:20:58 -04:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
2017-03-31 15:29:43 -04:00
|
|
|
package markup
|
2014-04-10 14:20:58 -04:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"fmt"
|
|
|
|
|
"path"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"regexp"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2014-10-04 17:15:22 -04:00
|
|
|
"github.com/russross/blackfriday"
|
|
|
|
|
|
2019-10-23 23:03:17 -07:00
|
|
|
"gogs.io/gogs/pkg/setting"
|
|
|
|
|
"gogs.io/gogs/pkg/tool"
|
2014-04-10 14:20:58 -04:00
|
|
|
)
|
|
|
|
|
|
2017-03-31 17:46:57 -04:00
|
|
|
// IsMarkdownFile reports whether name looks like a Markdown file based on its extension.
|
2014-04-10 14:20:58 -04:00
|
|
|
func IsMarkdownFile(name string) bool {
|
2016-08-11 05:48:08 -07:00
|
|
|
extension := strings.ToLower(filepath.Ext(name))
|
2016-08-12 02:29:29 -07:00
|
|
|
for _, ext := range setting.Markdown.FileExtensions {
|
2016-08-11 05:48:08 -07:00
|
|
|
if strings.ToLower(ext) == extension {
|
|
|
|
|
return true
|
|
|
|
|
}
|
2014-04-10 14:20:58 -04:00
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-31 17:46:57 -04:00
|
|
|
// MarkdownRenderer is a extended version of underlying Markdown render object.
|
|
|
|
|
type MarkdownRenderer struct {
|
|
|
|
|
blackfriday.Renderer
|
|
|
|
|
urlPrefix string
|
2014-04-10 14:20:58 -04:00
|
|
|
}
|
|
|
|
|
|
2017-03-31 17:46:57 -04:00
|
|
|
var validLinksPattern = regexp.MustCompile(`^[a-z][\w-]+://|^mailto:`)
|
2016-07-16 00:36:39 +08:00
|
|
|
|
2017-03-31 17:46:57 -04:00
|
|
|
// isLink reports whether link fits valid format.
|
|
|
|
|
func isLink(link []byte) bool {
|
|
|
|
|
return validLinksPattern.Match(link)
|
2014-04-10 14:20:58 -04:00
|
|
|
}
|
|
|
|
|
|
2016-02-20 17:10:05 -05:00
|
|
|
// Link defines how formal links should be processed to produce corresponding HTML elements.
|
2017-03-31 17:46:57 -04:00
|
|
|
func (r *MarkdownRenderer) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
|
2014-04-10 14:20:58 -04:00
|
|
|
if len(link) > 0 && !isLink(link) {
|
2016-02-20 17:10:05 -05:00
|
|
|
if link[0] != '#' {
|
2016-01-09 10:59:04 +08:00
|
|
|
link = []byte(path.Join(r.urlPrefix, string(link)))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r.Renderer.Link(out, link, title, content)
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-20 17:10:05 -05:00
|
|
|
// AutoLink defines how auto-detected links should be processed to produce corresponding HTML elements.
|
|
|
|
|
// Reference for kind: https://github.com/russross/blackfriday/blob/master/markdown.go#L69-L76
|
2017-03-31 17:46:57 -04:00
|
|
|
func (r *MarkdownRenderer) AutoLink(out *bytes.Buffer, link []byte, kind int) {
|
2016-02-20 17:10:05 -05:00
|
|
|
if kind != blackfriday.LINK_TYPE_NORMAL {
|
2016-01-09 10:59:04 +08:00
|
|
|
r.Renderer.AutoLink(out, link, kind)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-20 17:10:05 -05:00
|
|
|
// Since this method could only possibly serve one link at a time,
|
|
|
|
|
// we do not need to find all.
|
2017-04-06 17:27:57 -04:00
|
|
|
if bytes.HasPrefix(link, []byte(setting.AppURL)) {
|
2016-06-29 10:07:39 -05:00
|
|
|
m := CommitPattern.Find(link)
|
|
|
|
|
if m != nil {
|
|
|
|
|
m = bytes.TrimSpace(m)
|
|
|
|
|
i := strings.Index(string(m), "commit/")
|
|
|
|
|
j := strings.Index(string(m), "#")
|
|
|
|
|
if j == -1 {
|
|
|
|
|
j = len(m)
|
|
|
|
|
}
|
2017-04-06 17:13:53 -04:00
|
|
|
out.WriteString(fmt.Sprintf(` <code><a href="%s">%s</a></code>`, m, tool.ShortSHA1(string(m[i+7:j]))))
|
2016-06-29 10:07:39 -05:00
|
|
|
return
|
2016-01-09 10:59:04 +08:00
|
|
|
}
|
|
|
|
|
|
2016-06-29 10:07:39 -05:00
|
|
|
m = IssueFullPattern.Find(link)
|
|
|
|
|
if m != nil {
|
|
|
|
|
m = bytes.TrimSpace(m)
|
|
|
|
|
i := strings.Index(string(m), "issues/")
|
|
|
|
|
j := strings.Index(string(m), "#")
|
|
|
|
|
if j == -1 {
|
|
|
|
|
j = len(m)
|
|
|
|
|
}
|
2016-12-23 16:49:53 -07:00
|
|
|
|
2016-12-23 19:00:24 -05:00
|
|
|
index := string(m[i+7 : j])
|
2017-04-06 17:27:57 -04:00
|
|
|
fullRepoURL := setting.AppURL + strings.TrimPrefix(r.urlPrefix, "/")
|
2016-12-23 16:49:53 -07:00
|
|
|
var link string
|
2016-12-23 19:00:24 -05:00
|
|
|
if strings.HasPrefix(string(m), fullRepoURL) {
|
2016-12-23 16:49:53 -07:00
|
|
|
// Use a short issue reference if the URL refers to this repository
|
2016-12-23 19:00:24 -05:00
|
|
|
link = fmt.Sprintf(`<a href="%s">#%s</a>`, m, index)
|
2016-12-23 16:49:53 -07:00
|
|
|
} else {
|
|
|
|
|
// Use a cross-repository issue reference if the URL refers to a different repository
|
2017-04-06 17:27:57 -04:00
|
|
|
repo := string(m[len(setting.AppURL) : i-1])
|
2016-12-23 19:00:24 -05:00
|
|
|
link = fmt.Sprintf(`<a href="%s">%s#%s</a>`, m, repo, index)
|
2016-12-23 16:49:53 -07:00
|
|
|
}
|
|
|
|
|
out.WriteString(link)
|
2016-06-29 10:07:39 -05:00
|
|
|
return
|
2014-04-10 14:20:58 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-09 10:59:04 +08:00
|
|
|
r.Renderer.AutoLink(out, link, kind)
|
2014-04-10 14:20:58 -04:00
|
|
|
}
|
|
|
|
|
|
2016-02-20 17:10:05 -05:00
|
|
|
// ListItem defines how list items should be processed to produce corresponding HTML elements.
|
2017-03-31 17:46:57 -04:00
|
|
|
func (options *MarkdownRenderer) ListItem(out *bytes.Buffer, text []byte, flags int) {
|
2016-02-20 17:10:05 -05:00
|
|
|
// Detect procedures to draw checkboxes.
|
2016-01-13 13:25:52 +01:00
|
|
|
switch {
|
|
|
|
|
case bytes.HasPrefix(text, []byte("[ ] ")):
|
|
|
|
|
text = append([]byte(`<input type="checkbox" disabled="" />`), text[3:]...)
|
|
|
|
|
case bytes.HasPrefix(text, []byte("[x] ")):
|
|
|
|
|
text = append([]byte(`<input type="checkbox" disabled="" checked="" />`), text[3:]...)
|
|
|
|
|
}
|
|
|
|
|
options.Renderer.ListItem(out, text, flags)
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-31 18:14:40 -04:00
|
|
|
// RawMarkdown renders content in Markdown syntax to HTML without handling special links.
|
2017-03-31 17:46:57 -04:00
|
|
|
func RawMarkdown(body []byte, urlPrefix string) []byte {
|
2014-04-10 14:20:58 -04:00
|
|
|
htmlFlags := 0
|
2014-10-04 17:15:22 -04:00
|
|
|
htmlFlags |= blackfriday.HTML_SKIP_STYLE
|
|
|
|
|
htmlFlags |= blackfriday.HTML_OMIT_CONTENTS
|
2017-03-10 23:30:40 -05:00
|
|
|
|
|
|
|
|
if setting.Smartypants.Enabled {
|
|
|
|
|
htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS
|
|
|
|
|
if setting.Smartypants.Fractions {
|
|
|
|
|
htmlFlags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
|
|
|
|
|
}
|
|
|
|
|
if setting.Smartypants.Dashes {
|
|
|
|
|
htmlFlags |= blackfriday.HTML_SMARTYPANTS_DASHES
|
|
|
|
|
}
|
|
|
|
|
if setting.Smartypants.LatexDashes {
|
|
|
|
|
htmlFlags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
|
|
|
|
|
}
|
|
|
|
|
if setting.Smartypants.AngledQuotes {
|
|
|
|
|
htmlFlags |= blackfriday.HTML_SMARTYPANTS_ANGLED_QUOTES
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-31 17:46:57 -04:00
|
|
|
renderer := &MarkdownRenderer{
|
2014-10-04 17:15:22 -04:00
|
|
|
Renderer: blackfriday.HtmlRenderer(htmlFlags, "", ""),
|
2014-04-10 14:20:58 -04:00
|
|
|
urlPrefix: urlPrefix,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// set up the parser
|
|
|
|
|
extensions := 0
|
2014-10-04 17:15:22 -04:00
|
|
|
extensions |= blackfriday.EXTENSION_NO_INTRA_EMPHASIS
|
|
|
|
|
extensions |= blackfriday.EXTENSION_TABLES
|
|
|
|
|
extensions |= blackfriday.EXTENSION_FENCED_CODE
|
|
|
|
|
extensions |= blackfriday.EXTENSION_AUTOLINK
|
|
|
|
|
extensions |= blackfriday.EXTENSION_STRIKETHROUGH
|
|
|
|
|
extensions |= blackfriday.EXTENSION_SPACE_HEADERS
|
|
|
|
|
extensions |= blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK
|
|
|
|
|
|
2015-09-01 08:32:02 -04:00
|
|
|
if setting.Markdown.EnableHardLineBreak {
|
|
|
|
|
extensions |= blackfriday.EXTENSION_HARD_LINE_BREAK
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-04 17:15:22 -04:00
|
|
|
body = blackfriday.Markdown(body, renderer, extensions)
|
2014-05-05 13:08:01 -04:00
|
|
|
return body
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-31 16:37:30 -04:00
|
|
|
// Markdown takes a string or []byte and renders to HTML in Markdown syntax with special links.
|
|
|
|
|
func Markdown(input interface{}, urlPrefix string, metas map[string]string) []byte {
|
2017-03-31 17:46:57 -04:00
|
|
|
return Render(MARKDOWN, input, urlPrefix, metas)
|
2015-03-23 18:32:24 -04:00
|
|
|
}
|