goeditorjs

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2020 License: MIT Imports: 6 Imported by: 2

README

davidscottmills/goeditorjs

An extensible library that converts editor.js data into HTML or markdown.

test and build Coverage Status Go Report Card Documentation GitHub issues license GitHub go.mod Go version of a Go module

Installation

go get github.com/davidscottmills/goeditorjs

Usage

package main

import (
	"io/ioutil"
	"log"

	"github.com/davidscottmills/goeditorjs"
)

func main() {
	content, err := ioutil.ReadFile("editorjs_output.json")
	if err != nil {
		log.Fatal(err)
	}

	ejs := string(content)

    // HTML
    // Get the HTML engine
    htmlEngine := goeditorjs.NewHTMLEngine()
    // Register the handlers you wish to use
	htmlEngine.RegisterBlockHandlers(
		&goeditorjs.HeaderHandler{},
		&goeditorjs.BlockParagraphHandler{},
		&goeditorjs.BlockListHandler{},
		&goeditorjs.BlockCodeBoxHandler{},
	)
    // Generate the html
	html, err := htmlEngine.GenerateHTML(ejs)
	if err != nil {
		log.Fatal(err)
    }

    // Do something with the html output. In this case, write it to a file.
	err = ioutil.WriteFile("editorjs.html", []byte(html), 0644)
	if err != nil {
		log.Fatal(err)
	}

    // Generate markdown and save it to a file
    // Get the markdown engine
	markdownEngine := goeditorjs.NewMarkdownEngine()
    // Register the handlers you wish to use
	markdownEngine.RegisterBlockHandlers(
		&goeditorjs.HeaderHandler{},
		&goeditorjs.BlockParagraphHandler{},
		&goeditorjs.BlockListHandler{},
		&goeditorjs.BlockCodeBoxHandler{},
    )
    // Generate the markdown
	md, err := markdownEngine.GenerateMarkdown(ejs)
	if err != nil {
		log.Fatal(err)
	}

    // Do something with the md output. In this case, write it to a file.
	err = ioutil.WriteFile("editorjs.md", []byte(md), 0644)
	if err != nil {
		log.Fatal(err)
	}
}

Using a Custom Handler

You can create and use your own handler in either engine by implementing the required interface and registering it. This package provides two interfaces for handlers.

  • HTMLBlockHandler

    type MarkdownBlockHandler interface {
        Type() string // Type returns the type the block handler supports as a string
        GenerateHTML(editorJSBlock EditorJSBlock) (string, error) // Return associated HTML
    }
    
  • MarkdownBlockHandler

    type MarkdownBlockHandler interface {
        Type() string // Type returns the type the block handler supports as a string
        GenerateMarkdown(editorJSBlock EditorJSBlock) (string, error) // Return associated markdown
    }
    

If you're only planning to use the HTMLEngine, then you only need to implement the HTMLBlockHandler interface. The same goes for markdown.

Once you've met the required interface, register the handler for use in the engine.

htmlEngine := goeditorjs.NewHTMLEngine()
// Register the handlers you wish to use
htmlEngine.RegisterBlockHandlers(
    &MyCustomBlockHandler{},
)

Below is an example of how the header handle is implemented.

package header

import (
	"encoding/json"
	"fmt"
)

// HeaderHandler is the default HeaderHandler for EditorJS HTML generation
type HeaderHandler struct {
    // Notice that you could put some configurable options in this struct and then use them in your handler
}

// Header represents header data from EditorJS
type Header struct {
	Text  string `json:"text"`
	Level int    `json:"level"`
}

func (*HeaderHandler) parse(editorJSBlock EditorJSBlock) (*Header, error) {
	header := &Header{}
	return header, json.Unmarshal(editorJSBlock.Data, header)
}

// Type "header"
func (*HeaderHandler) Type() string {
	return "header"
}

// GenerateHTML generates html for HeaderBlocks
func (h *HeaderHandler) GenerateHTML(editorJSBlock EditorJSBlock) (string, error) {
	header, err := h.parse(editorJSBlock)
	if err != nil {
		return "", err
	}

	return fmt.Sprintf("<h%d>%s</h%d>", header.Level, header.Text, header.Level), nil
}

