mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-27 09:11:53 -05:00
88c2e24360
This PR adds mathematical rendering with KaTeX. The first step is to add a Goldmark extension that detects the latex (and tex) mathematics delimiters. The second step to make this extension only run if math support is enabled. The second step is to then add KaTeX CSS and JS to the head which will load after the dom is rendered. Fix #3445 Signed-off-by: Andrew Thornton <art27@cantab.net> Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
118 lines
2.3 KiB
Go
118 lines
2.3 KiB
Go
// Copyright 2020 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 (
|
|
"strings"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
"github.com/yuin/goldmark/ast"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// RenderConfig represents rendering configuration for this file
|
|
type RenderConfig struct {
|
|
Meta string
|
|
Icon string
|
|
TOC bool
|
|
Lang string
|
|
yamlNode *yaml.Node
|
|
}
|
|
|
|
// UnmarshalYAML implement yaml.v3 UnmarshalYAML
|
|
func (rc *RenderConfig) UnmarshalYAML(value *yaml.Node) error {
|
|
if rc == nil {
|
|
rc = &RenderConfig{
|
|
Meta: "table",
|
|
Icon: "table",
|
|
Lang: "",
|
|
}
|
|
}
|
|
rc.yamlNode = value
|
|
|
|
type basicRenderConfig struct {
|
|
Gitea *yaml.Node `yaml:"gitea"`
|
|
TOC bool `yaml:"include_toc"`
|
|
Lang string `yaml:"lang"`
|
|
}
|
|
|
|
var basic basicRenderConfig
|
|
|
|
err := value.Decode(&basic)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if basic.Lang != "" {
|
|
rc.Lang = basic.Lang
|
|
}
|
|
|
|
rc.TOC = basic.TOC
|
|
if basic.Gitea == nil {
|
|
return nil
|
|
}
|
|
|
|
var control *string
|
|
if err := basic.Gitea.Decode(&control); err == nil && control != nil {
|
|
log.Info("control %v", control)
|
|
switch strings.TrimSpace(strings.ToLower(*control)) {
|
|
case "none":
|
|
rc.Meta = "none"
|
|
case "table":
|
|
rc.Meta = "table"
|
|
default: // "details"
|
|
rc.Meta = "details"
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type giteaControl struct {
|
|
Meta string `yaml:"meta"`
|
|
Icon string `yaml:"details_icon"`
|
|
TOC *yaml.Node `yaml:"include_toc"`
|
|
Lang string `yaml:"lang"`
|
|
}
|
|
|
|
var controlStruct *giteaControl
|
|
if err := basic.Gitea.Decode(controlStruct); err != nil || controlStruct == nil {
|
|
return err
|
|
}
|
|
|
|
switch strings.TrimSpace(strings.ToLower(controlStruct.Meta)) {
|
|
case "none":
|
|
rc.Meta = "none"
|
|
case "table":
|
|
rc.Meta = "table"
|
|
default: // "details"
|
|
rc.Meta = "details"
|
|
}
|
|
|
|
rc.Icon = strings.TrimSpace(strings.ToLower(controlStruct.Icon))
|
|
|
|
if controlStruct.Lang != "" {
|
|
rc.Lang = controlStruct.Lang
|
|
}
|
|
|
|
var toc bool
|
|
if err := controlStruct.TOC.Decode(&toc); err == nil {
|
|
rc.TOC = toc
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (rc *RenderConfig) toMetaNode() ast.Node {
|
|
if rc.yamlNode == nil {
|
|
return nil
|
|
}
|
|
switch rc.Meta {
|
|
case "table":
|
|
return nodeToTable(rc.yamlNode)
|
|
case "details":
|
|
return nodeToDetails(rc.yamlNode, rc.Icon)
|
|
default:
|
|
return nil
|
|
}
|
|
}
|