Skip to content

Commit

Permalink
feat: add batch export functionality for markdown to PDF using Typora…
Browse files Browse the repository at this point in the history
… on MacOS
  • Loading branch information
p0n1 committed Jan 10, 2025
1 parent a13e17e commit 9859aa9
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
22 changes: 22 additions & 0 deletions batch-export.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash
# Will export all markdown files to PDF files using Typora on MacOS
# Find all markdown files (both .md and .markdown extensions) and sort them
find . -type f \( -name "*.md" -o -name "*.markdown" \) | sort | while read -r file; do
# Skip files in .git directory
if [[ "$file" == *".git"* ]]; then
continue
fi

# Skip blacklisted files
if [[ "$file" == *"README.md"* ||
"$file" == *"CONTRIBUTING.md"* ||
"$file" == *"mle_div.md"* ||
"$file" == *"mmcs/interpretions/src"* ]]; then
continue
fi

echo "Processing: $file"
./export-pdf.sh "$file"
done

echo "All markdown files have been processed."
26 changes: 26 additions & 0 deletions export-pdf.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash
# Will export a markdown file to a PDF file using Typora on MacOS

# Get the input markdown file path
md_file="$(realpath "$1")"
# Construct the PDF file path by replacing .md with .pdf
pdf_file="${md_file%.md}.pdf"

# Check if PDF exists and remove it
if [ -f "$pdf_file" ]; then
echo "Removing existing PDF file: $pdf_file"
rm "$pdf_file"
fi

# Export to PDF using Typora
echo "Exporting $md_file"
osascript typora-export-pdf.applescript "$md_file"
echo "Exported to $pdf_file"
# Check PDF file
ls -lh "$pdf_file"

# Open the PDF file if flag is set
if [ "$2" == "open" ]; then
echo "Opening $pdf_file"
open "$pdf_file"
fi
37 changes: 37 additions & 0 deletions typora-export-pdf.applescript
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# For Typora on MacOS
on run {inputFile}
tell application "Typora"
activate
open inputFile
tell application "System Events"

-- Wait for Typora window to be ready
delay 1
repeat until (exists window 1 of process "Typora")
delay 0.1
end repeat

-- Export to PDF
delay 1
keystroke "p" using {command down, control down}

-- Wait for save dialog
delay 1
repeat until (exists sheet 1 of window 1 of process "Typora")
delay 0.1
end repeat

-- Confirm save
delay 1
keystroke return

-- Wait until save dialog disappears
delay 1
repeat until not (exists sheet 1 of window 1 of process "Typora")
delay 0.1
end repeat
end tell

close front window -- Close the current file
end tell
end run

0 comments on commit 9859aa9

Please sign in to comment.