Skip to content

Instantly share code, notes, and snippets.

@danmayer
Created August 16, 2017 22:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danmayer/9629fa62e8e855472f5dc7dda1ac29bc to your computer and use it in GitHub Desktop.
Save danmayer/9629fa62e8e855472f5dc7dda1ac29bc to your computer and use it in GitHub Desktop.
convert a folder of markdown files to confluence wiki docs
#!/usr/bin/env ruby
require 'rubygems'
require 'confluence/api/client'
require 'kramdown'
username = 'your@email.com'
password = 'your_pass'
url = 'https://your_co.atlassian.net/wiki'
space = 'DP'
# if you don't want your files at the root give a parent page to nest them under
parent_page_id = 5564541
path = '/Users/danmayer/projects/DevDocuments'
original_root_path = 'https://github.com/OffgridElectric/DevDocuments/blob/master'
###
# see https://github.com/amishyn/confluence-api-client for usage
###
client = Confluence::Api::Client.new(username, password, url)
errors = []
files = Dir.glob("#{path}/**/*")
converted = 0
files.each do |file|
if file.to_s.match('.md')
converted += 1
next if converted <= 3
relative_path = file.to_s.gsub("#{path}/",'')
title = file.to_s.gsub("#{path}/",'').gsub('/','_').gsub('.md','')
content = File.read(file)
content += "\n\n\n [original document](#{original_root_path}/#{relative_path})"
html_content = Kramdown::Document.new(content).to_html
puts "creating #{title}"
page = client.create({type:"page",
title: title,
space: {key: space},
ancestors:[{id: parent_page_id}],
body: {storage:{value: html_content,
representation: "storage"}}})
errors << page if page['statusCode'].to_i > 200
end
end
puts "converted #{converted}"
puts "errors:"
puts errors.inspect
puts 'done'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment