ghosttohugo

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2024 License: AGPL-3.0 Imports: 11 Imported by: 1

Documentation

Index

Constants

View Source
const (
	DefaultRawShortcodeStart = "{{< rawhtml >}}"
	DefaultRawShortcodeEnd   = "{{</ rawhtml >}}"
	DefaultTemplate          = `` /* 289-byte string literal not displayed */

)

Default values used in the config if not set.

View Source
const (
	DefaultFrontMatterTitle = "title"
	DefaultFrontMatterDate  = "date"
	DefaultFrontMatterSlug  = "slug"
	DefaultFrontMatterDraft = "draft"
)

Default value that is used for front matter in lieu of a user-configured value.

View Source
const GhostPostStatusDraft = "draft"
View Source
const QUERY_POSTS_FIELDS = `` /* 766-byte string literal not displayed */

All of the fields that map to GhostPost.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Connection string for the mysql database.
	MySQLConnectionString string `json:"mysqlConnectionString"`
	// Values to use for the front matter.
	FrontMatter FrontMatterConfig `json:"frontMatter"`
	// Your theme's shortcode that starts the output of raw html, such as:
	// 	"{{< rawhtml >}}"
	RawShortcodeStart string `json:"rawShortcodeStart"`
	// Your theme's shortcode that ends the output of raw html, such as:
	// 	"{{</ rawHtml >}}"
	RawShortcodeEnd string `json:"rawShortcodeEnd"`
	// Path to save rendered markdown files to.
	OutputPath string `json:"outputPath"`
	// All occurrences of __GHOST_URL__ will be replaced with this string - this
	// is required in order to make images work, as well as other things.
	GhostURL string `json:"ghostUrl"`
	// Values are typically "post": true or "page": true.
	PostTypes map[string]bool `json:"postTypes"`
	// Values are typically "published": true, "draft": false.
	// Depending on your setup, you may set "sent": false.
	PostStatuses map[string]bool `json:"postStatuses"`
	// Values are typically "public": true.
	PostVisibilities map[string]bool `json:"postVisibilities"`
	// If true, empty (null) posts will cause the program to halt.
	ForbidEmptyPosts bool `json:"forbidEmptyPosts"`
	// If true, posts without publication dates with be set to now.
	SetUnpublishedToNow bool `json:"setUnpublishedToNow"`
	// If true, all posts will not be marked as drafts.
	PublishDrafts bool `json:"publishDrafts"`
	// A mapping of strings to replace within <a href=""> tags. This will only
	// be done if ReplaceLinks is set to true (which will under normal
	// circumstances be determined if a non-zero length map is provided for
	// LinkReplacements, but if you are doing something abnormal, you should set
	// ReplaceLinks to true manually.)
	LinkReplacements map[string]string `json:"linkReplacements"`
	// The template that will be rendered.
	//
	// The front matter will be placed at the top of every page. Usage looks
	// like this:
	//
	//  `
	//  ---
	//  {{ .FrontMatterConfig.Title }}: {{ .Post.Title }}
	//  {{ .FrontMatterConfig.Date }}: "{{ .Post.PublishedAt }}"
	//  {{ .FrontMatterConfig.Draft }}: {{ .Post.IsDraft }}
	//  customProperty: customValue # etc.
	//  ---
	//
	//  {{ .PostHtml }}
	//  `
	Template string `json:"template"`

	// If true, <a href=""> tags will have each key replaced with its respective
	// value. This is useful for swapping things like http://example.com with
	// https://nojs.example.com.
	//
	// Typically this gets set to true if using the
	// expected workflow for this program, but if you are doing something
	// out of the ordinary, please set this to true and then use the
	// LinkReplacements map to define link replacements.
	ReplaceLinks bool
	// contains filtered or unexported fields
}

func LoadConfig

func LoadConfig(f string) (Config, error)

LoadConfig reads from file f and applies sensible defaults to values not specifically set by the user.

func (*Config) ApplyDefaults

func (c *Config) ApplyDefaults()

ApplyDefaults applies sensible defaults to the config if left unconfigured. You shouldn't normally need to execute this, because it's called automatically by LoadConfig.

func (*Config) GetGhostPost

func (c *Config) GetGhostPost(rows *sql.Rows) (GhostPost, error)

GetGhostPost parses an SQL row-yielding iterator and returns a GhostPost from it.

Usage:

rows, err := db.Query(fmt.Sprintf("SELECT %v FROM posts LIMIT 10", g2h.QUERY_POSTS_FIELDS))

if err != nil {
	log.Fatalf("failed to query posts from db: %v", err.Error())
}

defer rows.Close()

for rows.Next() {
		post, err := ghosttohugo.GetGhostPost(rows)
		if err != nil {
		// ...
	}
}

