View Source Magma.DocumentStruct.Section (Magma v0.2.0)

Recursive structure for the nested sections of a Magma.DocumentStruct.

Summary

Functions

Checks if the given section is empty, i.e. it has no content and nested sections.

Checks if the given section consists solely of subsection headers.

Fetches the section with the given title and returns it in an ok tuple.

Creates a new section.

Removes all comment blocks from the given section_or_content.

Resolves internal links in the provided section by replacing them with their content or display text.

Processes and resolves transclusions within the given section.

Fetches the first section with the given title.

Sets a new header for the given section, updating the :title and :level fields accordingly.

Sets a new header with the given title and level for the given section, updating the :title and :level fields accordingly.

Changes the header level of section to the given level.

Shifts the header level of section by the given shift_level.

Types

@type t() :: %Magma.DocumentStruct.Section{
  content: [Panpipe.AST.Node.t()],
  header: Panpipe.AST.Header.t(),
  level: integer(),
  sections: [t()],
  title: binary()
}

Functions

@spec empty?(t()) :: boolean()

Checks if the given section is empty, i.e. it has no content and nested sections.

@spec empty_content?(t()) :: boolean()

Checks if the given section consists solely of subsection headers.

@spec fetch(t() | Magma.DocumentStruct.compatible(), binary()) :: {:ok, t()} | :error

Fetches the section with the given title and returns it in an ok tuple.

If no section with section exists, it returns :error.

This implements Access.fetch/2 function, so that the section[title] syntax and the Kernel macros for accessing nested data structures like get_in/2 are supported.

This function only searches sections directly under the given section. For a recursive search, use section_by_title/2.

Link to this function

new(header, content, sections \\ [])

View Source
@spec new(
  Panpipe.AST.Header.t() | {pos_integer(), binary()},
  [Panpipe.AST.Node.t()],
  [t()]
) :: t()

Creates a new section.

Link to this function

preserve_eex_tags(section)

View Source
Link to this function

remove_comments(section_or_content)

View Source
@spec remove_comments(t()) :: t()
@spec remove_comments([Panpipe.AST.Node.t()]) :: [Panpipe.AST.Node.t()]

Removes all comment blocks from the given section_or_content.

This function cleans up the document by removing all comment blocks (<!-- comment -->).

Link to this function

resolve_links(section, opts \\ [])

View Source
@spec resolve_links(
  t(),
  keyword()
) :: t()

Resolves internal links in the provided section by replacing them with their content or display text.

The style of the resolved links can be specified with the :style option, which accepts the following values:

  • :plain (default) - no styling
  • :emph - italic
  • :strong - bold
  • :underline - underlined
  • a function accepting the children of the link AST and returning the replacement AST node
Link to this function

resolve_transclusions(section)

View Source

Processes and resolves transclusions within the given section.

Transclusion resolution in Magma is a procedure where an Obsidian transclusion, such as ![[Some document]], is replaced with its actual content. This mechanism forms the foundation for constructing LLM prompts in Magma. The content from the referenced document or section undergoes several processing steps before its insertion:

  • Comments (<!-- comment -->) are removed.
  • Internal links are replaced with the target as plain text.
  • Transclusions within the transcluded content itself are resolved recursively (unless it would result in an infinite recursion).
  • If the transcluded content (after removing the comments), consists exclusively of a heading with no content below it, the transclusion is resolved with the empty string.
  • The level of the transcluded sections is adjusted according to the current level at the point of the transclusion.

Different types of transclusions are resolved in slightly varied ways, particularly regarding the leading header of the transcluded content:

  • Inline transclusions: Exclude the leading header.
  • Custom header transclusions: Replace the leading header.
  • Empty header transclusions: Retain the leading header.
Link to this function

section_by_title(section, title)

View Source
@spec section_by_title(t(), binary()) :: t() | nil

Fetches the first section with the given title.

Other than accessing the sections with the fetch/2, this searches the sections recursively.

Link to this function

set_header(section, header)

View Source

Sets a new header for the given section, updating the :title and :level fields accordingly.

Link to this function

set_header(section, title, level)

View Source

Sets a new header with the given title and level for the given section, updating the :title and :level fields accordingly.

Link to this function

set_level(section, level)

View Source
@spec set_level(t(), non_neg_integer()) :: t()

Changes the header level of section to the given level.

Computes the difference to the current level of section and shifts the level recursively on all subsections using shift_level/2.

Link to this function

shift_level(section, shift_level)

View Source
@spec shift_level(t(), integer()) :: t()

Shifts the header level of section by the given shift_level.

All subsections are shifted recursively.

Link to this function

to_markdown(section, opts \\ [])

View Source
@spec to_markdown(
  t(),
  keyword()
) :: binary()

Converts a Magma.DocumentStruct.Section into a Markdown string.