-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add KaTeX rendering to Markdown. #20571
Merged
Merged
Changes from 7 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
f57fb78
Add KaTeX rendering to Markdown.
zeripath 3ac5fb5
Switch to always available rendering with ```math
zeripath 1f81006
remove goldmark meta and handle yaml frontmatter ourselves
zeripath fa26c58
missedremoval
zeripath 1d6a332
placate lint
zeripath 3a8c4f0
Merge remote-tracking branch 'origin/main' into katex-rendering
zeripath 11c5c8d
Merge branch 'main' into katex-rendering
zeripath eba0331
Merge remote-tracking branch 'origin/main' into katex-rendering
zeripath a775a9c
as per silverwind
zeripath ca3573d
Apply suggestions from code review
zeripath 810c790
placate lint
zeripath 606ff7e
Merge remote-tracking branch 'origin/main' into katex-rendering
zeripath 3b361e2
Handle conflict from #20987
zeripath 438f9fa
Set inline dollar math to true - to match github
zeripath 47127fe
Merge remote-tracking branch 'origin/main' into katex-rendering
zeripath 056ab70
fix inline placement of display math
zeripath de68952
Merge branch 'main' into katex-rendering
lunny 7a2387a
Merge branch 'main' into katex-rendering
zeripath 5289f35
Merge branch 'main' into katex-rendering
zeripath 0cb8346
Merge branch 'main' into katex-rendering
zeripath b45a6fa
Merge branch 'main' into katex-rendering
zeripath 39d7b47
Merge branch 'main' into katex-rendering
lunny a7d60af
Merge branch 'main' into katex-rendering
lunny 841a22c
Merge branch 'main' into katex-rendering
silverwind 563a798
Merge branch 'main' into katex-rendering
lunny f114106
remove inline dollar option
zeripath 4b138db
fix spelling bug
zeripath 92d3748
comments
zeripath e7851ca
as per delvh
zeripath 07c13ce
Merge branch 'main' into katex-rendering
zeripath 97dcc1f
Merge branch 'main' into katex-rendering
lunny 8eae07a
Merge branch 'main' into katex-rendering
zeripath 59e0b7f
docs update
silverwind b53d5cb
Merge branch 'main' into katex-rendering
zeripath caa4130
Merge branch 'main' into katex-rendering
zeripath 3409860
Merge branch 'main' into katex-rendering
lunny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// 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 markdown | ||
|
||
import ( | ||
"github.com/yuin/goldmark/ast" | ||
east "github.com/yuin/goldmark/extension/ast" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func nodeToTable(meta *yaml.Node) ast.Node { | ||
for { | ||
if meta == nil { | ||
return nil | ||
} | ||
switch meta.Kind { | ||
case yaml.DocumentNode: | ||
meta = meta.Content[0] | ||
continue | ||
default: | ||
} | ||
break | ||
} | ||
switch meta.Kind { | ||
case yaml.MappingNode: | ||
return mappingNodeToTable(meta) | ||
case yaml.SequenceNode: | ||
return sequenceNodeToTable(meta) | ||
default: | ||
return ast.NewString([]byte(meta.Value)) | ||
} | ||
} | ||
|
||
func mappingNodeToTable(meta *yaml.Node) ast.Node { | ||
table := east.NewTable() | ||
alignments := []east.Alignment{} | ||
for i := 0; i < len(meta.Content); i += 2 { | ||
alignments = append(alignments, east.AlignNone) | ||
} | ||
|
||
headerRow := east.NewTableRow(alignments) | ||
valueRow := east.NewTableRow(alignments) | ||
for i := 0; i < len(meta.Content); i += 2 { | ||
cell := east.NewTableCell() | ||
|
||
cell.AppendChild(cell, nodeToTable(meta.Content[i])) | ||
headerRow.AppendChild(headerRow, cell) | ||
|
||
if i+1 < len(meta.Content) { | ||
cell = east.NewTableCell() | ||
cell.AppendChild(cell, nodeToTable(meta.Content[i+1])) | ||
valueRow.AppendChild(valueRow, cell) | ||
} | ||
} | ||
|
||
table.AppendChild(table, east.NewTableHeader(headerRow)) | ||
table.AppendChild(table, valueRow) | ||
return table | ||
} | ||
|
||
func sequenceNodeToTable(meta *yaml.Node) ast.Node { | ||
table := east.NewTable() | ||
alignments := []east.Alignment{east.AlignNone} | ||
for _, item := range meta.Content { | ||
row := east.NewTableRow(alignments) | ||
cell := east.NewTableCell() | ||
cell.AppendChild(cell, nodeToTable(item)) | ||
row.AppendChild(row, cell) | ||
table.AppendChild(table, row) | ||
} | ||
return table | ||
} | ||
|
||
func nodeToDetails(meta *yaml.Node, icon string) ast.Node { | ||
details := NewDetails() | ||
summary := NewSummary() | ||
summary.AppendChild(summary, NewIcon(icon)) | ||
details.AppendChild(details, summary) | ||
details.AppendChild(details, nodeToTable(meta)) | ||
|
||
return details | ||
} |
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 | ||
zeripath marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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,43 @@ | ||
// 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{} | ||
|
||
// NewBlockRenderer creates a new renderer for math Blocks | ||
func NewBlockRenderer() renderer.NodeRenderer { | ||
return &BlockRenderer{} | ||
} | ||
|
||
// 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(`<pre class="code-block is-loading"><code class="chroma language-math display">`) | ||
r.writeLines(w, source, n) | ||
} else { | ||
_, _ = w.WriteString(`</code></pre>` + "\n") | ||
} | ||
return gast.WalkContinue, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe enable by default as well? Any downsides?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are lots of downsides for this one - KaTeX strongly recommends against this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm okay, there's certainly risk for wrong interpretations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have any links to KaTeX docs on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given Github have enabled this by default I think we have to do so too.