func (*Config) IsValid

func (c *Config) IsValid(p GhostPost) bool

IsValid returns true or false if the post meets the criteria for being rendered based on the user-provided configuration. This doesn't get called by any of the other Render functions - it's up to you if you want to perform any filtering at all.

This function does not do any checks regarding the published/draft state.

func (*Config) ParseTemplate

func (conf *Config) ParseTemplate() error

ParseTemplate parses the user-configured template. This should only need to be run once.

func (*Config) Process

func (c *Config) Process()

Process makes a few changes to the config based on the user-provided configuration. You shouldn't normally need to execute this, because it's called automatically by LoadConfig.

func (*Config) ProcessGhostPost

func (c *Config) ProcessGhostPost(post GhostPost) (GhostPost, error)

ProcessGhostPost is called by [GetGhostPost] and fills in/processes fields that are required in order for this module to fulfill its intended purpose.

func (*Config) ProcessHTML

func (c *Config) ProcessHTML(s string) (string, error)

ProcessHTML removes all height and width tags from an input xml string, as well as anything else needed in order to process the document.

func (*Config) RenderAll

func (c *Config) RenderAll(p []GhostPost) error

Renders all the markdown posts from Ghost to the target directory.

func (*Config) RenderOne

func (c *Config) RenderOne(p GhostPost) (int, string, error)

Renders all the markdown posts from Ghost to the target directory. Returns the number of bytes written and the full file path that was written to.

func (*Config) RenderString

func (c *Config) RenderString(post GhostPost) (string, error)

Renders a Ghost post to Hugo markdown.

type FrontMatterConfig

type FrontMatterConfig struct {
	// The string to use instead of 'title' in front matter.
	Title string `json:"title"`
	// The string to use instead of 'date' in front matter.
	Date string `json:"date"`
	// The string to use instead of 'draft' in front matter.
	Draft string `json:"draft"`
	// https://gohugo.io/content-management/urls/#slug
	Slug string `json:"slug"`
}

FrontMatterConfig determines what strings to use for various front matter values - for example, if FrontMatterConfig.Title is set to a value of "pageTitle", the front matter will be rendered like this:

`
---
pageTitle: {{ Post.Title }}
# ...
---
`

func (*FrontMatterConfig) ApplyDefaults

func (f *FrontMatterConfig) ApplyDefaults()

ApplyDefaults applies sensible defaults to the front matter config if left unconfigured. You shouldn't normally need to execute this, because it's called automatically by LoadConfig.

type GhostPost

type GhostPost struct {
	ID                       string
	UUID                     string
	Title                    string
	Slug                     string
	Mobiledoc                sql.NullString
	Lexical                  sql.NullString
	HTML                     sql.NullString
	CommentID                sql.NullString
	Plaintext                sql.NullString
	FeatureImage             sql.NullString
	Featured                 bool
	Type                     string
	Status                   string
	Locale                   sql.NullString
	Visibility               string
	EmailRecipientFilter     string
	CreatedAt                time.Time
	CreatedBy                string
	UpdatedAt                time.Time // sql.NullTime
	UpdatedBy                sql.NullString
	PublishedAt              time.Time // sql.NullTime
	PublishedBy              sql.NullString
	CustomExcerpt            sql.NullString
	CodeinjectionHead        sql.NullString
	CodeinjectionFoot        sql.NullString
	CustomTemplate           sql.NullString
	CanonicalUrl             sql.NullString
	NewsletterId             sql.NullString
	ShowTitleAndFeatureImage bool

	// On the Go side, we need to parse these values before putting them into
	// the [GhostPost] struct.
	SqlCreatedAt string // time.Time
	// On the Go side, we need to parse these values before putting them into
	// the [GhostPost] struct.
	SqlUpdatedAt sql.NullString // sql.NullTime
	// On the Go side, we need to parse these values before putting them into
	// the [GhostPost] struct.
	SqlPublishedAt sql.NullString // sql.NullTime

	// This module parses the status field and determines if it's a draft. This
	// isn't specifically a boolean value in the database. It may not always
	// be accurate if Ghost has special logic that determines if something is
	// a draft or not.
	IsDraft bool
}

GhostPost is the data type that exists in the mysql database as of Ghost v5.87.1.

type PostTemplate

type PostTemplate struct {
	FrontMatterConfig FrontMatterConfig
	Post              GhostPost
	PostDate          string // Post's date rendered as a standard string
	PostHTML          string
	RawShortcodeStart string
	RawShortcodeEnd   string
}

PostTemplate is what's passed to the template directly. Your configuration's template string can use any field from here. For example:

`{{ .Post.FeatureImage }}`

Jump to

Keyboard shortcuts

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