diff --git a/Gemfile b/Gemfile index bf48e21..8e8404f 100644 --- a/Gemfile +++ b/Gemfile @@ -4,20 +4,18 @@ ruby "2.6.5" gem "dotenv" +gem "faraday" +gem "github_api" +gem "indieweb-endpoints" +gem "kramdown" +gem "puma" gem "sinatra" gem "sinatra-contrib" -gem "puma" - -gem "faraday" - -gem "github_api" - -gem "indieweb-endpoints" group :test do - gem "pry" gem "minitest" gem "minitest-hooks" gem "mocha" + gem "pry" gem "rack-test" end diff --git a/Gemfile.lock b/Gemfile.lock index ba6571c..d943728 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -42,6 +42,7 @@ GEM link-header-parser (~> 0.2.0) nokogiri (~> 1.10) jwt (2.2.1) + kramdown (2.1.0) link-header-parser (0.2.0) absolutely (~> 3.0) metaclass (0.0.4) @@ -103,6 +104,7 @@ DEPENDENCIES faraday github_api indieweb-endpoints + kramdown minitest minitest-hooks mocha diff --git a/bin/kramdown b/bin/kramdown new file mode 100755 index 0000000..73320ec --- /dev/null +++ b/bin/kramdown @@ -0,0 +1,29 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# +# This file was generated by Bundler. +# +# The application 'kramdown' is installed as part of a gem, and +# this file is here to facilitate running it. +# + +require "pathname" +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", + Pathname.new(__FILE__).realpath) + +bundle_binstub = File.expand_path("../bundle", __FILE__) + +if File.file?(bundle_binstub) + if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/ + load(bundle_binstub) + else + abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run. +Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.") + end +end + +require "rubygems" +require "bundler/setup" + +load Gem.bin_path("kramdown", "kramdown") diff --git a/lib/micropub/models/post.rb b/lib/micropub/models/post.rb index 36893fb..9109c78 100644 --- a/lib/micropub/models/post.rb +++ b/lib/micropub/models/post.rb @@ -1,4 +1,5 @@ require "date" +require "kramdown" class Post attr_accessor :params @@ -48,7 +49,21 @@ class Post end def content - params["content"] + if params["content"].is_a?(Hash) + content_from_hash + else + params["content"] + end + end + + def content_from_hash + if params["content"]["text"] + params["content"]["text"] + elsif params["content"]["html"] + Kramdown::Document. + new(params["content"]["html"], html_to_native: true). + to_kramdown + end end def post_content diff --git a/test/lib/micropub/models/post_test.rb b/test/lib/micropub/models/post_test.rb index d79a698..8686adc 100644 --- a/test/lib/micropub/models/post_test.rb +++ b/test/lib/micropub/models/post_test.rb @@ -130,6 +130,18 @@ describe Post do _(post.content).must_equal "Hello, World!" end + + it "accepts content in an object" do + post = Post.new("content" => { "text" => "Hello, World!" }) + + _(post.content).must_equal "Hello, World!" + end + + it "converts HTML content to Markdown" do + post = Post.new("content" => { "html" => "
Hello, World!
" }) + + _(post.content.strip).must_equal "Hello, World!" + end end describe "#post_content" do diff --git a/test/requests/create_post_test.rb b/test/requests/create_post_test.rb index 4adb149..3b5faf0 100644 --- a/test/requests/create_post_test.rb +++ b/test/requests/create_post_test.rb @@ -73,4 +73,18 @@ describe "create post" do assert_equal last_response.headers["Location"], "https://test.danbarber.me/blog/#{date}/hello-world" end + + it "creates a post with JSON and HTML content" do + post_json = { + content: { html: "Hello, World!
" }, + category: ["one", "two", "three"], + }.to_json + + post "/micropub/main", post_json, { "CONTENT_TYPE" => "application/json" } + + date = Time.now.strftime("%Y/%m/%d") + assert last_response.accepted? + assert_equal last_response.headers["Location"], + "https://test.danbarber.me/blog/#{date}/hello-world" + end end