Git Product home page Git Product logo

html-to-editorjs's Introduction

Simple converter from html to json for editor.js

Installation

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

Usage

package main

import (
	"encoding/json"
    "fmt"
    "github.com/LegGnom/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/LegGnom/html-to-editorjs"
	"github.com/LegGnom/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.

html-to-editorjs's People

Contributors

leggnom avatar

Stargazers

Zhu Xiaopeng avatar

Watchers

 avatar

Forkers

parkerjessica

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.