class JekyllAdmin::Server

Constants

EXTENSIONS

supported extensions, in order of preference, for now, no .csv

META_KEYS

Jekyll::Site instance method names that return a Hash.

ROUTES

Public Instance Methods

collection() click to toggle source
# File lib/jekyll-admin/server/collections.rb, line 50
def collection
  collection = site.collections.find { |l, _c| l == params["collection_id"] }
  collection[1] if collection
end
configuration() click to toggle source

Computed configuration, with updates and defaults Returns an instance of Jekyll::Configuration

# File lib/jekyll-admin/server/configuration.rb, line 28
def configuration
  @configuration ||= site.config.merge(overrides)
end
configuration_body() click to toggle source

The user's uploaded configuration for updates Instead of extracting `raw_content` directly from the `request_payload`,

assign the data to a new variable and then extract the `raw_content`
from it to circumvent CORS violation in `development` mode.
# File lib/jekyll-admin/server/configuration.rb, line 54
def configuration_body
  payload = request_payload
  payload["raw_content"]
end
configuration_path() click to toggle source

Returns the path to the first config file discovered

# File lib/jekyll-admin/server/configuration.rb, line 46
def configuration_path
  sanitized_path configuration.config_files(overrides).first
end
data_file_body() click to toggle source
# File lib/jekyll-admin/server/data.rb, line 71
def data_file_body
  if !request_payload["raw_content"].to_s.empty?
    request_payload["raw_content"]
  elsif !request_payload["content"].to_s.empty?
    YAML.dump(request_payload["content"]).sub(%r!\A---\n!, "")
  end
end
directory_docs() click to toggle source
# File lib/jekyll-admin/server/collections.rb, line 60
def directory_docs
  collection.docs.find_all { |d| File.dirname(d.path) == directory_path }
end
directory_drafts() click to toggle source

return drafts at the same level as directory

# File lib/jekyll-admin/server/drafts.rb, line 49
def directory_drafts
  drafts.find_all { |d| File.dirname(d.path) == directory_path }
end
directory_files() click to toggle source
# File lib/jekyll-admin/server/static_files.rb, line 49
def directory_files
  static_files.find_all do |file|
    sanitized_path(File.dirname(file.path)) == directory_path
  end
end
directory_pages() click to toggle source
# File lib/jekyll-admin/server/data.rb, line 44
def directory_pages
  DataFile.all.find_all do |p|
    sanitized_path(File.dirname(p.path)) == directory_path
  end
end
directory_paths() click to toggle source

returns relative path of root level directories that contain data files

# File lib/jekyll-admin/server/data.rb, line 40
def directory_paths
  DataFile.all.map { |p| File.dirname(p.relative_path).split("/")[0] }.uniq
end
dirs_at_root() click to toggle source

returns relative path of root level directories that contain static files

# File lib/jekyll-admin/server/static_files.rb, line 43
def dirs_at_root
  static_files.map do |file|
    File.dirname(file.path.sub("#{site.source}/", "")).split("/")[0]
  end.uniq
end
document_id() click to toggle source
# File lib/jekyll-admin/server/collections.rb, line 55
def document_id
  path = "#{params["splat"].first}/#{filename}"
  path.gsub(%r!(\d{4})/(\d{2})/(\d{2})/(.*)!, '\1-\2-\3-\4')
end
drafts() click to toggle source

return all documents in the 'posts' collection that output to an html file but reside in a separate directory, `<source_dir>/_drafts/`

# File lib/jekyll-admin/server/drafts.rb, line 43
def drafts
  posts = site.collections.find { |l, _c| l == "posts" }
  posts[1].docs.find_all { |d| d.output_ext == ".html" && d.draft? } if posts
end
ensure_collection() click to toggle source
# File lib/jekyll-admin/server/collections.rb, line 64
def ensure_collection
  render_404 if collection.nil?
end
ensure_directory() click to toggle source
# File lib/jekyll-admin/server/collections.rb, line 68
def ensure_directory
  ensure_collection
  render_404 unless Dir.exist?(directory_path)
end
ensure_drafts() click to toggle source
# File lib/jekyll-admin/server/drafts.rb, line 71
def ensure_drafts
  render_404 if drafts.nil?
end
ensure_html_content() click to toggle source
# File lib/jekyll-admin/server/drafts.rb, line 75
def ensure_html_content
  return if html_content?

  content_type :json
  halt 422, json("error_message" => "Invalid file extension for drafts")
end
entries() click to toggle source
# File lib/jekyll-admin/server/collections.rb, line 73
def entries
  collections_dir = site.config["collections_dir"]
  collection_id, splats = params.values_at("collection_id", "splat")
  args = {
    :base         => site.in_source_dir(collections_dir, "_#{collection_id}"),
    :content_type => collection_id,
    :splat        => splats&.first,
  }
  # get the directories inside the requested directory
  directory = JekyllAdmin::Directory.new(directory_path, **args)
  directories = directory.directories
  # merge directories with the documents at the same level
  directories.concat(directory_docs.sort_by(&:date).reverse)
end
html_content?() click to toggle source
# File lib/jekyll-admin/server/drafts.rb, line 102
def html_content?
  draft = Jekyll::PageWithoutAFile.new(
    site,
    site.source,
    "_drafts",
    request_payload["path"] || filename
  )
  draft.data = request_payload["front_matter"]
  draft.html?
