Skip to main content

Search OfficeIMO

Enter a topic, API type, or PowerShell command.

Documentation Search all OfficeIMO/
Class

FrontMatterBlock

OfficeIMO.Markdown

Namespace
OfficeIMO.Markdown
Assembly
OfficeIMO.Markdown
Source revision
0eef6a5756b9 with working tree changes
Implements
IFrontMatterMarkdownBlock IMarkdownBlock ISyntaxMarkdownBlock
Modifiers
sealed

Front matter at the start of a document: YAML between --- fences, read and written by OfficeIMO.Yaml, or TOML between +++ fences, read and written by OfficeIMO.Toml. FrontMatterBlock.Yaml or FrontMatterBlock.Toml holds the lossless document: edits made to it are saved with only the changed text rewritten, and an unedited block renders back to the bytes it was read from. FrontMatterBlock.Entries, FrontMatterBlock.Select and FrontMatterBlock.TryGetValue read both formats the same way.

Inheritance

Usage

Explore 5 API references to this type

This type appears in these public API surfaces even when no hand-authored example is attached directly to the page.

Methods

public Entry? FindEntry(string key, StringComparison comparison = OrdinalIgnoreCase) #

Finds a top-level entry by key. When keys repeat under the comparison, the last one wins, as it does for YAML loaders that accept repeated keys.

Parameters

key string required
comparison System.StringComparison = OrdinalIgnoreCase optional

Returns

Entry?
public static FrontMatterBlock FromObject(object data) #

Creates front matter from an anonymous object, a dictionary or another object's public properties. Nested dictionaries, objects and collections become YAML mappings and sequences, and YamlNode values are used as they are.

Parameters

data object required
public static FrontMatterBlock FromObject<T>(T? data) #

Creates front matter from a typed object's public properties or from a dictionary. Nested dictionaries and collections become YAML mappings and sequences; other nested objects are written as their invariant text.

Type Parameters

T

Parameters

data T? required
public static FrontMatterBlock FromToml(TomlDocument document) #

Creates TOML front matter, written between +++ fences, from a TOML document. The block keeps the document, so edits made to it later are part of the rendered front matter.

Parameters

document OfficeIMO.Toml.TomlDocument required
public bool HasEntry(string key, StringComparison comparison = OrdinalIgnoreCase) #

Checks whether the front matter has a top-level entry with the key.

Parameters

key string required
comparison System.StringComparison = OrdinalIgnoreCase optional

Returns

bool
public string Render() #

Renders the front matter with its fences. Unedited front matter read from Markdown renders the text it was read from; edits made through FrontMatterBlock.Yaml or FrontMatterBlock.Toml rewrite only what changed.

Returns

string
public YamlNode? Select(string path) #

Returns the node a path such as author.name or tags[0] addresses, or null.

Parameters

path string required

Returns

YamlNode?
public bool TryGetValue<T>(string key, out T? value) #

Reads a top-level scalar entry as T through the YAML schema, as YamlNode.TryGetValue does: string, bool, int, long, double, float, decimal and their nullable forms. Returns false when the key is missing, the value is a collection or it does not convert.

Type Parameters

T

Parameters

key string required
value T? required

Returns

bool

Exceptions

  • NotSupportedException – T is not a supported type.

Properties

public MarkdownFrontMatterFormat Format { get; } #

Gets the front matter language: YAML between --- fences or TOML between +++ fences.

public YamlDocument? Yaml { get; } #

Gets the YAML document of YAML front matter, or null for TOML front matter and when the text between the fences is not valid YAML; then FrontMatterBlock.Diagnostics holds the error and the block renders FrontMatterBlock.RawText unchanged.

public TomlDocument? Toml { get; } #

Gets the TOML document of TOML front matter, or null for YAML front matter and when the text between the fences is not valid TOML; then FrontMatterBlock.Diagnostics holds the error and the block renders FrontMatterBlock.RawText unchanged.

public YamlMapping? Mapping { get; } #

Gets the root mapping of the front matter, or null when it is empty, invalid or not a mapping. For TOML front matter it is a read-only view of the root table that follows edits made through FrontMatterBlock.Toml.

public IReadOnlyList<Entry> Entries { get; } #

Gets the top-level entries of FrontMatterBlock.Mapping in source order, with Markdown source spans for entries that were read from Markdown. The list follows edits made through FrontMatterBlock.Yaml or FrontMatterBlock.Toml.

public string? RawText { get; } #

Gets the text between the fences as it was read from Markdown, or null for front matter built in code.

public MarkdownSourceSpan? BodySourceSpan { get; } #

Gets the source span of the text between the fences when parsed from Markdown.

public IReadOnlyList<OfficeDiagnostic> Diagnostics { get; } #

Gets findings from reading the front matter: a OfficeDiagnosticCodes.MarkdownFrontMatterInvalidYaml or OfficeDiagnosticCodes.MarkdownFrontMatterInvalidToml error located in the Markdown source when the text is invalid, and otherwise the YAML document's own findings, such as repeated keys, located within the front matter text. Valid TOML has no findings.