Skip to content

Commit

Permalink
✨ 支持 ==mark== 高亮语法 #84
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Aug 17, 2020
1 parent 0fc32de commit 5fe83d4
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 5 deletions.
5 changes: 5 additions & 0 deletions ast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,5 +446,10 @@ const (
NodeBlockRefSpace NodeType = 432 // 内容块 ID 和内容块文本之间的空格
NodeBlockRefText NodeType = 433 // 内容块文本

// 支持 ==mark== 高亮语法 https://github.com/88250/lute/issues/84
NodeMark NodeType = 450 // 标记
NodeMarkOpenMarker NodeType = 451 // 开始标记标记符 ==
NodeMarkCloseMarker NodeType = 452 // 结束标记标记符 ==

NodeTypeMaxVal NodeType = 1024 // 节点类型最大值
)
10 changes: 8 additions & 2 deletions ast/nodetype_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions lute.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func NewOptions() *parse.Options {
ChineseParagraphBeginningSpace: false,
YamlFrontMatter: true,
BlockRef: false,
Mark: false,
}
}

Expand Down Expand Up @@ -324,6 +325,10 @@ func (lute *Lute) SetBlockRef(b bool) {
lute.BlockRef = b
}

func (lute *Lute) SetMark(b bool) {
lute.Mark = b
}

func (lute *Lute) SetJSRenderers(options map[string]map[string]*js.Object) {
for rendererType, extRenderer := range options["renderers"] {
switch extRenderer.Interface().(type) { // 稍微进行一点格式校验
Expand Down
10 changes: 9 additions & 1 deletion parse/delimiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ func (t *Tree) processEmphasis(stackBottom *delimiter, ctx *InlineContext) {
openerInl = opener.node
closerInl = closer.node

if t.Context.Option.GFMStrikethrough && lex.ItemTilde == closercc && opener.num != closer.num {
if ((t.Context.Option.GFMStrikethrough && lex.ItemTilde == closercc) ||
t.Context.Option.Mark && lex.ItemEqual == closercc) &&
opener.num != closer.num {
break
}

Expand Down Expand Up @@ -171,6 +173,12 @@ func (t *Tree) processEmphasis(stackBottom *delimiter, ctx *InlineContext) {
openMarker.Type = ast.NodeStrikethrough2OpenMarker
closeMarker.Type = ast.NodeStrikethrough2CloseMarker
}
} else if lex.ItemEqual == closercc {
if t.Context.Option.Mark {
emStrongDel.Type = ast.NodeMark
openMarker.Type = ast.NodeMarkOpenMarker
closeMarker.Type = ast.NodeMarkCloseMarker
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion parse/inline.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (t *Tree) parseInline(block *ast.Node, ctx *InlineContext) {
n = t.parseBackslash(block, ctx)
case lex.ItemBacktick:
n = t.parseCodeSpan(block, ctx)
case lex.ItemAsterisk, lex.ItemUnderscore, lex.ItemTilde:
case lex.ItemAsterisk, lex.ItemUnderscore, lex.ItemTilde, lex.ItemEqual:
t.handleDelim(block, ctx)
case lex.ItemNewline:
n = t.parseNewline(block, ctx)
Expand Down
2 changes: 2 additions & 0 deletions parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ type Options struct {
YamlFrontMatter bool
// BlockRef 设置是否开启内容块引用支持。
BlockRef bool
// Mark 设置是否打开“==标记==”支持。
Mark bool
}

func (context *Context) ParentTip() {
Expand Down
2 changes: 1 addition & 1 deletion parse/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (t *Tree) parseText(ctx *InlineContext) *ast.Node {
func (t *Tree) isMarker(token byte) bool {
switch token {
case lex.ItemAsterisk, lex.ItemUnderscore, lex.ItemOpenBracket, lex.ItemBang, lex.ItemNewline, lex.ItemBackslash, lex.ItemBacktick, lex.ItemLess,
lex.ItemCloseBracket, lex.ItemAmpersand, lex.ItemTilde, lex.ItemDollar, lex.ItemOpenCurlyBrace, lex.ItemOpenParen:
lex.ItemCloseBracket, lex.ItemAmpersand, lex.ItemTilde, lex.ItemDollar, lex.ItemOpenCurlyBrace, lex.ItemOpenParen, lex.ItemEqual:
return true
default:
return false
Expand Down
2 changes: 2 additions & 0 deletions pprof/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func main() {
luteEngine.FixTermTypo = false
luteEngine.ChinesePunct = false
luteEngine.Emoji = false
luteEngine.BlockRef = false
luteEngine.Mark = false

cpuProfile, _ := os.Create("pprof/cpu_profile")
pprof.StartCPUProfile(cpuProfile)
Expand Down
33 changes: 33 additions & 0 deletions test/mark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Lute - 一款对中文语境优化的 Markdown 引擎,支持 Go 和 JavaScript
// Copyright (c) 2019-present, b3log.org
//
// Lute is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
// http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.

package test

import (
"github.com/88250/lute"
"testing"
)

var markTests = []parseTest{

{"0", "==foo==\n", "<h2 id=\"foo\">foo</h2>\n"},
}

func TestMark(t *testing.T) {
luteEngine := lute.New()
luteEngine.Mark = true

for _, test := range markTests {
html := luteEngine.MarkdownStr(test.name, test.from)
if test.to != html {
t.Fatalf("test case [%s] failed\nexpected\n\t%q\ngot\n\t%q\noriginal markdown text\n\t%q", test.name, test.to, html, test.from)
}
}
}

0 comments on commit 5fe83d4

Please sign in to comment.