// Copyright 2014 The Gogs Authors. All rights reserved. // Copyright 2018 The Gitea Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. package markdown import ( "bytes" "sync" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/markup" "code.gitea.io/gitea/modules/markup/common" "code.gitea.io/gitea/modules/setting" giteautil "code.gitea.io/gitea/modules/util" chromahtml "github.com/alecthomas/chroma/formatters/html" "github.com/yuin/goldmark" "github.com/yuin/goldmark-highlighting" meta "github.com/yuin/goldmark-meta" "github.com/yuin/goldmark/extension" "github.com/yuin/goldmark/parser" "github.com/yuin/goldmark/renderer" "github.com/yuin/goldmark/renderer/html" "github.com/yuin/goldmark/util" ) var converter goldmark.Markdown var once = sync.Once{} var urlPrefixKey = parser.NewContextKey() var isWikiKey = parser.NewContextKey() var renderMetasKey = parser.NewContextKey() // NewGiteaParseContext creates a parser.Context with the gitea context set func NewGiteaParseContext(urlPrefix string, metas map[string]string, isWiki bool) parser.Context { pc := parser.NewContext(parser.WithIDs(newPrefixedIDs())) pc.Set(urlPrefixKey, urlPrefix) pc.Set(isWikiKey, isWiki) pc.Set(renderMetasKey, metas) return pc } // render renders Markdown to HTML without handling special links. func render(body []byte, urlPrefix string, metas map[string]string, wikiMarkdown bool) []byte { once.Do(func() { converter = goldmark.New( goldmark.WithExtensions(extension.Table, extension.Strikethrough, extension.TaskList, extension.DefinitionList, common.FootnoteExtension, highlighting.NewHighlighting( highlighting.WithFormatOptions( chromahtml.WithClasses(true), chromahtml.PreventSurroundingPre(true), ), highlighting.WithWrapperRenderer(func(w util.BufWriter, c highlighting.CodeBlockContext, entering bool) { language, _ := c.Language() if language == nil { language = []byte("text") } if entering { // include language-x class as part of commonmark spec _, err := w.WriteString("
")
							if err != nil {
								return
							}
						} else {
							_, err := w.WriteString("