-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
Showing
19 changed files
with
635 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright 2022 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 math | ||
|
||
import "github.com/yuin/goldmark/ast" | ||
|
||
// Block represents a math Block | ||
type Block struct { | ||
ast.BaseBlock | ||
} | ||
|
||
// KindBlock is the node kind for math blocks | ||
var KindBlock = ast.NewNodeKind("MathBlock") | ||
|
||
// NewBlock creates a new math Block | ||
func NewBlock() *Block { | ||
return &Block{} | ||
} | ||
|
||
// Dump dumps the block to a string | ||
func (n *Block) Dump(source []byte, level int) { | ||
m := map[string]string{} | ||
ast.DumpHelper(n, source, level, m, nil) | ||
} | ||
|
||
// Kind returns KindBlock for math Blocks | ||
func (n *Block) Kind() ast.NodeKind { | ||
return KindBlock | ||
} | ||
|
||
// IsRaw returns true as this block should not be processed further | ||
func (n *Block) IsRaw() bool { | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright 2022 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 math | ||
|
||
import ( | ||
"github.com/yuin/goldmark/ast" | ||
"github.com/yuin/goldmark/parser" | ||
"github.com/yuin/goldmark/text" | ||
"github.com/yuin/goldmark/util" | ||
) | ||
|
||
type blockParser struct { | ||
parseDollars bool | ||
} | ||
|
||
type blockData struct { | ||
dollars bool | ||
indent int | ||
} | ||
|
||
var blockInfoKey = parser.NewContextKey() | ||
|
||
// NewBlockParser creates a new math BlockParser | ||
func NewBlockParser(parseDollarBlocks bool) parser.BlockParser { | ||
return &blockParser{ | ||
parseDollars: parseDollarBlocks, | ||
} | ||
} | ||
|
||
// Open parses the current line and returns a result of parsing. | ||
func (b *blockParser) Open(parent ast.Node, reader text.Reader, pc parser.Context) (ast.Node, parser.State) { | ||
line, _ := reader.PeekLine() | ||
pos := pc.BlockOffset() | ||
if pos == -1 || len(line[pos:]) < 2 { | ||
return nil, parser.NoChildren | ||
} | ||
|
||
dollars := false | ||
if b.parseDollars && line[pos] == '$' && line[pos+1] == '$' { | ||
dollars = true | ||
} else if line[pos] != '\\' || line[pos+1] != '[' { | ||
return nil, parser.NoChildren | ||
} | ||
|
||
pc.Set(blockInfoKey, &blockData{dollars: dollars, indent: pos}) | ||
node := NewBlock() | ||
return node, parser.NoChildren | ||
} | ||
|
||
// Continue parses the current line and returns a result of parsing. | ||
func (b *blockParser) Continue(node ast.Node, reader text.Reader, pc parser.Context) parser.State { | ||
line, segment := reader.PeekLine() | ||
data := pc.Get(blockInfoKey).(*blockData) | ||
w, pos := util.IndentWidth(line, 0) | ||
if w < 4 { | ||
if data.dollars { | ||
i := pos | ||
for ; i < len(line) && line[i] == '$'; i++ { | ||
} | ||
length := i - pos | ||
if length >= 2 && util.IsBlank(line[i:]) { | ||
reader.Advance(segment.Stop - segment.Start - segment.Padding) | ||
return parser.Close | ||
} | ||
} else if len(line[pos:]) > 1 && line[pos] == '\\' && line[pos+1] == ']' && util.IsBlank(line[pos+2:]) { | ||
reader.Advance(segment.Stop - segment.Start - segment.Padding) | ||
return parser.Close | ||
} | ||
} | ||
|
||
pos, padding := util.IndentPosition(line, 0, data.indent) | ||
seg := text.NewSegmentPadding(segment.Start+pos, segment.Stop, padding) | ||
node.Lines().Append(seg) | ||
reader.AdvanceAndSetPadding(segment.Stop-segment.Start-pos-1, padding) | ||
return parser.Continue | parser.NoChildren | ||
} | ||
|
||
// Close will be called when the parser returns Close. | ||
func (b *blockParser) Close(node ast.Node, reader text.Reader, pc parser.Context) { | ||
pc.Set(blockInfoKey, nil) | ||
} | ||
|
||
// CanInterruptParagraph returns true if the parser can interrupt paragraphs, | ||
// otherwise false. | ||
func (b *blockParser) CanInterruptParagraph() bool { | ||
return true | ||
} | ||
|
||
// CanAcceptIndentedLine returns true if the parser can open new node when | ||
// the given line is being indented more than 3 spaces. | ||
func (b *blockParser) CanAcceptIndentedLine() bool { | ||
return false | ||
} | ||
|
||
// Trigger returns a list of characters that triggers Parse method of | ||
// this parser. | ||
// If Trigger returns a nil, Open will be called with any lines. | ||
// | ||
// We leave this as nil as our parse method is quick enough | ||
func (b *blockParser) Trigger() []byte { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2022 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 math | ||
|
||
import ( | ||
gast "github.com/yuin/goldmark/ast" | ||
"github.com/yuin/goldmark/renderer" | ||
"github.com/yuin/goldmark/util" | ||
) | ||
|
||
// BlockRenderer represents a renderer for math Blocks | ||
type BlockRenderer struct { | ||
startDelim string | ||
endDelim string | ||
} | ||
|
||
// NewBlockRenderer creates a new renderer for math Blocks | ||
func NewBlockRenderer(start, end string) renderer.NodeRenderer { | ||
return &BlockRenderer{start, end} | ||
} | ||
|
||
// RegisterFuncs registers the renderer for math Blocks | ||
func (r *BlockRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) { | ||
reg.Register(KindBlock, r.renderBlock) | ||
} | ||
|
||
func (r *BlockRenderer) writeLines(w util.BufWriter, source []byte, n gast.Node) { | ||
l := n.Lines().Len() | ||
for i := 0; i < l; i++ { | ||
line := n.Lines().At(i) | ||
_, _ = w.Write(util.EscapeHTML(line.Value(source))) | ||
} | ||
} | ||
|
||
func (r *BlockRenderer) renderBlock(w util.BufWriter, source []byte, node gast.Node, entering bool) (gast.WalkStatus, error) { | ||
n := node.(*Block) | ||
if entering { | ||
_, _ = w.WriteString(`<p><span class="math display">` + r.startDelim) | ||
r.writeLines(w, source, n) | ||
} else { | ||
_, _ = w.WriteString(r.endDelim + `</span></p>` + "\n") | ||
} | ||
return gast.WalkContinue, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2022 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 math | ||
|
||
import ( | ||
"github.com/yuin/goldmark/ast" | ||
"github.com/yuin/goldmark/util" | ||
) | ||
|
||
// Inline represents inline math | ||
type Inline struct { | ||
ast.BaseInline | ||
} | ||
|
||
// Inline implements Inline.Inline. | ||
func (n *Inline) Inline() {} | ||
|
||
// IsBlank returns if this inline node is empty | ||
func (n *Inline) IsBlank(source []byte) bool { | ||
for c := n.FirstChild(); c != nil; c = c.NextSibling() { | ||
text := c.(*ast.Text).Segment | ||
if !util.IsBlank(text.Value(source)) { | ||
return false | ||
} | ||
} | ||
return true | ||
} | ||
|
||
// Dump renders this inline math as debug | ||
func (n *Inline) Dump(source []byte, level int) { | ||
ast.DumpHelper(n, source, level, nil, nil) | ||
} | ||
|
||
// KindInline is the kind for math inline | ||
var KindInline = ast.NewNodeKind("MathInline") | ||
|
||
// Kind returns KindInline | ||
func (n *Inline) Kind() ast.NodeKind { | ||
return KindInline | ||
} | ||
|
||
// NewInline creates a new ast math inline node | ||
func NewInline() *Inline { | ||
return &Inline{ | ||
BaseInline: ast.BaseInline{}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright 2022 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 math | ||
|
||
import ( | ||
"bytes" | ||
|
||
"github.com/yuin/goldmark/ast" | ||
"github.com/yuin/goldmark/parser" | ||
"github.com/yuin/goldmark/text" | ||
"github.com/yuin/goldmark/util" | ||
) | ||
|
||
type inlineParser struct { | ||
start []byte | ||
end []byte | ||
} | ||
|
||
var defaultInlineDollarParser = &inlineParser{ | ||
start: []byte{'$'}, | ||
end: []byte{'$'}, | ||
} | ||
|
||
// NewInlineDollarParser returns a new inline parser | ||
func NewInlineDollarParser() parser.InlineParser { | ||
return defaultInlineDollarParser | ||
} | ||
|
||
var defaultInlineBracketParser = &inlineParser{ | ||
start: []byte{'\\', '('}, | ||
end: []byte{'\\', ')'}, | ||
} | ||
|
||
// NewInlineDollarParser returns a new inline parser | ||
func NewInlineBracketParser() parser.InlineParser { | ||
return defaultInlineBracketParser | ||
} | ||
|
||
// Trigger triggers this parser on $ | ||
func (parser *inlineParser) Trigger() []byte { | ||
return parser.start[0:1] | ||
} | ||
|
||
// Parse parses the current line and returns a result of parsing. | ||
func (parser *inlineParser) Parse(parent ast.Node, block text.Reader, pc parser.Context) ast.Node { | ||
line, startSegment := block.PeekLine() | ||
opener := bytes.Index(line, parser.start) | ||
if opener < 0 { | ||
return nil | ||
} | ||
opener += len(parser.start) | ||
block.Advance(opener) | ||
l, pos := block.Position() | ||
node := NewInline() | ||
|
||
for { | ||
line, segment := block.PeekLine() | ||
if line == nil { | ||
block.SetPosition(l, pos) | ||
return ast.NewTextSegment(startSegment.WithStop(startSegment.Start + opener)) | ||
} | ||
|
||
closer := bytes.Index(line, parser.end) | ||
if closer < 0 { | ||
if !util.IsBlank(line) { | ||
node.AppendChild(node, ast.NewRawTextSegment(segment)) | ||
} | ||
block.AdvanceLine() | ||
continue | ||
} | ||
segment = segment.WithStop(segment.Start + closer) | ||
if !segment.IsEmpty() { | ||
node.AppendChild(node, ast.NewRawTextSegment(segment)) | ||
} | ||
block.Advance(closer + len(parser.end)) | ||
break | ||
} | ||
|
||
trimBlock(node, block) | ||
return node | ||
} | ||
|
||
func trimBlock(node *Inline, block text.Reader) { | ||
if node.IsBlank(block.Source()) { | ||
return | ||
} | ||
|
||
// trim first space and last space | ||
first := node.FirstChild().(*ast.Text) | ||
if !(!first.Segment.IsEmpty() && block.Source()[first.Segment.Start] == ' ') { | ||
return | ||
} | ||
|
||
last := node.LastChild().(*ast.Text) | ||
if !(!last.Segment.IsEmpty() && block.Source()[last.Segment.Stop-1] == ' ') { | ||
return | ||
} | ||
|
||
first.Segment = first.Segment.WithStart(first.Segment.Start + 1) | ||
last.Segment = last.Segment.WithStop(last.Segment.Stop - 1) | ||
} |
Oops, something went wrong.