Skip to content

Commit

Permalink
Merge pull request #154 from jekyll/remove-adjacent
Browse files Browse the repository at this point in the history
Exclude next/previous contents from indexes
  • Loading branch information
benbalter committed Aug 20, 2016
2 parents 77e3362 + 54cf646 commit 342b009
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 19 deletions.
76 changes: 58 additions & 18 deletions lib/jekyll-admin/apiable.rb
Expand Up @@ -2,6 +2,9 @@ module JekyllAdmin
# Abstract module to be included in Convertible and Document to provide
# additional, API-specific functionality without duplicating logic
module APIable

CONTENT_FIELDS = %w(next previous content excerpt).freeze

# Returns a hash suitable for use as an API response.
#
# For Documents and Pages:
Expand All @@ -13,26 +16,19 @@ module APIable
#
# Options:
#
# content - if true, includes the content in the respond, false by default
# to support mapping on indexes where we only want metadata
# include_content - if true, includes the content in the respond, false by default
# to support mapping on indexes where we only want metadata
#
# Returns a hash (which can then be to_json'd)
def to_api(include_content: false)
output = to_liquid.to_h

if include_content && File.exist?(file_path)
if is_a?(Jekyll::StaticFile)
output["encoded_content"] = encoded_content
elsif is_a?(JekyllAdmin::DataFile)
output["content"] = content
output["raw_content"] = raw_content
else
output["raw_content"] = raw_content
output["front_matter"] = front_matter
end
end
output = hash_for_api

output.delete("content") unless include_content
# Include content, if requested, otherwise remove it
if include_content
output = output.merge(content_fields)
else
CONTENT_FIELDS.each { |field| output.delete(field) }
end

# Documents have duplicate output and content fields, Pages do not
# Since it's an API, use `content` in both for consistency
Expand Down Expand Up @@ -66,14 +62,15 @@ def name
end

def file_contents
@file_contents ||= File.read(file_path, file_read_options)
@file_contents ||= File.read(file_path, file_read_options) if file_exists?
end

def file_read_options
Jekyll::Utils.merged_file_read_opts(site, {})
end

def front_matter
return unless file_exists?
@front_matter ||= if file_contents =~ Jekyll::Document::YAML_FRONT_MATTER_REGEXP
SafeYAML.load(Regexp.last_match(1))
else
Expand All @@ -82,6 +79,7 @@ def front_matter
end

def raw_content
return unless file_exists?
@raw_content ||= if file_contents =~ Jekyll::Document::YAML_FRONT_MATTER_REGEXP
$POSTMATCH
else
Expand All @@ -90,7 +88,49 @@ def raw_content
end

def encoded_content
@encoded_content ||= Base64.encode64(file_contents)
@encoded_content ||= Base64.encode64(file_contents) if file_exists?
end

def file_exists?
return @file_exists if defined? @file_exists
@file_exists = File.exist?(file_path)
end

# Base hash from which to generate the API output
def hash_for_api
output = to_liquid
if output.respond_to?(:hash_for_json)
output.hash_for_json
else
output.to_h
end
end

# Returns a hash of content fields for inclusion in the API output
def content_fields
output = {}

# Include file content-related fields
if is_a?(Jekyll::StaticFile)
output["encoded_content"] = encoded_content
elsif is_a?(JekyllAdmin::DataFile)
output["content"] = content
output["raw_content"] = raw_content
else
output["raw_content"] = raw_content
output["front_matter"] = front_matter
end

# Include next and previous documents non-recursively
if is_a?(Jekyll::Document)
%w(next previous).each do |direction|
method = "#{direction}_doc".to_sym
doc = public_send(method)
output[direction] = doc.to_api if doc
end
end

output
end
end
end
5 changes: 5 additions & 0 deletions spec/fixtures/site/_posts/2016-02-01-test.md
@@ -0,0 +1,5 @@
---
foo: bar
---

# Test Post2
5 changes: 5 additions & 0 deletions spec/fixtures/site/_posts/2016-03-01-test.md
@@ -0,0 +1,5 @@
---
foo: bar
---

# Test Post3
28 changes: 27 additions & 1 deletion spec/jekyll-admin/server/collection_spec.rb
Expand Up @@ -45,13 +45,20 @@ def app
expect(last_response_parsed.first).to_not have_key("front_matter")
end

it "deoesn't include content in the index" do
it "doesn't include content in the index" do
get "/collections/posts/documents"
expect(last_response).to be_ok
expect(last_response_parsed.first).to_not have_key("content")
expect(last_response_parsed.first).to_not have_key("raw_content")
expect(last_response_parsed.first).to_not have_key("output")
end

it "doesn't include next/previous in the index" do
get "/collections/posts/documents"
expect(last_response).to be_ok
expect(last_response_parsed.first).to_not have_key("next")
expect(last_response_parsed.first).to_not have_key("previous")
end
end

it "404s for an unknown collection" do
Expand Down Expand Up @@ -91,6 +98,25 @@ def app
expect(last_response_parsed["raw_content"]).to eq("# Test Post\n")
end

%w(next previous).each do |direction|
it "includes the #{direction} document non-recursively" do
get "/collections/posts/2016-02-01-test.md"
expect(last_response).to be_ok
expect(last_response_parsed).to have_key(direction)
expect(last_response_parsed[direction]).to_not have_key("next")
expect(last_response_parsed[direction]).to_not have_key("previous")
end

it "doesn't include the #{direction} document's content" do
get "/collections/posts/2016-02-01-test.md"
expect(last_response).to be_ok
expect(last_response_parsed).to have_key(direction)
expect(last_response_parsed[direction]).to_not have_key("content")
expect(last_response_parsed[direction]).to_not have_key("raw_content")
expect(last_response_parsed[direction]).to_not have_key("output")
end
end

context "front matter" do
let(:front_matter) { last_response_parsed["front_matter"] }

Expand Down

0 comments on commit 342b009

Please sign in to comment.