Help us shape the future of Go! Take the 2026 Go Developer Survey

gutenberg

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Overview

Package gutenberg parses WordPress block markup (<!-- wp:ns/name {json} --> … <!-- /wp:ns/name -->) into a tree and converts it to clean HTML for GoRun (spec §4.7). Rendered content (no block comments, as returned by the public REST API) goes through CleanHTML instead.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Autop

func Autop(s string) string

Autop wraps classic-editor content (no block markup, no block-level tags) in paragraphs split on blank lines, with single newlines as <br>, like WordPress's wpautop for the simple case.

func CleanHTML

func CleanHTML(s string, o Options) string

CleanHTML strips WordPress block classes and inline styles (unless o.KeepClasses) from rendered HTML, drops empty class attributes and collapses runs of blank lines. Text, comments and end tags are kept byte for byte.

func HasBlocks

func HasBlocks(raw string) bool

HasBlocks reports whether raw contains block delimiters.

func Shortcodes

func Shortcodes(s string) map[string]int

Shortcodes counts shortcode tags ([tag ...]) in s; closing tags are not counted. Tags must look like WordPress shortcode names (lowercase, starting with a letter), so text such as [PDF] or [1] is ignored.

Types

type Block

type Block struct {
	Name  string
	Attrs map[string]any
	Void  bool
	Inner []*Block
	Parts []Part
}

Block is one parsed block. Name is "core/paragraph" style ("" for freeform HTML between blocks). Parts interleave the block's own HTML with its inner blocks, in document order.

func Parse

func Parse(raw string) []*Block

Parse builds the block tree. Mismatched closers are ignored and unclosed blocks are closed at the end of the input, like WordPress's parser.

func (*Block) HTML

func (b *Block) HTML() string

HTML returns the block's own markup without its inner blocks.

type Options

type Options struct {
	// KeepClasses keeps WordPress classes (wp-block-*, has-*, is-*, …) and
	// inline styles. Note that content.Sanitize strips non-editor classes and
	// all styles on save anyway; the option matters for raw exports.
	KeepClasses bool
}

Options tune conversion.

type Part

type Part struct {
	HTML  string
	Child *Block
}

Part is either a chunk of HTML or an inner block.

type Result

type Result struct {
	HTML string
	// Unknown counts blocks without a core converter (inner HTML kept).
	Unknown map[string]int
	// Shortcodes counts shortcode tags left in place.
	Shortcodes map[string]int
}

Result is a conversion outcome.

func Convert

func Convert(blocks []*Block, o Options) Result

Convert renders blocks to clean HTML.

func ConvertRaw

func ConvertRaw(raw string, o Options) Result

ConvertRaw converts raw post content: block markup when present, otherwise it is treated as classic/rendered HTML and cleaned.

func (Result) UnknownNames

func (r Result) UnknownNames() []string

UnknownNames returns the unknown block names sorted.

Jump to

Keyboard shortcuts

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