// GenerateMarkdown generates markdown for HeaderBlocks
func (h *HeaderHandler) GenerateMarkdown(editorJSBlock EditorJSBlock) (string, error) {
	header, err := h.parse(editorJSBlock)
	if err != nil {
		return "", err
	}

	return fmt.Sprintf("%s %s", strings.Repeat("#", header.Level), header.Text), nil
}

TODO

  • Provide more handlers (image, table, etc.)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultImageHandlerOptions = &ImageHandlerOptions{
	StretchClass:    "image-tool--stretched",
	BorderClass:     "image-tool--withBorder",
	BackgroundClass: "image-tool--withBackground"}

DefaultImageHandlerOptions are the default options available to the ImageHandler

View Source
var (
	//ErrBlockHandlerNotFound is returned from GenerateHTML when the HTML engine doesn't have a registered handler
	//for that type and the HTMLEngine is set to return on errors.
	ErrBlockHandlerNotFound = errors.New("Handler not found for block type")
)

Functions

This section is empty.

Types

type CodeBoxHandler

type CodeBoxHandler struct{}

CodeBoxHandler is the default CodeBoxHandler for EditorJS HTML generation

func (*CodeBoxHandler) GenerateHTML

func (h *CodeBoxHandler) GenerateHTML(editorJSBlock EditorJSBlock) (string, error)

GenerateHTML generates html for CodeBoxBlocks

func (*CodeBoxHandler) GenerateMarkdown

func (h *CodeBoxHandler) GenerateMarkdown(editorJSBlock EditorJSBlock) (string, error)

GenerateMarkdown generates markdown for CodeBoxBlocks

func (*CodeBoxHandler) Type

func (*CodeBoxHandler) Type() string

Type "codeBox"

type EditorJSBlock

type EditorJSBlock struct {
	Type string `json:"type"`
	// Data is the Data for an editorJS block in the form of RawMessage ([]byte). It is left up to the Handler to parse the Data field
	Data json.RawMessage `json:"data"`
}

EditorJSBlock type

type HTMLBlockHandler

type HTMLBlockHandler interface {
	Type() string // Type returns the type the block handler supports as a string
	GenerateHTML(editorJSBlock EditorJSBlock) (string, error)
}

HTMLBlockHandler is an interface for a plugable EditorJS HTML generator

type HTMLEngine

type HTMLEngine struct {
	BlockHandlers map[string]HTMLBlockHandler
}

HTMLEngine is the engine that creates the HTML from EditorJS blocks

func NewHTMLEngine

func NewHTMLEngine() *HTMLEngine

NewHTMLEngine creates a new HTMLEngine

func (*HTMLEngine) GenerateHTML

func (htmlEngine *HTMLEngine) GenerateHTML(editorJSData string) (string, error)

GenerateHTML generates html from the editorJS using configured set of HTML handlers

func (*HTMLEngine) RegisterBlockHandlers

func (htmlEngine *HTMLEngine) RegisterBlockHandlers(handlers ...HTMLBlockHandler)

RegisterBlockHandlers registers or overrides a block handlers for blockType given by HTMLBlockHandler.Type()

type HeaderHandler

type HeaderHandler struct{}

HeaderHandler is the default HeaderHandler for EditorJS HTML generation

func (*HeaderHandler) GenerateHTML

func (h *HeaderHandler) GenerateHTML(editorJSBlock EditorJSBlock) (string, error)

GenerateHTML generates html for HeaderBlocks

func (*HeaderHandler) GenerateMarkdown

func (h *HeaderHandler) GenerateMarkdown(editorJSBlock EditorJSBlock) (string, error)

GenerateMarkdown generates markdown for HeaderBlocks

func (*HeaderHandler) Type

func (*HeaderHandler) Type() string

Type "header"

type ImageHandler

