Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to migrate your Hubspot blog to GitHub Pages, Jekyll, or somewhere else
require 'active_support'
require 'fileutils'
require 'reverse_markdown'
require 'hubspot-ruby'
require 'sanitize'
Hubspot.configure hapikey: ENV.fetch('HUBSPOT_API_KEY')
HUBSPOT_BLOG_ID = ENV.fetch('HUBSPOT_BLOG_ID')
POSTS_DIR_NAME = 'blog'
POSTS_DIR = File.expand_path(POSTS_DIR_NAME, __dir__)
SANITIZE_CONFIG = Sanitize::Config.merge(Sanitize::Config::BASIC, elements: Sanitize::Config::BASIC[:elements] + %w(h1 h2 h3 h4 h5))
HANDLEBARS_MATCHER = /\{\{.*\}\}/
FileUtils.mkdir_p POSTS_DIR
FileUtils.rm Dir.glob("#{POSTS_DIR}/*")
Hubspot::Blog.find_by_id(HUBSPOT_BLOG_ID).posts(created__gt: (Time.now - 100.months), limit: 999).each do |post|
next if post['archived']
slug = post['slug'].split('/').last
title = post['name']
html = Sanitize.fragment(post['post_body'], SANITIZE_CONFIG).gsub(HANDLEBARS_MATCHER, '')
body_markdown = ReverseMarkdown.convert html
image = post['featured_image']
description = post['meta_description']
original_url = post['published_url']
filename = File.join(POSTS_DIR, "#{slug}.md")
puts title
markdown = body_markdown.prepend <<-eof
---
title: "#{title.gsub /"/, '\"'}"
---
eof
File.write filename, markdown
end
source 'https://rubygems.org'
gem 'hubspot-ruby'
gem 'pry'
gem 'reverse_markdown'
gem 'sanitize'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.