-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathsyntax_highlight.rb
More file actions
67 lines (55 loc) · 1.91 KB
/
syntax_highlight.rb
File metadata and controls
67 lines (55 loc) · 1.91 KB
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
64
65
66
67
# frozen_string_literal: true
module Kramdown
module Converter
class Html # rubocop:disable Style/Documentation
alias original_convert_codeblock convert_codeblock
def convert_codeblock(el, indent)
render_shiki(el, indent)
end
def render_shiki(el, _indent) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Naming/MethodParameterName
language = extract_code_language!(el.attr) || 'text'
data = data_attributes(el.attr)
copy = copy?(el.attr)
code = el.value
id = SecureRandom.uuid
snippet = CodeHighlighter.new.highlight(code, language, id)
Liquid::Template.parse(template, { line_numbers: true }).render(
{
'codeblock' => {
'copy' => copy,
'css_classes' => el.attr['class'],
'collapsible' => el.attr.fetch('class', '').include?('collapsible'),
'ask_kai' => ask_kai(data, code),
'render_header' => !data['data-file'].nil? || !ask_kai(data, code).nil?,
'id' => id,
'data' => data,
'snippet' => snippet
}
},
context
)
end
def template
@template ||= File.read(File.expand_path('app/_includes/syntax_highlighting.html'))
end
def context
@context = Liquid::Context.new(site, {}, {})
end
def site
@site ||= Jekyll.sites.first
end
def copy?(attr)
!attr.fetch('class', '').include?('no-copy-code')
end
def data_attributes(attr)
@data_attributes = attr.each_with_object({}) do |(key, value), data|
data[key] = value if key.start_with?('data-')
end
end
def ask_kai(data, code)
return nil if data['data-ask-kai'].nil?
"https://cloud.konghq.com/?agent=true&agent-prompt=#{URI.encode_www_form_component(code)}"
end
end
end
end