|
#!/usr/bin/env zsh |
|
################################################################################ |
|
# Plugin: to_markdown.plugin.zsh |
|
# Description: An Oh My Zsh plugin to combine the contents of multiple files |
|
# into a single markdown document, with support for syntax highlighting based |
|
# on file extensions. If no output file is specified, the content is copied |
|
# to the clipboard, facilitating easy pasting. |
|
# |
|
# Features: |
|
# - Combines multiple files into a single markdown document. |
|
# - Adds syntax highlighting automatically, based on file extensions. |
|
# - Copies combined content to the clipboard if no output file is specified. |
|
# |
|
# Requirements: |
|
# - Z shell (zsh) |
|
# - `pbcopy` available for clipboard functionality (macOS default, with |
|
# alternatives like `xclip` or `clip.exe` for Linux and Windows). |
|
# |
|
# Usage: |
|
# to_markdown [-o output_file] file1 [file2 ...] |
|
# -o Specify the output file to write the markdown content. If omitted, |
|
# content is copied to the clipboard. |
|
# |
|
# Installation: |
|
# 1. Clone and copy the plugin into the Oh My Zsh custom plugins directory. |
|
# 2. Add 'to_markdown' to the plugins list in your .zshrc file. |
|
# 3. Reload your shell configuration with `source ~/.zshrc`. |
|
# |
|
# Examples: |
|
# - Copy LICENSE and package.json contents to clipboard: |
|
# to_markdown ./LICENSE ./package.json |
|
# |
|
# - Combine README.md and CONTRIBUTING.md into combined.md: |
|
# to_markdown -o combined.md ./README.md ./CONTRIBUTING.md |
|
# |
|
# Author: Sinan Bolel |
|
# Date: 2024-04-01 |
|
################################################################################ |
|
|
|
function to_markdown() { |
|
# Function to display usage information |
|
usage() { |
|
echo "Usage: $0 [-o output_file] file1 [file2 ...]" |
|
echo "Combine the contents of multiple files into a single markdown document." |
|
echo "" |
|
echo "Options:" |
|
echo " -o Specify the output file to write the markdown content. If omitted, content is copied to the clipboard." |
|
echo " -h Display this help and exit." |
|
echo "" |
|
echo "Example:" |
|
echo " $0 -o combined.md ./LICENSE ./package.json" |
|
echo " $0 ./README.md" |
|
exit 1 |
|
} |
|
|
|
# Check if no arguments were passed |
|
if [ $# -eq 0 ]; then |
|
usage |
|
fi |
|
|
|
# Initialize variables |
|
output_file="" |
|
markdown_content="" |
|
should_copy_to_clipboard=1 |
|
|
|
# Use getopts to parse the -o flag and its argument for output file |
|
while getopts "o:" opt; do |
|
case $opt in |
|
o) |
|
output_file="$OPTARG" |
|
should_copy_to_clipboard=0 |
|
;; |
|
\?) |
|
echo "Invalid option: -$OPTARG" >&2 |
|
exit 1 |
|
;; |
|
esac |
|
done |
|
|
|
# Adjust the argument pointer to skip over the processed flags |
|
shift $((OPTIND - 1)) |
|
|
|
# Counter for numbering the files in the markdown |
|
file_counter=1 |
|
|
|
# Initialize the markdown content variable |
|
markdown_content="# File Contents\n\n" |
|
|
|
# Iterate over the file paths |
|
for file in "$@"; do |
|
# Check if the file exists |
|
if [ -f "$file" ]; then |
|
# Extract the file extension |
|
file_extension="${file##*.}" |
|
# Append the file path as a subheading |
|
markdown_content+="## $file\n\n" |
|
# Append the file contents in a code block |
|
markdown_content+='```' |
|
markdown_content+="$file_extension" |
|
markdown_content+='\n' |
|
markdown_content+=$(cat "$file" | sed 's/\\/\\\\/g') |
|
markdown_content+='\n```\n\n' |
|
else |
|
echo "File not found: $file" |
|
fi |
|
done |
|
|
|
# Check if an output file is specified |
|
if [ -n "$output_file" ]; then |
|
# Write the markdown content to the output file |
|
echo "$markdown_content" >"$output_file" |
|
echo "Markdown file generated: $output_file" |
|
else |
|
# Copy the markdown content to the clipboard using pbcopy |
|
echo "$markdown_content" | pbcopy |
|
echo "Markdown content copied to clipboard." |
|
fi |
|
} |