Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

nushell/modules/markdown_render.nu

Raw
# Native subset; unsupported or unsafe sources remain complete literal text.
def literal [source: string] {
    $source | str replace --all --regex '[\x00-\x09\x0b-\x1f\x7f-\x9f]' {|| $in | encode hex | str lowercase | $'\x($in)' }
}

def supported [node: record] {
    match $node.type {
        text => true
        emphasis | strong | link | h1 | h2 | h3 | h4 | h5 | h6 => {
            $node.children | all {|child| $child.type == text }
        }
        list => { $node.attrs.level == 0 and not $node.attrs.ordered and ($node.children | all {|child| $child.type == text }) }
        code => { $node.attrs.fence }
        _ => false
    }
}

def node-text [node: record] {
    let text = $node.children | each {|child| literal $child.attrs.value } | str join
    match $node.type {
        text | code => (literal $node.attrs.value)
        emphasis => $'(ansi i)($text)(ansi reset)'
        strong | h1 | h2 | h3 | h4 | h5 | h6 => $'(ansi bo)($text)(ansi reset)'
        link => $'(ansi u)($text)(ansi reset) <(literal $node.attrs.url)>'
        list => $'• ($text)'
    }
}

# Best-effort terminal cells, not `fill`'s grapheme count.
# Ambiguous-width punctuation (arrows, curly quotes, etc.) stays one cell.
export def display-width [text: string] {
    let plain = $text | ansi strip
    if ($plain | str length --utf-8-bytes) == ($plain | str length --chars) { return ($plain | str length --chars) }
    $plain | split chars --grapheme-clusters | each {|g|
        if $g =~ '[\x{1100}-\x{115f}\x{2329}\x{232a}\x{2e80}-\x{a4cf}\x{ac00}-\x{d7a3}\x{f900}-\x{faff}\x{fe10}-\x{fe19}\x{fe30}-\x{fe6f}\x{ff01}-\x{ff60}\x{ffe0}-\x{ffe6}\x{1f000}-\x{1faff}\x{20000}-\x{3fffd}\x{fe0f}\x{20e3}]' { 2 } else if $g =~ '^\p{M}+$' { 0 } else { 1 }
    } | math sum
}

# Keep whitespace as source characters; only overlong tokens split graphemes.
def wrap-line [text: string width: int] {
    if (display-width $text) <= $width { return [{text: $text offset: 0}] }
    mut rows = []
    mut line = ''
    mut cells = 0
    mut offset = 0
    mut start = 0
    for token in ($text | parse --regex '(?<token>\s+|\S+)' | get token) {
        let size = display-width $token
        if $token !~ '^\s+$' and $size <= $width {
            if $cells + $size > $width and $line != '' {
                $rows = ($rows | append {text: $line offset: $start})
                $line = ''; $cells = 0; $start = $offset
            }
            $line += $token
            $cells += $size
            $offset += ($token | str length --grapheme-clusters)
        } else {
            for g in ($token | split chars --grapheme-clusters) {
                let size = display-width $g
                if $cells + $size > $width and $line != '' {
                    $rows = ($rows | append {text: $line offset: $start})
                    $line = ''; $cells = 0; $start = $offset
                }
                $line += $g; $cells += $size; $offset += 1
            }
        }
    }
    $rows | append {text: $line offset: $start}
}

# Whole-source literal fallback avoids losing unknown Markdown or splitting SGR.
# Styled single lines stay styled only when they fit; raw sources remain in RAM.
export def format-blocks [source: string prefix: string = md --width: int = 80] {
    let width = [1 $width] | math max
    let safe = literal $source
    let nodes = if $safe == $source and not ($source | str contains (char nl)) and not ($source | str contains '\n') {
        try { $source | from md --verbose } catch { [] }
    } else { [] }
    let styled = if ($nodes | is-not-empty) and ($nodes | all {|node| supported $node }) {
        $nodes | each {|node| node-text $node } | str join
    } else { '' }
    let rows = if $styled != '' and (display-width $styled) <= $width { [$styled] } else { $safe | split row (char nl) }
    $rows | enumerate | each {|row|
        wrap-line $row.item $width | each {|part|
            {id: ($prefix + ':' + ($row.index | into string)) kind: text line: $part.offset text: $part.text}
        }
    } | flatten
}

export def main [file?: path --width: int = 80] {
    let input = $in
    let source = if $file != null { open --raw $file } else { $input | default '' }
    format-blocks $source --width ([1 $width] | math max) | get text | str join (char nl)
}