wordpress

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertBlocks

func ConvertBlocks(wpContent string, imageMap map[string]string, featuredImageURL string) (string, error)

ConvertBlocks converts WordPress block-editor HTML into a TipTap JSON document string. imageMap remaps WordPress image URLs to local media URLs; entries not present in the map keep their original URL. If featuredImageURL is non-empty, it is prepended as the first image node in the document. Unsupported blocks degrade to a plain paragraph so no content is silently lost.

func ExtractImageURLs

func ExtractImageURLs(wpContent string) []string

ExtractImageURLs scans WordPress block HTML and returns every image source URL found (img src, plus cover/gallery background URLs in block attrs). The importer uses this to know which media to download before conversion.

Types

type ImportResult

type ImportResult struct {
	Imported      int      `json:"imported"`
	Skipped       int      `json:"skipped"`
	UsersImported int      `json:"usersImported"`
	Errors        []string `json:"errors,omitempty"`
}

ImportResult summarizes the outcome of an import run.

type Importer

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

Importer orchestrates a WordPress import: parse the WXR, download images, convert each item to TipTap JSON, and create it via the content service.

func NewImporter

func NewImporter(
	contentService contentCreator,
	downloader *MediaDownloader,
	resolver userResolver,
	postTypes postTypeLister,
	language string,
	logger *util.Logger,
) *Importer

NewImporter creates an importer.

func (*Importer) Import

func (imp *Importer) Import(ctx context.Context, wxrData io.Reader, userID int) (*ImportResult, error)

Import reads a WXR stream and imports every item whose post type is registered in the post-type service. Image download failures are logged and recorded but never abort the run; the original WordPress URL is kept as a fallback. Authors are auto-created as Contributor users and their posts are assigned to them; authors that cannot be resolved fall back to the importing admin. Returns an aggregated result.

type MediaDownloader

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

MediaDownloader downloads images from a WordPress site and re-uploads them through the media service (which converts to WebP, deduplicates by hash, and generates thumbnails). Results are cached per URL so each image is fetched at most once per import.

func NewMediaDownloader

func NewMediaDownloader(httpClient *http.Client, mediaService mediaService) *MediaDownloader

NewMediaDownloader creates a downloader. If httpClient is nil a default client with a 30-second timeout is used.

func (*MediaDownloader) DownloadAndUpload

func (d *MediaDownloader) DownloadAndUpload(ctx context.Context, imageURL string, userID int) (string, error)

DownloadAndUpload fetches the image at imageURL and re-uploads it to local storage. Returns the local media URL. Non-image URLs and failed downloads return an empty string and nil error so the caller can fall back to the original WordPress URL without aborting the import.

type ParsedAuthor added in v0.5.0

type ParsedAuthor struct {
	Login       string
	Email       string
	DisplayName string
}

ParsedAuthor is a normalized WordPress author ready for user resolution.

type ParsedItem

type ParsedItem struct {
	Title    string
	Content  string
	Slug     string
	Status   string // mapped to "published" or "draft"
	PostType string
	Tags     []string
	PubDate  string
	Creator  string // WordPress author login (<dc:creator>)
	Meta     map[string]string
}

ParsedItem is a normalized WordPress item ready for conversion.

type UserResolver added in v0.5.0

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

UserResolver resolves WordPress authors to Lesstruct user IDs. If a user already exists (by username or email) it is reused; otherwise a new Contributor with a random password and verified status is created.

func NewUserResolver added in v0.5.0

func NewUserResolver(repo userResolverRepo, logger *util.Logger) *UserResolver

NewUserResolver creates a UserResolver.

func (*UserResolver) ResolveOrCreate added in v0.5.0

func (r *UserResolver) ResolveOrCreate(
	ctx context.Context,
	login,
	email,
	displayName string,
) (int, bool, error)

ResolveOrCreate returns the Lesstruct userID for the given WordPress author. The created flag is true when a new user was created.

type WXRDocument

type WXRDocument struct {
	SiteTitle   string
	SiteURL     string
	Authors     []ParsedAuthor
	Items       []ParsedItem
	Attachments map[int]string // attachment post ID → wp:attachment_url
}

WXRDocument is the parsed, normalized representation of a WXR export. Only items whose post type is in the caller-supplied allowlist are retained; other post types (attachment, wp_navigation, wp_global_styles, revisions) are filtered out during parsing. Attachments are captured as a lookup table (attachment post ID → media URL) so that featured images can be resolved by downstream consumers.

func Parse

func Parse(r io.Reader, allowedTypes map[string]bool) (*WXRDocument, error)

Parse reads a WordPress eXtended RSS (WXR) stream and returns a normalized document containing only items whose post type is in allowedTypes. Statuses are mapped to the Lesstruct vocabulary ("publish" → "published", everything else → "draft"). Tags are collected from item-level category elements with domain "post_tag" or "category". Custom field values (<wp:postmeta>) are collected into each item's Meta map.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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