Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
skunkie committed Feb 29, 2024
0 parents commit 1901987
Show file tree
Hide file tree
Showing 22 changed files with 1,007 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 skunkie

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
goldmark-tg
=========================

goldmark-tg is a collection of extensions for the [goldmark](http://github.com/yuin/goldmark)
that adds Telegram Markdown and MarkdownV2 functionalities.

### Examples

Markdown:

```go
package main

import (
"github.com/yuin/goldmark"

tg "github.com/skunkie/goldmark-tg"
)

func main() {
md := goldmark.New(tg.Markdown()...)
...
}
```

MarkdownV2:

```go
package main

import (
"github.com/yuin/goldmark"

tg "github.com/skunkie/goldmark-tg"
)

func main() {
md := goldmark.New(tg.MarkdownV2()...)
...
}
```

MarkdownV2 with Go Template extension:

```go
package main

import (
"fmt"
"strings"

"github.com/yuin/goldmark"

tg "github.com/skunkie/goldmark-tg"
"github.com/skunkie/goldmark-tg/extension"
)

var template = []byte(`*{{ if eq .Status "firing" }}2{{ else }}0{{ end }}*`)

func main() {
var buf strings.Builder
md := goldmark.New(append(tg.MarkdownV2(), goldmark.WithExtensions(extension.Template))...)
if err := md.Convert(template, &buf); err != nil {
panic(err)
}
fmt.Printf("%q\n", buf.String())
}
```
11 changes: 11 additions & 0 deletions _test/markdown.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
*bold text*
_italic text_
[inline URL](http://www.example.com/)
[inline mention of a user](tg://user?id=123456789)
`inline fixed-width code`
```
pre-formatted fixed-width code block
```
```python
pre-formatted fixed-width code block written in the Python programming language
```
19 changes: 19 additions & 0 deletions _test/markdownv2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
*bold \*text*
_italic \*text_
__underline__
~strikethrough~
||spoiler||
*bold _italic bold ~italic bold strikethrough ||italic bold strikethrough spoiler||~ __underline italic bold___ bold*
[inline URL](http://www.example.com/)
[inline mention of a user](tg://user?id=123456789)
![👍](tg://emoji?id=5368324170671202286)
`inline fixed-width code`
```
pre-formatted fixed-width code block
```
```python
pre-formatted fixed-width code block written in the Python programming language
```
>Block quotation started
>Block quotation continued
>The last line of the block quotation
4 changes: 4 additions & 0 deletions _test/markdownv2_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*{{ if eq .Status "firing" }}2{{ else }}0{{ end }}*
_T1_ __{{ __T2__
||"T3"|| }}__
}} ||"T4"|| {{
22 changes: 22 additions & 0 deletions examples/template/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"fmt"
"strings"

"github.com/yuin/goldmark"

tg "github.com/skunkie/goldmark-tg"
"github.com/skunkie/goldmark-tg/extension"
)

var template = []byte(`*{{ if eq .Status "firing" }}2{{ else }}0{{ end }}*`)

func main() {
var buf strings.Builder
md := goldmark.New(append(tg.MarkdownV2(), goldmark.WithExtensions(extension.Template))...)
if err := md.Convert(template, &buf); err != nil {
panic(err)
}
fmt.Printf("%q\n", buf.String()) //nolint:forbidigo
}
31 changes: 31 additions & 0 deletions extension/ast/emphasis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ast

import (
gast "github.com/yuin/goldmark/ast"
)

// A Emphasis struct represents a emphasis of Telegram Markdown text.
type Emphasis struct {
gast.BaseInline

Delimiter byte
Level int
}

// Dump implements Node.Dump.
func (n *Emphasis) Dump(source []byte, level int) {
gast.DumpHelper(n, source, level, nil, nil)
}

// KindEmphasis is a NodeKind of the Emphasis node.
var KindEmphasis = gast.NewNodeKind("Emphasis")

// Kind implements Node.Kind.
func (n *Emphasis) Kind() gast.NodeKind {
return KindEmphasis
}

// NewEmphasis returns a new Emphasis node.
func NewEmphasis(delimiter byte, level int) *Emphasis {
return &Emphasis{Delimiter: delimiter, Level: level}
}
28 changes: 28 additions & 0 deletions extension/ast/spoiler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ast

import (
gast "github.com/yuin/goldmark/ast"
)

// A Spoiler struct represents a spoiler of Telegram MarkdownV2 text.
type Spoiler struct {
gast.BaseInline
}

// Dump implements Node.Dump.
func (n *Spoiler) Dump(source []byte, level int) {
gast.DumpHelper(n, source, level, nil, nil)
}

// KindSpoiler is a NodeKind of the Spoiler node.
var KindSpoiler = gast.NewNodeKind("Spoiler")

// Kind implements Node.Kind.
func (n *Spoiler) Kind() gast.NodeKind {
return KindSpoiler
}

// NewSpoiler returns a new Spoiler node.
func NewSpoiler() *Spoiler {
return &Spoiler{}
}
28 changes: 28 additions & 0 deletions extension/ast/strikethrough.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package ast

import (
gast "github.com/yuin/goldmark/ast"
)

// A Strikethrough struct represents a strikethrough of Telegram MarkdownV2 text.
type Strikethrough struct {
gast.BaseInline
}

// Dump implements Node.Dump.
func (n *Strikethrough) Dump(source []byte, level int) {
gast.DumpHelper(n, source, level, nil, nil)
}

// KindStrikethrough is a NodeKind of the Strikethrough node.
var KindStrikethrough = gast.NewNodeKind("Strikethrough")

// Kind implements Node.Kind.
func (n *Strikethrough) Kind() gast.NodeKind {
return KindStrikethrough
}

// NewStrikethrough returns a new Strikethrough node.
func NewStrikethrough() *Strikethrough {
return &Strikethrough{}
}
31 changes: 31 additions & 0 deletions extension/ast/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ast

import (
gast "github.com/yuin/goldmark/ast"
textm "github.com/yuin/goldmark/text"
)

// A Template struct represents a Go template like {{ if eq .Status "firing" }}.
type Template struct {
gast.BaseInline

Segment textm.Segment
}

// Dump implements Node.Dump.
func (n *Template) Dump(source []byte, level int) {
gast.DumpHelper(n, source, level, nil, nil)
}

// KindTemplate is a NodeKind of the Template node.
var KindTemplate = gast.NewNodeKind("Template")

// Kind implements Node.Kind.
func (n *Template) Kind() gast.NodeKind {
return KindTemplate
}

// NewTemplate returns a new Template node.
func NewTemplate() *Template {
return &Template{}
}
Loading

0 comments on commit 1901987

Please sign in to comment.