mirror of
https://mau.dev/mautrix/go.git
synced 2026-03-14 22:35:52 +01:00
format/mdext: add single-character bold, italic and strikethrough parsers
This commit is contained in:
parent
83d3a0de5b
commit
ea3cd96e25
2 changed files with 172 additions and 0 deletions
96
format/mdext/shortemphasis.go
Normal file
96
format/mdext/shortemphasis.go
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
// Copyright (c) 2024 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/text"
|
||||
"github.com/yuin/goldmark/util"
|
||||
)
|
||||
|
||||
var ShortEmphasis goldmark.Extender = &shortEmphasisExtender{}
|
||||
|
||||
type shortEmphasisExtender struct{}
|
||||
|
||||
func (s *shortEmphasisExtender) Extend(m goldmark.Markdown) {
|
||||
m.Parser().AddOptions(parser.WithInlineParsers(
|
||||
util.Prioritized(&italicsParser{}, 500),
|
||||
util.Prioritized(&boldParser{}, 500),
|
||||
))
|
||||
}
|
||||
|
||||
type italicsDelimiterProcessor struct{}
|
||||
|
||||
func (p *italicsDelimiterProcessor) IsDelimiter(b byte) bool {
|
||||
return b == '_'
|
||||
}
|
||||
|
||||
func (p *italicsDelimiterProcessor) CanOpenCloser(opener, closer *parser.Delimiter) bool {
|
||||
return opener.Char == closer.Char
|
||||
}
|
||||
|
||||
func (p *italicsDelimiterProcessor) OnMatch(consumes int) ast.Node {
|
||||
return ast.NewEmphasis(1)
|
||||
}
|
||||
|
||||
var defaultItalicsDelimiterProcessor = &italicsDelimiterProcessor{}
|
||||
|
||||
type italicsParser struct{}
|
||||
|
||||
func (s *italicsParser) Trigger() []byte {
|
||||
return []byte{'_'}
|
||||
}
|
||||
|
||||
func (s *italicsParser) Parse(parent ast.Node, block text.Reader, pc parser.Context) ast.Node {
|
||||
before := block.PrecendingCharacter()
|
||||
line, segment := block.PeekLine()
|
||||
node := parser.ScanDelimiter(line, before, 1, defaultItalicsDelimiterProcessor)
|
||||
if node == nil || node.OriginalLength > 1 || before == '_' {
|
||||
return nil
|
||||
}
|
||||
node.Segment = segment.WithStop(segment.Start + node.OriginalLength)
|
||||
block.Advance(node.OriginalLength)
|
||||
pc.PushDelimiter(node)
|
||||
return node
|
||||
}
|
||||
|
||||
type boldDelimiterProcessor struct{}
|
||||
|
||||
func (p *boldDelimiterProcessor) IsDelimiter(b byte) bool {
|
||||
return b == '*'
|
||||
}
|
||||
|
||||
func (p *boldDelimiterProcessor) CanOpenCloser(opener, closer *parser.Delimiter) bool {
|
||||
return opener.Char == closer.Char
|
||||
}
|
||||
|
||||
func (p *boldDelimiterProcessor) OnMatch(consumes int) ast.Node {
|
||||
return ast.NewEmphasis(2)
|
||||
}
|
||||
|
||||
var defaultBoldDelimiterProcessor = &boldDelimiterProcessor{}
|
||||
|
||||
type boldParser struct{}
|
||||
|
||||
func (s *boldParser) Trigger() []byte {
|
||||
return []byte{'*'}
|
||||
}
|
||||
|
||||
func (s *boldParser) Parse(parent ast.Node, block text.Reader, pc parser.Context) ast.Node {
|
||||
before := block.PrecendingCharacter()
|
||||
line, segment := block.PeekLine()
|
||||
node := parser.ScanDelimiter(line, before, 1, defaultBoldDelimiterProcessor)
|
||||
if node == nil || node.OriginalLength > 1 || before == '*' {
|
||||
return nil
|
||||
}
|
||||
node.Segment = segment.WithStop(segment.Start + node.OriginalLength)
|
||||
block.Advance(node.OriginalLength)
|
||||
pc.PushDelimiter(node)
|
||||
return node
|
||||
}
|
||||
76
format/mdext/shortstrike.go
Normal file
76
format/mdext/shortstrike.go
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
// Copyright (c) 2024 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"
|
||||
gast "github.com/yuin/goldmark/ast"
|
||||
"github.com/yuin/goldmark/extension"
|
||||
"github.com/yuin/goldmark/extension/ast"
|
||||
"github.com/yuin/goldmark/parser"
|
||||
"github.com/yuin/goldmark/renderer"
|
||||
"github.com/yuin/goldmark/text"
|
||||
"github.com/yuin/goldmark/util"
|
||||
)
|
||||
|
||||
var ShortStrike goldmark.Extender = &shortStrikeExtender{length: 1}
|
||||
var LongStrike goldmark.Extender = &shortStrikeExtender{length: 2}
|
||||
|
||||
type shortStrikeExtender struct {
|
||||
length int
|
||||
}
|
||||
|
||||
func (s *shortStrikeExtender) Extend(m goldmark.Markdown) {
|
||||
m.Parser().AddOptions(parser.WithInlineParsers(
|
||||
util.Prioritized(&strikethroughParser{length: s.length}, 500),
|
||||
))
|
||||
m.Renderer().AddOptions(renderer.WithNodeRenderers(
|
||||
util.Prioritized(extension.NewStrikethroughHTMLRenderer(), 500),
|
||||
))
|
||||
}
|
||||
|
||||
type strikethroughDelimiterProcessor struct{}
|
||||
|
||||
func (p *strikethroughDelimiterProcessor) IsDelimiter(b byte) bool {
|
||||
return b == '~'
|
||||
}
|
||||
|
||||
func (p *strikethroughDelimiterProcessor) CanOpenCloser(opener, closer *parser.Delimiter) bool {
|
||||
return opener.Char == closer.Char
|
||||
}
|
||||
|
||||
func (p *strikethroughDelimiterProcessor) OnMatch(consumes int) gast.Node {
|
||||
return ast.NewStrikethrough()
|
||||
}
|
||||
|
||||
var defaultStrikethroughDelimiterProcessor = &strikethroughDelimiterProcessor{}
|
||||
|
||||
type strikethroughParser struct {
|
||||
length int
|
||||
}
|
||||
|
||||
func (s *strikethroughParser) Trigger() []byte {
|
||||
return []byte{'~'}
|
||||
}
|
||||
|
||||
func (s *strikethroughParser) Parse(parent gast.Node, block text.Reader, pc parser.Context) gast.Node {
|
||||
before := block.PrecendingCharacter()
|
||||
line, segment := block.PeekLine()
|
||||
node := parser.ScanDelimiter(line, before, 1, defaultStrikethroughDelimiterProcessor)
|
||||
if node == nil || node.OriginalLength != s.length || before == '~' {
|
||||
return nil
|
||||
}
|
||||
|
||||
node.Segment = segment.WithStop(segment.Start + node.OriginalLength)
|
||||
block.Advance(node.OriginalLength)
|
||||
pc.PushDelimiter(node)
|
||||
return node
|
||||
}
|
||||
|
||||
func (s *strikethroughParser) CloseBlock(parent gast.Node, pc parser.Context) {
|
||||
// nothing to do
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue