Skip to content

Instantly share code, notes, and snippets.

@aenain
Created November 12, 2017 14:20
Show Gist options
  • Save aenain/a5ca7d303fd50e434ffe3f0769ae8074 to your computer and use it in GitHub Desktop.
Save aenain/a5ca7d303fd50e434ffe3f0769ae8074 to your computer and use it in GitHub Desktop.
Convert a markdown file to a google doc file
# Usage: md2gd <PATH>.md
# Prerequisites:
# - gdrive cli
# - brew install gdrive
# - https://github.com/prasmussen/gdrive
# - authorization for the action is required
# - pandoc
# - brew install pandoc
# Known issues:
# - lists started with dashes in markdown end up being bulleted in a GDoc
# - to remedy this select entire list
# - open Format -> Lists -> Other punctors -> look for dash and choose small em dash
class CommandExecutionError < StandardError; end
def execute_cmd(command)
puts "Executing: #{command}"
output = `#{command}`.tap { |it| puts it }
raise CommandExecutionError, ouput if $?.exitstatus.to_i > 0
end
GDRIVE_DIR_ID = '1Sxld8oEnTcO41zWYBt1EI59VQImaC2Q2'.freeze
markdown_path = ARGV.fetch(0)
docx_path = markdown_path.gsub(/\.md/, '.docx')
# convert markdown to docx
execute_cmd("pandoc '#{markdown_path}' --from markdown --to docx -o '#{docx_path}'")
# import docx to given folder and open it in browser
execute_cmd("gdrive import --parent '#{GDRIVE_DIR_ID}' '#{docx_path}' | awk '/Imported/{print $2}' | xargs -I{} open 'https://docs.google.com/document/d/{}/edit'")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment