Class: Jekyll::Mentions
- Inherits:
-
Object
- Object
- Jekyll::Mentions
- Defined in:
- lib/jekyll-mentions.rb
Constant Summary collapse
- GITHUB_DOT_COM =
"https://github.com"
- BODY_START_TAG =
"<body"
- OPENING_BODY_TAG_REGEX =
%r!<body(.*?)>\s*!
- InvalidJekyllMentionConfig =
Class.new(Jekyll::Errors::FatalException)
Class Method Summary collapse
-
.filter_with_mention(src) ⇒ Object
Public: Create or fetch the filter for the given {src} base URL.
-
.filters ⇒ Object
Public: Filters hash where the key is the mention base URL.
-
.mention_base(config = {}) ⇒ Object
Public: Calculate the base URL to use for mentioning.
- .mention_username_pattern ⇒ Object
-
.mentionable?(doc) ⇒ Boolean
Public: Defines the conditions for a document to be emojiable.
-
.mentionify(doc) ⇒ Object
rubocop:disable Metrics/AbcSize.
Class Method Details
.filter_with_mention(src) ⇒ Object
Public: Create or fetch the filter for the given {src} base URL.
src - the base URL (e.g. github.com)
Returns an HTML::Pipeline instance for the given base URL.
41 42 43 44 45 |
# File 'lib/jekyll-mentions.rb', line 41 def filter_with_mention(src) filters[src] ||= HTML::Pipeline.new([ HTML::Pipeline::MentionFilter, ], { :base_url => src, :username_pattern => mention_username_pattern }) end |
.filters ⇒ Object
Public: Filters hash where the key is the mention base URL. Effectively a cache.
53 54 55 |
# File 'lib/jekyll-mentions.rb', line 53 def filters @filters ||= {} end |
.mention_base(config = {}) ⇒ Object
Public: Calculate the base URL to use for mentioning. The custom base URL can be defined in the config as jekyll-mentions.base_url or jekyll-mentions, and must be a valid URL (i.e. it must include a protocol and valid domain) It should not have a trailing slash.
config - the hash-like configuration of the document’s site
Returns a URL to use as the base URL for mentions. Defaults to the github.com.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/jekyll-mentions.rb', line 67 def mention_base(config = {}) mention_config = config["jekyll-mentions"] case mention_config when nil, NilClass default_mention_base when String mention_config.to_s when Hash mention_config.fetch("base_url", default_mention_base) else raise InvalidJekyllMentionConfig, "Your jekyll-mentions config has to either be a" \ " string or a hash. It's a #{mention_config.class} right now." end end |
.mention_username_pattern ⇒ Object
47 48 49 |
# File 'lib/jekyll-mentions.rb', line 47 def mention_username_pattern @mention_username_pattern ||= %r![\w][\w-]*! end |
.mentionable?(doc) ⇒ Boolean
Public: Defines the conditions for a document to be emojiable.
doc - the Jekyll::Document or Jekyll::Page
Returns true if the doc is written & is HTML.
88 89 90 91 |
# File 'lib/jekyll-mentions.rb', line 88 def mentionable?(doc) (doc.is_a?(Jekyll::Page) || doc.write?) && doc.output_ext == ".html" || (doc.permalink&.end_with?("/")) end |
.mentionify(doc) ⇒ Object
rubocop:disable Metrics/AbcSize
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/jekyll-mentions.rb', line 17 def mentionify(doc) content = doc.output return unless content.include?("@") src = mention_base(doc.site.config) if content.include? BODY_START_TAG head, opener, tail = content.partition(OPENING_BODY_TAG_REGEX) body_content, *rest = tail.partition("</body>") return unless body_content =~ filter_regex processed_markup = filter_with_mention(src).call(body_content)[:output].to_s doc.output = String.new(head) << opener << processed_markup << rest.join else return unless content =~ filter_regex doc.output = filter_with_mention(src).call(content)[:output].to_s end end |