jekyll-titles-from-headings-0.5.3/0000755000004100000410000000000013562104530017063 5ustar www-datawww-datajekyll-titles-from-headings-0.5.3/README.md0000644000004100000410000000512513562104530020345 0ustar www-datawww-data# Jekyll Titles from Headings *A Jekyll plugin to pull the page title from the first Markdown heading when none is specified.* [![Build Status](https://travis-ci.org/benbalter/jekyll-titles-from-headings.svg?branch=master)](https://travis-ci.org/benbalter/jekyll-titles-from-headings) ## What it does If you have a Jekyll page that doesn't have a title specified in the YAML Front Matter, but the first non-whitespace line in the page is a Markdown H1 / H2 / H3, this plugin instructs Jekyll to use that first heading as the page's title. ## Why Because lots of plugins and templates rely on `page.title`. If you're using a plugin like [Jekyll Optional Front Matter](https://github.com/benbalter/jekyll-optional-front-matter), you'd have to add Front Matter, just to get the title, which you're already specifying in the document. Additionally, this allows you to store the title semantically, in the document itself so that it's readable, both as Markdown and when rendered, as machine-readable for plugins like [Jekyll SEO Tag](https://github.com/benbalter/jekyll-seo-tag). ## Usage 1. Add the following to your site's Gemfile: ```ruby gem 'jekyll-titles-from-headings' ``` 2. Add the following to your site's config file: ```yml plugins: - jekyll-titles-from-headings ``` Note: If you are using a Jekyll version less than 3.5.0, use the `gems` key instead of `plugins`. ## Configuration Configuration options are optional and placed in `_config.yml` under the `titles_from_headings` key. They default to: ```yml titles_from_headings: enabled: true strip_title: false collections: false ``` ### Stripping titles If your theme renders titles based on `page.title`, you can remove the title from the content by setting `strip_title` to prevent rendering it twice. To limit this behavior to a certain layouts or paths, you can use [front matter defaults](https://jekyllrb.com/docs/configuration/#front-matter-defaults), e.g. ```yml defaults: - scope: path: some-path layout: some_layout values: strip_title: true ``` ### Processing Collections If you want to enable this plugin for collection items, set the `collections` option to `true`. Since collection items (including posts) already have a title inferred from their filename, this option changes the behavior of this plugin to override the inferred title. The inferred title is only used as a fallback in case the document doesn't start with a heading. ### Disabling Even if the plugin is enabled (e.g., via the `:jekyll_plugins` group in your Gemfile) you can disable it by setting the `enabled` key to `false`. jekyll-titles-from-headings-0.5.3/LICENSE.md0000644000004100000410000000210013562104530020460 0ustar www-datawww-dataMIT License Copyright (c) 2016 Ben Balter Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. jekyll-titles-from-headings-0.5.3/lib/0000755000004100000410000000000013562104530017631 5ustar www-datawww-datajekyll-titles-from-headings-0.5.3/lib/jekyll-titles-from-headings/0000755000004100000410000000000013562104530025146 5ustar www-datawww-datajekyll-titles-from-headings-0.5.3/lib/jekyll-titles-from-headings/version.rb0000644000004100000410000000012713562104530027160 0ustar www-datawww-data# frozen_string_literal: true module JekyllTitlesFromHeadings VERSION = "0.5.3" end jekyll-titles-from-headings-0.5.3/lib/jekyll-titles-from-headings/filters.rb0000644000004100000410000000041613562104530027144 0ustar www-datawww-data# frozen_string_literal: true module JekyllTitlesFromHeadings class Filters include Jekyll::Filters include Liquid::StandardFilters def initialize(site) @site = site @context = JekyllTitlesFromHeadings::Context.new(site) end end end jekyll-titles-from-headings-0.5.3/lib/jekyll-titles-from-headings/context.rb0000644000004100000410000000032713562104530027161 0ustar www-datawww-data# frozen_string_literal: true module JekyllTitlesFromHeadings class Context attr_reader :site def initialize(site) @site = site end def registers { :site => site } end end end jekyll-titles-from-headings-0.5.3/lib/jekyll-titles-from-headings/generator.rb0000644000004100000410000000720113562104530027461 0ustar www-datawww-data# frozen_string_literal: true module JekyllTitlesFromHeadings class Generator < Jekyll::Generator attr_accessor :site TITLE_REGEX = %r! \A\s* # Beginning and whitespace (?: # either \#{1,3}\s+(.*)(?:\s+\#{1,3})? # atx-style header | # or (.*)\r?\n[-=]+\s* # Setex-style header )$ # end of line !x.freeze CONVERTER_CLASS = Jekyll::Converters::Markdown STRIP_MARKUP_FILTERS = [:markdownify, :strip_html, :normalize_whitespace].freeze # Regex to strip extra markup still present after markdownify # (footnotes at the moment). EXTRA_MARKUP_REGEX = %r!\[\^[^\]]*\]!.freeze CONFIG_KEY = "titles_from_headings" ENABLED_KEY = "enabled" STRIP_TITLE_KEY = "strip_title" COLLECTIONS_KEY = "collections" safe true priority :lowest def initialize(site) @site = site end def generate(site) @site = site return if disabled? documents = site.pages documents = site.pages + site.docs_to_write if collections? documents.each do |document| next unless should_add_title?(document) next if document.is_a?(Jekyll::StaticFile) document.data["title"] = title_for(document) strip_title!(document) if strip_title?(document) end end def should_add_title?(document) markdown?(document) && !title?(document) end def title?(document) !inferred_title?(document) && !document.data["title"].nil? end def markdown?(document) markdown_converter.matches(document.extname) end def markdown_converter @markdown_converter ||= site.find_converter_instance(CONVERTER_CLASS) end def title_for(document) return document.data["title"] if title?(document) matches = document.content.to_s.match(TITLE_REGEX) return strip_markup(matches[1] || matches[2]) if matches document.data["title"] # If we cant match a title, we use the inferred one. rescue ArgumentError => e raise e unless e.to_s.start_with?("invalid byte sequence in UTF-8") end private def strip_markup(string) STRIP_MARKUP_FILTERS.reduce(string) do |memo, method| filters.public_send(method, memo) end.gsub(EXTRA_MARKUP_REGEX, "") end def option(key) site.config[CONFIG_KEY] && site.config[CONFIG_KEY][key] end def disabled? option(ENABLED_KEY) == false end def strip_title?(document) if document.data.key?(STRIP_TITLE_KEY) document.data[STRIP_TITLE_KEY] == true else option(STRIP_TITLE_KEY) == true end end def strip_title_excerpt?(document) document.is_a?(Jekyll::Document) && document.collection.label == "posts" && document.generate_excerpt? end def collections? option(COLLECTIONS_KEY) == true end # Documents (posts and collection items) have their title inferred from the filename. # We want to override these titles, because they were not excplicitly set. def inferred_title?(document) document.is_a?(Jekyll::Document) end def strip_title!(document) if document.content document.content = document.content.gsub(TITLE_REGEX, "").strip strip_title_excerpt!(document) if strip_title_excerpt?(document) end end def strip_title_excerpt!(document) document.data["excerpt"] = Jekyll::Excerpt.new(document) end def filters @filters ||= JekyllTitlesFromHeadings::Filters.new(site) end end end jekyll-titles-from-headings-0.5.3/lib/jekyll-titles-from-headings.rb0000644000004100000410000000037313562104530025476 0ustar www-datawww-data# frozen_string_literal: true require "jekyll" require "jekyll-titles-from-headings/generator" module JekyllTitlesFromHeadings autoload :Context, "jekyll-titles-from-headings/context" autoload :Filters, "jekyll-titles-from-headings/filters" end jekyll-titles-from-headings-0.5.3/jekyll-titles-from-headings.gemspec0000644000004100000410000000410413562104530025744 0ustar www-datawww-data######################################################### # This file has been automatically generated by gem2tgz # ######################################################### # -*- encoding: utf-8 -*- # stub: jekyll-titles-from-headings 0.5.3 ruby lib Gem::Specification.new do |s| s.name = "jekyll-titles-from-headings".freeze s.version = "0.5.3" s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version= s.require_paths = ["lib".freeze] s.authors = ["Ben Balter".freeze] s.date = "2019-10-23" s.email = ["ben.balter@github.com".freeze] s.files = ["LICENSE.md".freeze, "README.md".freeze, "lib/jekyll-titles-from-headings.rb".freeze, "lib/jekyll-titles-from-headings/context.rb".freeze, "lib/jekyll-titles-from-headings/filters.rb".freeze, "lib/jekyll-titles-from-headings/generator.rb".freeze, "lib/jekyll-titles-from-headings/version.rb".freeze] s.homepage = "https://github.com/benbalter/jekyll-titles-from-headings".freeze s.licenses = ["MIT".freeze] s.rubygems_version = "2.5.2.1".freeze s.summary = "A Jekyll plugin to pull the page title from the first Markdown heading when none is specified.".freeze if s.respond_to? :specification_version then s.specification_version = 4 if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then s.add_runtime_dependency(%q.freeze, ["< 5.0", ">= 3.3"]) s.add_development_dependency(%q.freeze, ["~> 3.5"]) s.add_development_dependency(%q.freeze, ["~> 0.71"]) s.add_development_dependency(%q.freeze, ["~> 0.10"]) else s.add_dependency(%q.freeze, ["< 5.0", ">= 3.3"]) s.add_dependency(%q.freeze, ["~> 3.5"]) s.add_dependency(%q.freeze, ["~> 0.71"]) s.add_dependency(%q.freeze, ["~> 0.10"]) end else s.add_dependency(%q.freeze, ["< 5.0", ">= 3.3"]) s.add_dependency(%q.freeze, ["~> 3.5"]) s.add_dependency(%q.freeze, ["~> 0.71"]) s.add_dependency(%q.freeze, ["~> 0.10"]) end end