html_to_editorjs

package module
v0.0.0-...-63173ed Latest Latest
Warning

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

Go to latest
Published: May 16, 2024 License: BSD-3-Clause Imports: 5 Imported by: 0

README

Simple converter from html to json for editor.js

Installation

go get github.com/ParkerJessica/html-to-editorjs

Usage

package main

import (
	"encoding/json"
    "fmt"
    "github.com/ParkerJessica/html-to-editorjs"
)

func main() {
	htmlString := `
        <h2>Editor.js</h2>
        <p>Hey. Meet the new Editor. On this page you can see it in action — try to edit this text.</p>
        <h3>Key features</h3>
        <ul>
            <li>It is a block-styled editor</li>
            <li>It returns clean data output in JSON</li>
            <li>Designed to be extendable and pluggable with a simple API</li>
        </ul>
	`

	html_to_editorjs.RegistryAll()
	j, _ := json.MarshalIndent(html_to_editorjs.Parse(html), "", "   ")
	fmt.Println(string(j))
}

It will generate the following output:

{
   "time": 1641162829,
   "blocks": [
      {
         "type": "header",
         "data": {
            "level": 2,
            "text": "Editor.js"
         }
      },
      {
         "type": "paragraph",
         "data": {
            "text": "Hey. Meet the new Editor. On this page you can see it in action — try to edit this text."
         }
      },
      {
         "type": "header",
         "data": {
            "level": 3,
            "text": "Key features"
         }
      },
      {
         "type": "list",
         "data": {
            "items": [
               "It is a block-styled editor",
               "It returns clean data output in JSON",
               "Designed to be extendable and pluggable with a simple API"
            ],
            "style": "unordered"
         }
      }
   ],
   "version": ""
}

Adding new handlers

package main

import (
	"fmt"
	"github.com/ParkerJessica/html-to-editorjs"
	"github.com/ParkerJessica/html-to-editorjs/scheme"
	"github.com/PuerkitoBio/goquery"
)


func main() {
	html := `
		<pre>My code block</pre>
	`
	html_to_editorjs.RegistryBlock("pre", CodeHandler)
	fmt.Println(html_to_editorjs.Parse(html))
}

func CodeHandler(selection *goquery.Selection) *scheme.Block {
	html, _ := selection.Unwrap().Html()
	if html != "" {
		return &scheme.Block{
			Type: "code",
			Data: scheme.BlockData{
				"code": html,
			},
		}
	}

	return nil
}

Supported handlers

  • blocks.Paragraph - tag p
  • blocks.Header - tags h1, h2, h3, h4, h5
  • blocks.List - tags ul, ol
  • blocks.Image - tags img, figure
  • blocks.Quote - tags figure, blockquote
  • blocks.Delimiter - tag hr
  • blocks.Table - tag table

If you need to add a handler to another tag, for example to div:

html_to_editorjs.RegistryBlock("div", blocks.Image)

License

The BSD 3-Clause license, the same as the Go language.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClearAllBlocks

func ClearAllBlocks()

ClearAllBlocks removes all block handlers

func Parse

func Parse(payload string) scheme.Response

Parse - converts html to scheme.Response.

func RegistryAll

func RegistryAll()

RegistryAll adds all system block handlers to the parser

func RegistryBlock

func RegistryBlock(name string, handler scheme.BlockHandler)

RegistryBlock adds new handlers for blocks

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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