Skip to content

Instantly share code, notes, and snippets.

@sbolel
Last active April 3, 2024 08:12
Show Gist options
  • Save sbolel/3bfabae3c4ad6b219b4c94be04d9b2b7 to your computer and use it in GitHub Desktop.
Save sbolel/3bfabae3c4ad6b219b4c94be04d9b2b7 to your computer and use it in GitHub Desktop.
Oh My Zsh plugin to output the contents of multiple files into a single markdown file with code blocks

Markdown File Generator Script

This Oh My Zsh plugin combines the contents of multiple files into a single markdown document, supporting syntax highlighting based on file extensions. If an output file is not specified, the script copies the content to the clipboard, making it easy to paste anywhere.

Features

  • Combines multiple files into one markdown document.
  • Automatically adds syntax highlighting based on the file extension.
  • Option to copy combined content to the clipboard if no output file is specified.

Requirements

  • Z shell (zsh)
  • pbcopy command available (for clipboard functionality, macOS default, use alternatives like xclip or clip.exe for Linux and Windows)

Usage

To use the script, run it from the command line, providing the files you wish to combine as arguments. You can optionally specify an output file using the -o flag.

to_markdown [-o output_file] file1 [file2 ...]

Installation

To install this script as a custom plugin for Oh My Zsh:

  1. Copy this plugin directory into the custom/plugins directory of your Oh My Zsh installation. The default directory is:
git clone [email protected]:sbolel/.dotfiles.git
cd .dotfiles
cp -r .oh-my-zsh/custom/plugins/to_markdown ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/to_markdown
  1. Add to_markdown to the list of plugins in your .zshrc file. Find the line that starts with plugins= and add to_markdown to the list of plugins to be loaded.
plugins=(... to_markdown)
  1. Reload your shell configuration to apply the changes:
source ~/.zshrc

Examples

  • To combine LICENSE and package.json and copy the result to the clipboard:
to_markdown ./LICENSE ./package.json
  • To combine README.md and CONTRIBUTING.md into a file named combined.md:
to_markdown -o combined.md ./README.md ./CONTRIBUTING.md
#!/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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment