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 +4 -4
- data/README.md +38 -2
- data/lib/jekyll-titles-from-headings/generator.rb +44 -3
- data/lib/jekyll-titles-from-headings/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 72fa914e6e3a049002a1f8b7ea2e9e27e7e72828
|
4
|
+
data.tar.gz: c40c43c7606cf23b0b78c70bc90ec1df5f65bb77
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 12b31cbcc1a5bf70721e1c614a908c49c42290ae902b133ae64848af385c2e7f7b9f007d87d234929037b1f158588e2c156a6323361108949c1172f16e290ed1
|
7
|
+
data.tar.gz: 730f1cad5650da5490e37043ac26b664da99a228a2bc277e9e938195d94f14b7c349fc9d4bbeb6a12893c7b9a644367cf3113d012cbbd77a82821ddff41344a9
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
|
-
# Jekyll
|
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-
|
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
|
-
|
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
|
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
|
+
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-
|
11
|
+
date: 2017-09-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: jekyll
|