mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-04 13:50:39 +00:00
Add KaTeX rendering to Markdown. (#20571)
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>
This commit is contained in:
parent
eaa561145a
commit
88c2e24360
28 changed files with 1079 additions and 174 deletions
|
@ -5,47 +5,101 @@
|
|||
package markdown
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"strings"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func isYAMLSeparator(line string) bool {
|
||||
line = strings.TrimSpace(line)
|
||||
for i := 0; i < len(line); i++ {
|
||||
if line[i] != '-' {
|
||||
func isYAMLSeparator(line []byte) bool {
|
||||
idx := 0
|
||||
for ; idx < len(line); idx++ {
|
||||
if line[idx] >= utf8.RuneSelf {
|
||||
r, sz := utf8.DecodeRune(line[idx:])
|
||||
if !unicode.IsSpace(r) {
|
||||
return false
|
||||
}
|
||||
idx += sz
|
||||
continue
|
||||
}
|
||||
if line[idx] != ' ' {
|
||||
break
|
||||
}
|
||||
}
|
||||
dashCount := 0
|
||||
for ; idx < len(line); idx++ {
|
||||
if line[idx] != '-' {
|
||||
break
|
||||
}
|
||||
dashCount++
|
||||
}
|
||||
if dashCount < 3 {
|
||||
return false
|
||||
}
|
||||
for ; idx < len(line); idx++ {
|
||||
if line[idx] >= utf8.RuneSelf {
|
||||
r, sz := utf8.DecodeRune(line[idx:])
|
||||
if !unicode.IsSpace(r) {
|
||||
return false
|
||||
}
|
||||
idx += sz
|
||||
continue
|
||||
}
|
||||
if line[idx] != ' ' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return len(line) > 2
|
||||
return true
|
||||
}
|
||||
|
||||
// ExtractMetadata consumes a markdown file, parses YAML frontmatter,
|
||||
// and returns the frontmatter metadata separated from the markdown content
|
||||
func ExtractMetadata(contents string, out interface{}) (string, error) {
|
||||
var front, body []string
|
||||
lines := strings.Split(contents, "\n")
|
||||
for idx, line := range lines {
|
||||
if idx == 0 {
|
||||
// First line has to be a separator
|
||||
if !isYAMLSeparator(line) {
|
||||
return "", errors.New("frontmatter must start with a separator line")
|
||||
}
|
||||
continue
|
||||
body, err := ExtractMetadataBytes([]byte(contents), out)
|
||||
return string(body), err
|
||||
}
|
||||
|
||||
// ExtractMetadata consumes a markdown file, parses YAML frontmatter,
|
||||
// and returns the frontmatter metadata separated from the markdown content
|
||||
func ExtractMetadataBytes(contents []byte, out interface{}) ([]byte, error) {
|
||||
var front, body []byte
|
||||
|
||||
start, end := 0, len(contents)
|
||||
idx := bytes.IndexByte(contents[start:], '\n')
|
||||
if idx >= 0 {
|
||||
end = start + idx
|
||||
}
|
||||
line := contents[start:end]
|
||||
|
||||
if !isYAMLSeparator(line) {
|
||||
return contents, errors.New("frontmatter must start with a separator line")
|
||||
}
|
||||
frontMatterStart := end + 1
|
||||
for start = frontMatterStart; start < len(contents); start = end + 1 {
|
||||
end = len(contents)
|
||||
idx := bytes.IndexByte(contents[start:], '\n')
|
||||
if idx >= 0 {
|
||||
end = start + idx
|
||||
}
|
||||
line := contents[start:end]
|
||||
if isYAMLSeparator(line) {
|
||||
front, body = lines[1:idx], lines[idx+1:]
|
||||
front = contents[frontMatterStart:start]
|
||||
body = contents[end+1:]
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if len(front) == 0 {
|
||||
return "", errors.New("could not determine metadata")
|
||||
return contents, errors.New("could not determine metadata")
|
||||
}
|
||||
|
||||
if err := yaml.Unmarshal([]byte(strings.Join(front, "\n")), out); err != nil {
|
||||
return "", err
|
||||
log.Info("%s", string(front))
|
||||
|
||||
if err := yaml.Unmarshal(front, out); err != nil {
|
||||
return contents, err
|
||||
}
|
||||
return strings.Join(body, "\n"), nil
|
||||
return body, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue