jekyll-titles-from-headings 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9be84447c8ef2d9c7524d0597ed0554f86921400
4
- data.tar.gz: fcea154a73a1c77c91fc474d55f72304ae207f26
3
+ metadata.gz: 72fa914e6e3a049002a1f8b7ea2e9e27e7e72828
4
+ data.tar.gz: c40c43c7606cf23b0b78c70bc90ec1df5f65bb77
5
5
  SHA512:
6
- metadata.gz: 86d521565b46e9113f816d7713080232bdb3b77be9504dd05c61b2f6c9c4b7fc538e55230a9576907edccc7b643d1df406cafa44bddbb3d7303632745105032c
7
- data.tar.gz: 895456a1a6e7f80c6726f932acabc0ad2a131b8cc5ab48e168b2467ffbda1632413b60d914bacbbc32bf706bde057d168f56a9127850d7c32b61961959ea17bb
6
+ metadata.gz: 12b31cbcc1a5bf70721e1c614a908c49c42290ae902b133ae64848af385c2e7f7b9f007d87d234929037b1f158588e2c156a6323361108949c1172f16e290ed1
7
+ data.tar.gz: 730f1cad5650da5490e37043ac26b664da99a228a2bc277e9e938195d94f14b7c349fc9d4bbeb6a12893c7b9a644367cf3113d012cbbd77a82821ddff41344a9
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
- # Jekyll Title from Headings
1
+ # Jekyll Titles from Headings
2
2
 
3
3
  *A Jekyll plugin to pull the page title from the first Markdown heading when none is specified.*
4
4
 
5
- [![Build Status](https://travis-ci.org/benbalter/jekyll-title-from-headings.svg?branch=master)](https://travis-ci.org/benbalter/jekyll-title-from-headings)
5
+ [![Build Status](https://travis-ci.org/benbalter/jekyll-titles-from-headings.svg?branch=master)](https://travis-ci.org/benbalter/jekyll-titles-from-headings)
6
6
 
7
7
  ## What it does
8
8
 
@@ -30,3 +30,39 @@ Additionally, this allows you to store the title semantically, in the document i
30
30
  gems:
31
31
  - jekyll-titles-from-headings
32
32
  ```
33
+
34
+ ## Configuration
35
+
36
+ Configuration options are optional and placed in `_config.yml` under the `titles_from_headings` key. They default to:
37
+
38
+ ```yml
39
+ titles_from_headings:
40
+ enabled: true
41
+ strip_title: false
42
+ collections: false
43
+ ```
44
+
45
+ ### Stripping titles
46
+
47
+ 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.
48
+
49
+ 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.
50
+
51
+ ```yml
52
+ defaults:
53
+ - scope:
54
+ path: some-path
55
+ layout: some_layout
56
+ values:
57
+ strip_title: true
58
+ ```
59
+
60
+ ### Processing Collections
61
+
62
+ If you want to enable this plugin for collection items, set the `collections` option to `true`.
63
+
64
+ 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.
65
+
66
+ ### Disabling
67
+
68
+ 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`.
@@ -20,6 +20,11 @@ module JekyllTitlesFromHeadings
20
20
  # (footnotes at the moment).
21
21
  EXTRA_MARKUP_REGEX = %r!\[\^[^\]]*\]!
22
22
 
23
+ CONFIG_KEY = "titles_from_headings".freeze
24
+ ENABLED_KEY = "enabled".freeze
25
+ STRIP_TITLE_KEY = "strip_title".freeze
26
+ COLLECTIONS_KEY = "collections".freeze
27
+
23
28
  safe true
24
29
  priority :lowest
25
30
 
@@ -29,10 +34,15 @@ module JekyllTitlesFromHeadings
29
34
 
30
35
  def generate(site)
31
36
  @site = site
37
+ return if disabled?
38
+
39
+ documents = site.pages
40
+ documents = site.pages + site.docs_to_write if collections?
32
41
 
33
- site.pages.each do |document|
42
+ documents.each do |document|
34
43
  next unless should_add_title?(document)
35
44
  document.data["title"] = title_for(document)
45
+ strip_title!(document) if strip_title?(document)
36
46
  end
37
47
  end
38
48
 
@@ -41,7 +51,7 @@ module JekyllTitlesFromHeadings
41
51
  end
42
52
 
43
53
  def title?(document)
44
- !document.data["title"].nil?
54
+ !inferred_title?(document) && !document.data["title"].nil?
45
55
  end
46
56
 
47
57
  def markdown?(document)
@@ -55,7 +65,8 @@ module JekyllTitlesFromHeadings
55
65
  def title_for(document)
56
66
  return document.data["title"] if title?(document)
57
67
  matches = document.content.match(TITLE_REGEX)
58
- strip_markup(matches[1] || matches[2]) if matches
68
+ return strip_markup(matches[1] || matches[2]) if matches
69
+ document.data["title"] # If we cant match a title, we use the inferred one.
59
70
  rescue ArgumentError => e
60
71
  raise e unless e.to_s.start_with?("invalid byte sequence in UTF-8")
61
72
  end
@@ -68,6 +79,36 @@ module JekyllTitlesFromHeadings
68
79
  end.gsub(EXTRA_MARKUP_REGEX, "")
69
80
  end
70
81
 
82
+ def option(key)
83
+ site.config[CONFIG_KEY] && site.config[CONFIG_KEY][key]
84
+ end
85
+
86
+ def disabled?
87
+ option(ENABLED_KEY) == false
88
+ end
89
+
90
+ def strip_title?(document)
91
+ if document.data.key?(STRIP_TITLE_KEY)
92
+ document.data[STRIP_TITLE_KEY] == true
93
+ else
94
+ option(STRIP_TITLE_KEY) == true
95
+ end
96
+ end
97
+
98
+ def collections?
99
+ option(COLLECTIONS_KEY) == true
100
+ end
101
+
102
+ # Documents (posts and collection items) have their title inferred from the filename.
103
+ # We want to override these titles, because they were not excplicitly set.
104
+ def inferred_title?(document)
105
+ document.is_a?(Jekyll::Document)
106
+ end
107
+
108
+ def strip_title!(document)
109
+ document.content.gsub!(TITLE_REGEX, "")
110
+ end
111
+
71
112
  def filters
72
113
  @filters ||= JekyllTitlesFromHeadings::Filters.new(site)
73
114
  end
@@ -1,3 +1,3 @@
1
1
  module JekyllTitlesFromHeadings
2
- VERSION = "0.4.0".freeze
2
+ VERSION = "0.5.0".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-titles-from-headings
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Balter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-08-11 00:00:00.000000000 Z
11
+ date: 2017-09-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll