forked from home-assistant/home-assistant.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tabbed_block.rb
63 lines (53 loc) · 2.16 KB
/
tabbed_block.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require 'securerandom'
module Jekyll
class TabbedBlock < Liquid::Block
def slug(key)
key.downcase.strip.gsub(' ', '-').gsub(/[^\w\-]/, '')
end
def render_tabbed_block(vars:, converter:, classes: nil, parent_type: nil)
block = Array.new
tabs = Array.new
tabContent = Array.new
uuid = "id" + SecureRandom.hex(10)
tabs << "<div class='tabbed-content-block-tabs'>"
vars.map do |entry|
tabs << "<label onclick='openTab(this)'><input type='radio' name='#{uuid}'><div id='#{uuid}-#{slug(entry['title'])}'>#{entry['title']}</div></label>"
tabContent << "<div id='#{uuid}-#{slug(entry['title'])}' class='tabbed-content-block-content'>#{converter.convert(entry['content'].to_s)}</div>"
end
tabs << "</div>"
block << tabs.join
block << tabContent.join
block.join
end
def render(context)
contents = super(context)
vars = SafeYAML.load(contents)
site = context.registers[:site]
converter = site.find_converter_instance(::Jekyll::Converters::Markdown)
<<~MARKUP
<script>
function openTab(tab){
const tabKey = tab.querySelector("div").id;
const targetTabContent = tab.parentElement.parentElement.querySelector(`#${tabKey}.tabbed-content-block-content`);
const tabContents = tab.parentElement.parentElement.querySelectorAll(".tabbed-content-block-content")
tabContents.forEach((content) => {
content.style.display = "none"
});
targetTabContent.style.display = "block";
}
window.addEventListener('DOMContentLoaded', (event) => {
const tabbedBlocks = document.querySelectorAll(".tabbed-content-block");
tabbedBlocks.forEach((block) => {
block.querySelector("input").checked = true;
block.querySelector(".tabbed-content-block-content").style.display = "block";
});
});
</script>
<div class="tabbed-content-block">
#{render_tabbed_block(vars: vars, converter: converter)}
</div>
MARKUP
end
end
end
Liquid::Template.register_tag('tabbed_block', Jekyll::TabbedBlock)