Types/OpenPackage.View/site.standard.document.ps1

<#
.SYNOPSIS
    Views input as `standard.site.document`
.DESCRIPTION
    Views any applicable input object as a `standard.site.document` object
.INPUTS
    OpenPackage
.INPUTS
    OpenPackage.Part
.INPUTS
    string
.LINK
    https://standard.site/
#>

param()

$allInput = @($input) + @($args)

# Make one quick pass over all input
$allInput = @(
    foreach ($in in $allInput) {
        # and expand any packages we find into their parts.
        if ($in -is [IO.Packaging.Package]) {
            $in.GetParts()
        } 
        elseif ($in.Package -is [IO.Packaging.Package]) {
            $in.Package.GetParts()
        }
        else {
            $in
        }
    }
)

# Declare a filter

# Now, let's go over all input
:nextInput foreach ($in in $allInput) {        
    # If the input is a string
    if ($in -is [string]) {
        # Just take the markdown
        # and put it in an at.markpub.markdown object
        
        # and continue to the next input.
        $in | at.markpub.markdown
        continue nextInput
    }

    # If the input has a `$type`, and it is `site.standard.document`
    if ($in.'$type' -eq 'site.standard.document') {
        $in # pass the input thru
        continue nextInput # and continue to the next input.
    }
            
    # If the input is a package part, we can do more
    if (
        $in -is [IO.Packaging.PackagePart] -and 
        # (if it is not a markdown file, we should ignore it).
        $in.Uri -match '(?>\.md|\.markdown)$'
    ) {
        # Get our markdown files as `at.markpub.markdown`
        $atMarkPub = $in | Format-OpenPackage -View at.markpub.markdown
        # and create a standard site document to hold them.
        $standardSiteDocument = [Ordered]@{'$type' = 'site.standard.document'}
        # the content is the `at.markpub.markdown`
        $standardSiteDocument.content = $atMarkPub
        # and we want to propagate various front-matter into the site:
        if ($atMarkPub.frontMatter) {
            # * `title`
            if ($atMarkPub.frontMatter.title) {
                $standardSiteDocument.title = $atMarkPub.frontMatter.title
            }

            # * `description`
            if ($atMarkPub.frontMatter.description) {
                $standardSiteDocument.title = $atMarkPub.frontMatter.description
            }

            # * `tags`
            if ($atMarkPub.frontMatter.tags) {
                $standardSiteDocument.tags = @(
                    $atMarkPub.frontMatter.tags
                )
            }
            
            # * `path`
            if ($atMarkPub.frontMatter.path) {
                $standardSiteDocument.path =
                    $atMarkPub.frontMatter.path -replace '^/?', '/'
            }

            # * `image` to `coverImage`
            if ($atMarkPub.frontMatter.image) {
                # * Please note that at this point our image should be a url.
                $standardSiteDocument.coverImage = $atMarkPub.frontMatter.image
            }

            # * `bskyPostRef`
            if ($atMarkPub.frontMatter.bskyPostRef) {
                $standardSiteDocument.bskyPostRef = $atMarkPub.frontMatter.bskyPostRef
            }
        }

        # Now get our inner text
        $innerText =
            ("<article>$(
                $atMarkPub.text.markdown |
                    ConvertFrom-Markdown |
                    Select-Object -Expand Html
            )</article>"
 -as [xml]).article.innerText

        # set the text content
        if ($innerText) {
            $standardSiteDocument.textContent = $innerText
        }
        
        
        # Make it an object, and emit the standard site document.
        [PSCustomObject]$standardSiteDocument
        # Then continue to the next input.
        continue nextInput
    }

    if ($in -is [IO.Packaging.PackagePart] -and 
        $in.Uri -match '(?>\.json)$'
    ) {
        # If there is no reader
        if (-not $in.Reader) {
            # write a warning and continue to the next input.
            Write-Warning "No reader for '$($in.Uri)'"
            continue nextInput
        }

        foreach ($value in $in.Read()) {
            if ($value.'$type' -eq 'site.standard.document') {
                $value
            }
            elseif ($value.value.'$type' -eq 'site.standard.document') {
                $value.value
            }
            elseif ($value.record.'$type' -eq 'site.standard.document') {
                $value.record
            }
        }        
    }
}