class Bridgetown::RubyTemplateView::Helpers
Constants
- Context
Attributes
@return [Bridgetown::Site]
@return [Bridgetown::RubyTemplateView]
Public Class Methods
# File lib/bridgetown-core/helpers.rb, line 17 def initialize(view, site) @view = view @site = site # duck typing for Liquid context @context = Context.new({ site: site }) end
Public Instance Methods
@param pairs [Hash] A hash of key/value pairs.
@return [String] Space-separated keys where the values are truthy.
# File lib/bridgetown-core/helpers.rb, line 32 def class_map(pairs = {}) pairs.select { |_key, truthy| truthy }.keys.join(" ") end
@param relative_path [String] source file path, e.g.
"_posts/2020-10-20-my-post.md"
@raise [ArgumentError] if the file cannot be found
# File lib/bridgetown-core/helpers.rb, line 70 def find_relative_url_for_path(relative_path) site.each_site_file do |item| if item.relative_path == relative_path || item.relative_path == "/#{relative_path}" return safe(item.respond_to?(:relative_url) ? item.relative_url : relative_url(item)) end end raise ArgumentError, <<~MSG Could not find document '#{relative_path}' in 'url_for' helper. Make sure the document exists and the path is correct. MSG end
This helper will generate the correct permalink URL
for the file path.
@param text [String] the content inside the anchor tag @param relative_path [String, Object] source file path, e.g.
"_posts/2020-10-20-my-post.md", or object that responds to `url`
@param options [Hash] key-value pairs of HTML attributes to add to the tag @return [String] the anchor tag HTML @raise [ArgumentError] if the file cannot be found
# File lib/bridgetown-core/helpers.rb, line 92 def link_to(text, relative_path, options = {}) segments = [] segments << "a" segments << "href=\"#{url_for(relative_path)}\"" options.each do |attr, option| attr = attr.to_s.tr("_", "-") segments << "#{attr}=\"#{Utils.xml_escape(option)}\"" end # TODO: this might leak an XSS string into text, need to check safe("<#{segments.join(" ")}>#{text}</a>") end
Convert a Markdown string into HTML output.
@param input [String] the Markdown to convert, if no block is passed @return [String]
# File lib/bridgetown-core/helpers.rb, line 40 def markdownify(input = nil, &block) content = Bridgetown::Utils.reindent_for_markdown( block.nil? ? input.to_s : view.capture(&block) ) converter = site.find_converter_instance(Bridgetown::Converters::Markdown) safe(converter.convert(content).strip) end
For template contexts where ActiveSupport's output safety is loaded, we can ensure a string has been marked safe
@param input [Object] @return [String]
# File lib/bridgetown-core/helpers.rb, line 117 def safe(input) input.to_s.html_safe end
Forward all arguments to I18n.t method
@return [String] the translated string @see I18n
# File lib/bridgetown-core/helpers.rb, line 108 def t(*args) I18n.send :t, *args end
This helper will generate the correct permalink URL
for the file path.
@param relative_path [String, Object] source file path, e.g.
"_posts/2020-10-20-my-post.md", or object that responds to either `url` or `relative_url`
@return [String] the permalink URL
for the file
# File lib/bridgetown-core/helpers.rb, line 54 def url_for(relative_path) if relative_path.respond_to?(:relative_url) return safe(relative_path.relative_url) # new resource engine elsif relative_path.respond_to?(:url) return safe(relative_url(relative_path.url)) # old legacy engine elsif relative_path.start_with?("/", "http") return safe(relative_path) end find_relative_url_for_path(relative_path) end
# File lib/bridgetown-core/helpers.rb, line 25 def webpack_path(asset_type) Bridgetown::Utils.parse_webpack_manifest_file(site, asset_type.to_s) end