Skip to content

Commit

Permalink
🎨 Improve math block parsing https://ld246.com/article/1730804245164
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Nov 6, 2024
1 parent 6885b1e commit a4e60c7
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion javascript/lute.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion javascript/lute.min.js.map

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions parse/math_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ func (context *Context) mathBlockFinalize(mathBlock *ast.Node) {
if bytes.HasSuffix(tokens, MathBlockMarker) {
tokens = tokens[:len(tokens)-2] // 剔除结尾的 $$
}
if bytes.Contains(tokens, []byte("<span data-type=")) {
// 行级元素转换为块级元素 https://ld246.com/article/1730804245164
inlineTree := Inline("", tokens, context.ParseOption)
if nil != inlineTree {
tokens = []byte(inlineTree.Root.Content())
}
}

mathBlock.Tokens = nil
mathBlock.AppendChild(&ast.Node{Type: ast.NodeMathBlockOpenMarker})
mathBlock.AppendChild(&ast.Node{Type: ast.NodeMathBlockContent, Tokens: tokens})
Expand Down
1 change: 1 addition & 0 deletions test/spin_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

var spinBlockDOMTests = []*parseTest{

{"233", "<div data-node-id=\"20241106092017-k5ghamq\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20241106092032\"><div contenteditable=\"true\" spellcheck=\"false\">$$<wbr><span data-type=\"inline-math\" data-subtype=\"math\" data-content=\"foo\" contenteditable=\"false\" class=\"render-node\" data-render=\"true\"><span class=\"katex\"><span class=\"katex-html\" aria-hidden=\"true\"><span class=\"base\"><span class=\"strut\" style=\"height:0.8889em;vertical-align:-0.1944em;\"></span><span class=\"mord mathnormal\" style=\"margin-right:0.10764em;\">f</span><span class=\"mord mathnormal\">oo</span></span></span></span></span>\n</div><div class=\"protyle-attr\" contenteditable=\"false\">​</div></div>", "<div data-node-id=\"20241106092017-k5ghamq\" data-node-index=\"1\" data-type=\"NodeMathBlock\" class=\"render-node\" updated=\"20241106092032\" data-content=\"foo\" data-subtype=\"math\"><div spin=\"1\"></div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
{"232", "<div data-node-id=\"20241104224414-zw2bqmr\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20241104224424\"><div contenteditable=\"true\" spellcheck=\"false\">[<wbr><span data-type=\"strong\">foo</span></div><div class=\"protyle-attr\" contenteditable=\"false\">​</div></div>", "<div data-node-id=\"20241104224414-zw2bqmr\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20241104224424\"><div contenteditable=\"true\" spellcheck=\"false\">[<wbr><span data-type=\"strong\">foo</span></div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
{"231", "<div data-node-id=\"20241103085149-shu8xzh\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20241103085157\"><div contenteditable=\"true\" spellcheck=\"false\">:\n1<wbr></div><div class=\"protyle-attr\" contenteditable=\"false\">​</div></div>", "<div data-node-id=\"20241103085149-shu8xzh\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20241103085157\"><div contenteditable=\"true\" spellcheck=\"false\">:\n1<wbr></div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
{"230", "<div data-node-id=\"20240929215806-kilt4j6\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20240929215857\"><div contenteditable=\"true\" spellcheck=\"false\">​<span data-type=\"tag\">​Android</span>​<wbr><span data-type=\"tag\">​Linux</span>​</div><div class=\"protyle-attr\" contenteditable=\"false\">​</div></div>", "<div data-node-id=\"20240929215806-kilt4j6\" data-node-index=\"1\" data-type=\"NodeParagraph\" class=\"p\" updated=\"20240929215857\"><div contenteditable=\"true\" spellcheck=\"false\">\u200b<span data-type=\"tag\">\u200bAndroid<wbr>Linux</span>\u200b</div><div class=\"protyle-attr\" contenteditable=\"false\">\u200b</div></div>"},
Expand Down

0 comments on commit a4e60c7

Please sign in to comment.