forked from mautrix/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplespoiler.go
62 lines (51 loc) · 1.83 KB
/
simplespoiler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright (c) 2022 Tulir Asokan
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package mdext
import (
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)
type simpleSpoilerParser struct{}
var defaultSimpleSpoilerParser = &simpleSpoilerParser{}
func NewSimpleSpoilerParser() parser.InlineParser {
return defaultSimpleSpoilerParser
}
func (s *simpleSpoilerParser) Trigger() []byte {
return []byte{'|'}
}
func (s *simpleSpoilerParser) Parse(parent ast.Node, block text.Reader, pc parser.Context) ast.Node {
// This is basically copied from https://github.com/yuin/goldmark/blob/master/extension/strikethrough.go
before := block.PrecendingCharacter()
line, segment := block.PeekLine()
node := parser.ScanDelimiter(line, before, 2, defaultSpoilerDelimiterProcessor)
if node == nil {
return nil
}
node.Segment = segment.WithStop(segment.Start + node.OriginalLength)
block.Advance(node.OriginalLength)
pc.PushDelimiter(node)
return node
}
func (s *simpleSpoilerParser) CloseBlock(parent ast.Node, pc parser.Context) {
// nothing to do
}
type simpleSpoiler struct{}
// SimpleSpoiler is an extension that allow you to use simple spoiler expression like '||text||' .
//
// For spoilers with reasons ('||reason|text||'), use the Spoiler extension.
var SimpleSpoiler = &simpleSpoiler{}
func (e *simpleSpoiler) Extend(m goldmark.Markdown) {
m.Parser().AddOptions(parser.WithInlineParsers(
util.Prioritized(NewSimpleSpoilerParser(), 500),
))
m.Renderer().AddOptions(renderer.WithNodeRenderers(
util.Prioritized(NewSpoilerHTMLRenderer(), 500),
))
}