Position

The Position class defines the location of text in a file.

Definition

Source Code

# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

using module ./DocumentLink.psm1

class ParsedDocument {
  [System.IO.FileInfo]$FileInfo
  [string]$RawContent
  [Markdig.Syntax.MarkdownDocument]$ParsedMarkdown
  [System.Collections.Specialized.OrderedDictionary]$FrontMatter
  [string]$Body
  [DocumentLink[]]$Links

  hidden [bool]$HasParsedLinks

  ParsedDocument() {}

  hidden ParseLinksFromBody() {
    $this.Links = [DocumentLink]::Parse($this.Body)
    | ForEach-Object -Process {
      # Add the file info to each link
      $_.Position.FileInfo = $FileInfo
      # Emit the link for the list
      $_
    }

    $this.HasParsedLinks = $true
  }

  [DocumentLink[]] ParsedLinks() {
    if (!$this.HasParsedLinks) {
      $this.ParseLinksFromBody()
    }

    return $this.Links
  }

  [DocumentLink[]] ParsedLinks([bool]$Force) {
    if (!$this.HasParsedLinks -or $Force) {
      $this.ParseLinksFromBody()
    }

    return $this.Links
  }

  [DocumentLink[]] InlineLinks() {
    return [DocumentLink]::FilterForInlineLinks($this.Links)
  }

  [DocumentLink[]] ReferenceLinks() {
    return [DocumentLink]::FilterForReferenceLinks($this.Links)
  }

  [DocumentLink[]] ReferenceDefinitions() {
    return [DocumentLink]::FilterForReferenceDefinitions($this.Links)
  }

  [DocumentLink[]] ReferenceLinksAndDefinitions() {
    return [DocumentLink]::FilterForReferenceLinksAndDefinitions($this.Links)
  }

  [DocumentLink[]] UndefinedReferenceLinks() {
    return [DocumentLink]::FilterForUndefinedReferenceLinks($this.Links)
  }

  [DocumentLink[]] UnusedReferenceLinkDefinitions() {
    return [DocumentLink]::FilterForUnusedReferenceLinkDefinitions($this.Links)
  }

  [DocumentLink[]] ValidReferenceLinksAndDefinitions() {
    return [DocumentLink]::FilterForValidReferenceLinksAndDefinitions($this.Links)
  }

  [string] ToDecoratedString() {
    return $this.Body
    | ConvertFrom-Markdown -AsVT100EncodedString
    | Select-Object -ExpandProperty VT100EncodedString
  }
}

The Position class is used to define the location of text in a document. This allows other classes and cmdlets to find a specific piece of text precisely in a document instead of only relying on matching the text’s value.

It includes information about the file, if any, as well as the line and column number.

Examples

Example 1

[Position]$Position = @{
    FileInfo    = Get-Item -Path ./CHANGELOG.md
    LineNumber  = 1
    StartColumn = 1
}

"The position is:`n`t$Position"
The position is:
    C:\code\pwsh\Documentarian\Source\Modules\Documentarian\CHANGELOG.md:1:1

Constructors

Position()
Initializes a new instance of the Position class.

Methods

ToString()
Returns the string representation of a position.

Properties

FileInfo
Represents the file the Position exists in, if any.
LineNumber
Represents the line from the text the Position is at.
StartColumn
Represents how many characters into a line the Position is.