Class

Item

class Item<M> where M : Metadata

A model representing an item.

An item can be any text file (like a Markdown or RestructedText file). Readers will turn the file into an Item, and Writers will turn the Item into a String (for example HTML or RSS) to be written to disk.

Mentioned In

Initializers

init(
  absoluteSource: Path,
  relativeSource: Path,
  relativeDestination: Path,
  title: String,
  body: String,
  date: Date,
  created: Date,
  lastModified: Date,
  metadata: M
)
init(from decoder: any Decoder) throws

Creates a new instance by decoding from the given decoder.

This initializer throws an error if reading from the decoder fails, or if the data read is corrupted or otherwise invalid.

  • Parameter decoder: The decoder to read data from.
convenience init(
  title: String,
  body: String = "",
  date: Date = Date(),
  relativeDestination: Path? = nil,
  metadata: M
)

Create an Item programmatically (without reading from a file).

Parameters

title

The title of the item.

body

The body content. Defaults to an empty string.

date

The date of the item. Defaults to the current date.

relativeDestination

The output path relative to the site's output folder. Defaults to title-slug/index.html.

metadata

The parsed metadata.

Instance Properties

let absoluteSource: Path

The absolute path of the file

var body: String

The body of the file, without the metadata header, and without the first title.

var children: [any AnyItem]

Type-erased children. Populated automatically by nested registrations.

let created: Date

The creation date of the item. Please note that this value can be inconsistent when cloning or pulling from git, see https://github.com/loopwerk/Saga/issues/21.

var date: Date

The date of the item. Will be taken from the metadata if available, defaults to the creation date otherwise. Please note that the creation date value can be inconsistent when cloning or pulling from git, see https://github.com/loopwerk/Saga/issues/21.

var filenameWithoutExtension: String { get }
let lastModified: Date

The last modified date of the item. Please note that this value can be inconsistent when cloning or pulling from git, see https://github.com/loopwerk/Saga/issues/21.

var locale: SagaLocale?

The locale of this item, or nil when i18n is not configured.

var metadata: M

The parsed metadata. Metadata can be any Codable object.

weak var parent: (any AnyItem)?

Type-erased parent. Populated automatically by nested registrations.

var relativeDestination: Path

The destination, where the Writer will write it to disk.

let relativeSource: Path

The path of the file, relative to the site’s input.

var title: String

The title of the item.

var translations: [SagaLocale : any AnyItem]

Other language versions of this item, keyed by locale.

var url: String { get }

Instance Methods

func children<C>(as type: C.Type) -> [Item<C>] where C : Metadata

Typed accessor for children.

func parent<P>(as type: P.Type) -> Item<P> where P : Metadata

Typed accessor for parent.

func translation(for locale: SagaLocale) -> Item<M>?

Returns the translation for the given locale, if available.

Relationships

Conforms To

AnyItem , Swift.Decodable , Swift.Encodable , Swift.Sendable