Skip to content

Commit 7e4d202

Browse files
committed
feat(how-tos): add kong_plugins meta tag
Extract the plugins used in the how-to and add a meta tag with them so that we can filter them in Algolia
1 parent b863541 commit 7e4d202

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

app/_plugins/blocks/entity_examples.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,17 @@ def render(context) # rubocop:disable Metrics/MethodLength
1616
config = config.merge('formats' => @page['tools']) unless config['formats']
1717

1818
unless config['formats']
19-
raise ArgumentError, "Missing key `tools` in metadata, or `formats` in entity_examples block on page #{@page['path']}"
19+
raise ArgumentError,
20+
"Missing key `tools` in metadata, or `formats` in entity_examples block on page #{@page['path']}"
2021
end
2122
unless config['formats'] == ['deck']
2223
raise ArgumentError, "entity_examples only supports deck, on page #{@page['path']}"
2324
end
2425

26+
@page['kong_plugins'] ||= []
27+
kong_plugins = plugins(config)
28+
@page['kong_plugins'].concat(kong_plugins) if kong_plugins.any?
29+
2530
entity_examples_drop = Drops::EntityExamples.new(config:)
2631

2732
template = File.read(entity_examples_drop.template)
@@ -32,12 +37,18 @@ def render(context) # rubocop:disable Metrics/MethodLength
3237
end
3338
rescue Psych::SyntaxError => e
3439
message = <<~STRING
35-
On `#{@page['path']}`, the following {% entity_examples %} block contains a malformed yaml:
36-
#{contents.strip.split("\n").each_with_index.map { |l, i| "#{i}: #{l}" }.join("\n")}
37-
#{e.message}
40+
On `#{@page['path']}`, the following {% entity_examples %} block contains a malformed yaml:
41+
#{contents.strip.split("\n").each_with_index.map { |l, i| "#{i}: #{l}" }.join("\n")}
42+
#{e.message}
3843
STRING
3944
raise ArgumentError.new(message)
4045
end
46+
47+
def plugins(config)
48+
return [] unless config.dig('entities', 'plugins')
49+
50+
config['entities']['plugins'].map { |plugin| plugin['name'] }.compact
51+
end
4152
end
4253
end
4354

app/_plugins/hooks/post_process_html.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,48 @@ def process # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
9898
end
9999
end
100100

101+
class KongPluginsMetaInjector
102+
def initialize(page_or_doc)
103+
@page_or_doc = page_or_doc
104+
end
105+
106+
def process
107+
return unless should_inject?
108+
109+
inject_meta_tag(build_meta_tag)
110+
end
111+
112+
private
113+
114+
def should_inject?
115+
kong_plugins&.any?
116+
end
117+
118+
def kong_plugins
119+
@kong_plugins ||= @page_or_doc.data.fetch('kong_plugins', []).uniq
120+
end
121+
122+
def build_meta_tag
123+
%(<meta name="algolia:kong_plugins" content="#{kong_plugins.join(', ')}">)
124+
end
125+
126+
def inject_meta_tag(meta_tag)
127+
doc = Nokogiri::HTML(@page_or_doc.output)
128+
head = doc.at_css('head')
129+
130+
last_meta = head.css('meta').last
131+
132+
if last_meta
133+
last_meta.add_next_sibling("\n #{meta_tag}")
134+
else
135+
head.prepend_child("#{meta_tag}\n ")
136+
end
137+
138+
@page_or_doc.output = doc.to_html
139+
end
140+
end
141+
101142
Jekyll::Hooks.register [:documents, :pages], :post_render do |page_or_doc|
102143
AddLinksToHeadings.new(page_or_doc).process
144+
KongPluginsMetaInjector.new(page_or_doc).process
103145
end

0 commit comments

Comments
 (0)