1
0
mirror of https://github.com/danbee/micropub.git synced 2025-03-04 08:59:13 +00:00

Accept a JSON content hash and convert HTML to markdown

This commit is contained in:
Daniel Barber 2019-11-16 17:17:04 -05:00
parent 084511e8b8
commit bcd2180438
Signed by: danbarber
GPG Key ID: 931D8112E0103DD8
6 changed files with 79 additions and 9 deletions

14
Gemfile
View File

@ -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

View File

@ -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

29
bin/kramdown Executable file
View File

@ -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")

View File

@ -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

View File

@ -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" => "<p>Hello, World!</p>" })
_(post.content.strip).must_equal "Hello, World!"
end
end
describe "#post_content" do

View File

@ -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: "<p>Hello, World!</p>" },
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