1function mdas
 2    set DIR "$HOME/LLM Responses"
 3    mkdir -p $DIR
 4    pushd $DIR
 5
 6    set exit_status 0
 7
 8    # Parse command line arguments
 9    argparse 'p/pdf' 'h/html' 'e/epub' -- $argv
10    or begin
11        echo "Usage: mdas [-p|--pdf] [-h|--html] [-e|--epub]" >&2
12        echo "At least one format flag must be specified." >&2
13        popd
14        return 1
15    end
16
17    # Check if at least one format is specified
18    if not set -q _flag_pdf; and not set -q _flag_html; and not set -q _flag_epub
19        echo "Error: At least one format flag (-p, -h, or -e) must be specified." >&2
20        popd
21        return 1
22    end
23
24    read -z CONTENT
25    set FILENAME (echo "$CONTENT" | llm -f - -t 'srht:~amolith/filename')
26    set TITLE (llm -c "Give me that same file name but now in conversation case. For example, 'Port-out PIN implementation spec', but without quotes. The first letter should be capitalized, proper nouns capitalized, and most everything else lowercased.")
27
28    # remove path traversal, special chars, limit length
29    set FILENAME (echo "$FILENAME" | sed 's/[^a-zA-Z0-9._-]/_/g' | sed 's/\.\.//g' | cut -c1-200)
30    # remove potentially dangerous chars but keep spaces
31    set TITLE (echo "$TITLE" | sed 's/[<>&"'\''`]//g' | sed 's/\$//g' | cut -c1-200)
32
33   	set date "$(date +%Y-%m-%d)"
34   	set header "---
35title: $TITLE
36date: $date
37mainfont: Atkinson Hyperlegible Next
38sansfont: Atkinson Hyperlegible Next
39monofont: 0xProto
40---"
41
42    set CONTENT "$header
43
44$CONTENT"
45
46    echo $CONTENT > "$FILENAME.md"
47
48    if not prettier -w --parser markdown --no-editorconfig --no-config "$FILENAME.md"
49        echo "Error: prettier failed" >&2
50        set exit_status 1
51    else
52        echo "$FILENAME.md"
53    end
54
55    # Convert to HTML if requested
56    if test $exit_status -eq 0; and set -q _flag_html
57        if not pandoc --from markdown --to html -so "$FILENAME.html" "$FILENAME.md"
58            echo "Error: HTML conversion failed" >&2
59            set exit_status 1
60        else
61            echo "$FILENAME.html"
62        end
63    end
64
65    # Convert to PDF if requested
66    if test $exit_status -eq 0; and set -q _flag_pdf
67        if not pandoc --pdf-engine=xelatex --from markdown --to pdf --epub-title-page=false -o "$FILENAME.pdf" "$FILENAME.md"
68            echo "Error: PDF conversion failed" >&2
69            set exit_status 1
70        else
71            echo "$FILENAME.pdf"
72        end
73    end
74
75    # Convert to EPUB if requested
76    if test $exit_status -eq 0; and set -q _flag_epub
77        if not pandoc --from markdown --to epub -o "$FILENAME.epub" "$FILENAME.md"
78            echo "Error: EPUB conversion failed" >&2
79            set exit_status 1
80        else
81            echo "$FILENAME.epub"
82        end
83    end
84
85    popd
86
87
88
89    return $exit_status
90end