type ImageHandler struct {
	// Options are made available to the GenerateHTML and GenerateMarkdown functions.
	// If not provided, DefaultImageHandlerOptions will be used.
	Options *ImageHandlerOptions
}

ImageHandler is the default ImageHandler for EditorJS HTML generation

func (*ImageHandler) GenerateHTML

func (h *ImageHandler) GenerateHTML(editorJSBlock EditorJSBlock) (string, error)

GenerateHTML generates html for ImageBlocks

func (*ImageHandler) GenerateMarkdown

func (h *ImageHandler) GenerateMarkdown(editorJSBlock EditorJSBlock) (string, error)

GenerateMarkdown generates markdown for ImageBlocks

func (*ImageHandler) Type

func (*ImageHandler) Type() string

Type "image"

type ImageHandlerOptions

type ImageHandlerOptions struct {
	BorderClass     string
	StretchClass    string
	BackgroundClass string
}

ImageHandlerOptions are the options available to the ImageHandler

type ListHandler

type ListHandler struct{}

ListHandler is the default ListHandler for EditorJS HTML generation

func (*ListHandler) GenerateHTML

func (h *ListHandler) GenerateHTML(editorJSBlock EditorJSBlock) (string, error)

GenerateHTML generates html for ListBlocks

func (*ListHandler) GenerateMarkdown

func (h *ListHandler) GenerateMarkdown(editorJSBlock EditorJSBlock) (string, error)

GenerateMarkdown generates markdown for ListBlocks

func (*ListHandler) Type

func (*ListHandler) Type() string

Type "list"

type MarkdownBlockHandler

type MarkdownBlockHandler interface {
	Type() string // Type returns the type the block handler supports as a string
	GenerateMarkdown(editorJSBlock EditorJSBlock) (string, error)
}

MarkdownBlockHandler is an interface for a plugable EditorJS HTML generator

type MarkdownEngine

type MarkdownEngine struct {
	BlockHandlers map[string]MarkdownBlockHandler
}

MarkdownEngine is the engine that creates the HTML from EditorJS blocks

func NewMarkdownEngine

func NewMarkdownEngine() *MarkdownEngine

NewMarkdownEngine creates a new MarkdownEngine

func (*MarkdownEngine) GenerateMarkdown

func (markdownEngine *MarkdownEngine) GenerateMarkdown(editorJSData string) (string, error)

GenerateMarkdown generates markdown from the editorJS using configured set of markdown handlers

func (*MarkdownEngine) RegisterBlockHandlers

func (markdownEngine *MarkdownEngine) RegisterBlockHandlers(handlers ...MarkdownBlockHandler)

RegisterBlockHandlers registers or overrides a block handlers for blockType given by MarkdownBlockHandler.Type()

type ParagraphHandler

type ParagraphHandler struct{}

ParagraphHandler is the default ParagraphHandler for EditorJS HTML generation

func (*ParagraphHandler) GenerateHTML

func (h *ParagraphHandler) GenerateHTML(editorJSBlock EditorJSBlock) (string, error)

GenerateHTML generates html for ParagraphBlocks

func (*ParagraphHandler) GenerateMarkdown

func (h *ParagraphHandler) GenerateMarkdown(editorJSBlock EditorJSBlock) (string, error)

GenerateMarkdown generates markdown for ParagraphBlocks

func (*ParagraphHandler) Type

func (*ParagraphHandler) Type() string

Type "paragraph"

type RawHTMLHandler

type RawHTMLHandler struct{}

RawHTMLHandler is the default raw handler for EditorJS HTML generation

func (*RawHTMLHandler) GenerateHTML

func (h *RawHTMLHandler) GenerateHTML(editorJSBlock EditorJSBlock) (string, error)

GenerateHTML generates html for rawBlocks

func (*RawHTMLHandler) GenerateMarkdown

func (h *RawHTMLHandler) GenerateMarkdown(editorJSBlock EditorJSBlock) (string, error)

GenerateMarkdown generates markdown for rawBlocks

func (*RawHTMLHandler) Type

func (*RawHTMLHandler) Type() string

Type "raw"

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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