123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- package markup
- import (
- "bytes"
- "fmt"
- "path"
- "path/filepath"
- "strings"
- "github.com/russross/blackfriday"
- "gogs.io/gogs/internal/conf"
- "gogs.io/gogs/internal/lazyregexp"
- "gogs.io/gogs/internal/tool"
- )
- func IsMarkdownFile(name string) bool {
- extension := strings.ToLower(filepath.Ext(name))
- for _, ext := range conf.Markdown.FileExtensions {
- if strings.ToLower(ext) == extension {
- return true
- }
- }
- return false
- }
- type MarkdownRenderer struct {
- blackfriday.Renderer
- urlPrefix string
- }
- var validLinksPattern = lazyregexp.New(`^[a-z][\w-]+://|^mailto:`)
- func isLink(link []byte) bool {
- return validLinksPattern.Match(link)
- }
- func (r *MarkdownRenderer) Link(out *bytes.Buffer, link []byte, title []byte, content []byte) {
- if len(link) > 0 && !isLink(link) {
- if link[0] != '#' {
- link = []byte(path.Join(r.urlPrefix, string(link)))
- }
- }
- r.Renderer.Link(out, link, title, content)
- }
- func (r *MarkdownRenderer) AutoLink(out *bytes.Buffer, link []byte, kind int) {
- if kind != blackfriday.LINK_TYPE_NORMAL {
- r.Renderer.AutoLink(out, link, kind)
- return
- }
-
-
- if bytes.HasPrefix(link, []byte(conf.Server.ExternalURL)) {
- 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)
- }
- out.WriteString(fmt.Sprintf(` <code><a href="%s">%s</a></code>`, m, tool.ShortSHA1(string(m[i+7:j]))))
- return
- }
- 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)
- }
- index := string(m[i+7 : j])
- fullRepoURL := conf.Server.ExternalURL + strings.TrimPrefix(r.urlPrefix, "/")
- var link string
- if strings.HasPrefix(string(m), fullRepoURL) {
-
- link = fmt.Sprintf(`<a href="%s">#%s</a>`, m, index)
- } else {
-
- repo := string(m[len(conf.Server.ExternalURL) : i-1])
- link = fmt.Sprintf(`<a href="%s">%s#%s</a>`, m, repo, index)
- }
- out.WriteString(link)
- return
- }
- }
- r.Renderer.AutoLink(out, link, kind)
- }
- func (options *MarkdownRenderer) ListItem(out *bytes.Buffer, text []byte, flags int) {
-
- 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)
- }
- func RawMarkdown(body []byte, urlPrefix string) []byte {
- htmlFlags := 0
- htmlFlags |= blackfriday.HTML_SKIP_STYLE
- htmlFlags |= blackfriday.HTML_OMIT_CONTENTS
- if conf.Smartypants.Enabled {
- htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS
- if conf.Smartypants.Fractions {
- htmlFlags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
- }
- if conf.Smartypants.Dashes {
- htmlFlags |= blackfriday.HTML_SMARTYPANTS_DASHES
- }
- if conf.Smartypants.LatexDashes {
- htmlFlags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
- }
- if conf.Smartypants.AngledQuotes {
- htmlFlags |= blackfriday.HTML_SMARTYPANTS_ANGLED_QUOTES
- }
- }
- renderer := &MarkdownRenderer{
- Renderer: blackfriday.HtmlRenderer(htmlFlags, "", ""),
- urlPrefix: urlPrefix,
- }
-
- extensions := 0
- 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
- if conf.Markdown.EnableHardLineBreak {
- extensions |= blackfriday.EXTENSION_HARD_LINE_BREAK
- }
- return blackfriday.Markdown(body, renderer, extensions)
- }
- func Markdown(input interface{}, urlPrefix string, metas map[string]string) []byte {
- return Render(MARKDOWN, input, urlPrefix, metas)
- }
|