end
html_page_at_source?(page) click to toggle source

Test if a given page is *physically located* within the source directory and outputs to an HTML file. We don't want *virtual pages* that are generated via plugins.

# File lib/jekyll-admin/server/pages.rb, line 66
def html_page_at_source?(page)
  return false unless page.html?

  # If page is not an instance of a `Jekyll::Page` subclass, then it needs additional
  # inspection.
  # Can't use `is_a?(Jekyll::Page)` here because it returns true for subclass instances
  return true if page.class == Jekyll::Page

  File.exist?(site.in_source_dir(page.relative_path))
end
overrides() click to toggle source
# File lib/jekyll-admin/server/configuration.rb, line 20
def overrides
  @overrides ||= {
    "source" => sanitized_path("/"),
  }
end
pages() click to toggle source
# File lib/jekyll-admin/server/pages.rb, line 59
def pages
  site.pages.select { |p| html_page_at_source?(p) }
end
parsed_configuration() click to toggle source

Configuration data, as read by Jekyll

# File lib/jekyll-admin/server/configuration.rb, line 33
def parsed_configuration
  configuration.read_config_file(configuration_path)
end
raw_configuration() click to toggle source

Raw configuration content, as it sits on disk

# File lib/jekyll-admin/server/configuration.rb, line 38
def raw_configuration
  File.read(
    configuration_path,
    **Jekyll::Utils.merged_file_read_opts(site, {})
  )
end
relative_draft_path(document) click to toggle source
# File lib/jekyll-admin/server/drafts.rb, line 62
def relative_draft_path(document)
  File.dirname(document.relative_path.sub("_drafts/", ""))
end
relevant_directory_paths() click to toggle source

returns directories at the root of `/_drafts/` that contain drafts

# File lib/jekyll-admin/server/drafts.rb, line 58
def relevant_directory_paths
  drafts.map { |doc| relative_draft_path(doc).split("/")[0] }.uniq
end
reverse_sorted_drafts() click to toggle source
# File lib/jekyll-admin/server/drafts.rb, line 53
def reverse_sorted_drafts
  directory_drafts.sort_by(&:date).reverse
end
site_meta() click to toggle source

Stash a Hash generated with pre-determined keys.

# File lib/jekyll-admin/server/site_meta.rb, line 17
def site_meta
  @site_meta ||= META_KEYS.zip(META_KEYS.map { |k| site.send(k).keys }).to_h
end
splats() click to toggle source
# File lib/jekyll-admin/server/data.rb, line 79
def splats
  params["splat"] || ["/"]
end
static_file_body() click to toggle source
# File lib/jekyll-admin/server/static_files.rb, line 71
def static_file_body
  raw_content = request_payload["raw_content"]
  return raw_content if raw_content.is_a?(String) && raw_content != ""

  Base64.decode64 request_payload["encoded_content"].to_s
end
static_files() click to toggle source
# File lib/jekyll-admin/server/static_files.rb, line 78
def static_files
  site.static_files.select { |f| f.path.start_with? site.source }
end

Private Instance Methods

base_url() click to toggle source
# File lib/jekyll-admin/server.rb, line 59
def base_url
  "#{request.scheme}://#{request.host_with_port}"
end
document_body() click to toggle source
# File lib/jekyll-admin/server.rb, line 67
def document_body
  body = +""
  body << if front_matter && !front_matter.empty?
            YAML.dump(restored_front_matter).strip
              .gsub(": 'null'", ": null") # restore null values
          else
            "---"
          end
  body << "\n---\n\n"
  body << request_payload["raw_content"].to_s
  body << "\n" unless body.end_with?("\n")
  body
end
Also aliased as: page_body
front_matter() click to toggle source
# File lib/jekyll-admin/server.rb, line 63
def front_matter
  request_payload["front_matter"]
end
json(object, options = {}) click to toggle source
# File lib/jekyll-admin/server.rb, line 36
def json(object, options = {})
  content_type :json
  JSON.pretty_generate(object, options)
end
namespace() click to toggle source
# File lib/jekyll-admin/server.rb, line 89
def namespace
  namespace = request.path_info.split("/")[1].to_s.downcase
  namespace if ROUTES.include?(namespace)
end
page_body()
Alias for: document_body
render_404() click to toggle source
# File lib/jekyll-admin/server.rb, line 45
def render_404
  status 404
  content_type :json
  halt
end
request_body() click to toggle source
# File lib/jekyll-admin/server.rb, line 82
def request_body
  @request_body ||= begin
    request.body.rewind
    request.body.read
  end
end
request_payload() click to toggle source
# File lib/jekyll-admin/server.rb, line 51
def request_payload
  @request_payload ||= if request_body.to_s.empty?
                         {}
                       else
                         JSON.parse(request_body)
                       end
end
restored_front_matter() click to toggle source

verbose 'null' values in front matter

# File lib/jekyll-admin/server.rb, line 95
def restored_front_matter
  front_matter.map do |key, value|
    value = "null" if value.nil?
    [key, value]
  end.to_h
end
site() click to toggle source
# File lib/jekyll-admin/server.rb, line 41
def site
  JekyllAdmin.site
end