goldmark_telegram

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 21, 2023 License: MIT Imports: 8 Imported by: 0

README

goldmark-telegram

CI

goldmark-telegram is a renderer for goldmark that allows render (generate) a Telegram message with formatting (Entities) from a markdown document.

Usage

Extra parsing extensions:

// All extensions
goldmark.WithExtensions(gte.GTE)
// Contains:
// Strikethrough.Extend(m)
//     Example: ~~strikethrough~~
// Underline.Extend(m)
//     Example (like in Confluence): +underline+

Renderer:

goldmark.WithRenderer(gt.New())

Example:

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	gt "github.com/igor-pavlenko/goldmark-telegram"
	gte "github.com/igor-pavlenko/goldmark-telegram/extension"
	"github.com/yuin/goldmark"
)

func main() {

	md := goldmark.New(
		goldmark.WithExtensions(gte.GTE),
		goldmark.WithRenderer(gt.New()),
	)

	var buf bytes.Buffer
	source := []byte("Just simple message **寓言**. **Bold**, _italic_, +underline+, ~~strikethrough~~, `monospace`, [link](https://example.com)")

	if err := md.Convert(source, &buf); err != nil {
		panic(err)
	}

	fmt.Println(buf.String())
	/*
    Output:
    {
      "text": "Just simple message 寓言. Bold, italic, underline, strikethrough, monospace, link",
      "entities": [
        {
          "type": "bold",
          "offset": 20,
          "length": 2
        },
        {
          "type": "bold",
          "offset": 24,
          "length": 4
        },
        {
          "type": "italic",
          "offset": 30,
          "length": 6
        },
        {
          "type": "underline",
          "offset": 38,
          "length": 9
        },
        {
          "type": "strikethrough",
          "offset": 49,
          "le ngth": 13
        },
        {
          "type": "code",
          "offset": 64,
          "length": 9
        },
        {
          "type": "text_link",
          "offset": 75,
          "length": 4,
          "url": "https://example.com"
        }
      ]
    }
	*/

	msg := &gt.Message{}
	/*
	// Message is a struct that represents a Telegram text message
	type Message struct {
		Text     string   `json:"text"`
		Entities []Entity `json:"entities"`
	}

	// Entity is a struct that represents a Telegram text entity
	type Entity struct {
		Type   string `json:"type"`
		Offset int    `json:"offset"`
		Length int    `json:"length"`
		Url    string `json:"url,omitempty"`
	}
	*/
	if err := json.Unmarshal(buf.Bytes(), msg); err != nil {
		panic(err)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New() gr.Renderer

New returns a new Renderer with Telegram node renderers.

func NewNodeRenderer

func NewNodeRenderer() gr.NodeRenderer

NewNodeRenderer returns a new Renderer with given options.

func NewRenderer

func NewRenderer(options ...gr.Option) gr.Renderer

NewRenderer returns a new Renderer with given options.

Types

type Config

type Config struct {
	Writer WriterInterface
}

A Config struct has configurations for the Telegram based renderers.

func NewConfig

func NewConfig() Config

NewConfig returns a new Config with defaults.

type Entity

type Entity struct {
	Type   string `json:"type"`
	Offset int    `json:"offset"`
	Length int    `json:"length"`
	Url    string `json:"url,omitempty"`
}

Entity is a struct that represents a Telegram text entity

type Message

type Message struct {
	Text     string   `json:"text"`
	Entities []Entity `json:"entities"`
}

Message is a struct that represents a Telegram text message

type Node

type Node struct {
	Offset int
	Length int
}

Node is a struct that represents a node in the stack

type NodeRenderer

type NodeRenderer struct {
	Config
	// contains filtered or unexported fields
}

NodeRenderer A Renderer struct is an implementation of renderer.NodeRenderer that renders Telegram

func (*NodeRenderer) RegisterFuncs

func (r *NodeRenderer) RegisterFuncs(reg gr.NodeRendererFuncRegisterer)

RegisterFuncs implements NodeRenderer.RegisterFuncs .

type Stack

type Stack struct {
	// contains filtered or unexported fields
}

Stack is a basic LIFO stack that resizes as needed.

func NewStack

func NewStack() *Stack

NewStack returns a new stack.

func (*Stack) GetLength

func (s *Stack) GetLength() int

GetLength returns the length of the stack starting from the current index to the end.

func (*Stack) Pop

func (s *Stack) Pop() *Node

Pop removes and returns a node from the stack in last to first order.

func (*Stack) Push

func (s *Stack) Push(n *Node)

Push adds a node to the stack.

type WriterInterface

type WriterInterface interface {
	AddLen(l int)
	GetLen() int
	AddText(text []byte)
	GetText() []byte
	AddEntity(entity Entity)
	GetEntities() []Entity
}

A WriterInterface interface writes textual contents to a writer.

func NewWriter

func NewWriter() WriterInterface

NewWriter returns a new Writer.

Directories

Path Synopsis
ast

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL