Skip to content

Commit 21a04f9

Browse files
fix the clean_html file for optimizations
1 parent 15fcdc2 commit 21a04f9

File tree

1 file changed

+8
-53
lines changed

1 file changed

+8
-53
lines changed

lib/docs/filters/threejs/clean_html.rb

Lines changed: 8 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ class CleanHtmlFilter < Filter
44
def call
55
# Remove unnecessary elements
66
css('head, script, style').remove
7+
# Wrap code blocks with pre tags and add syntax highlighting
8+
css('code').each do |node|
9+
unless node.parent.name == 'pre'
10+
pre = node.wrap('<pre>')
11+
pre['data-language'] = 'javascript'
12+
pre['class'] = 'language-javascript'
13+
end
14+
end
715

816
# Handle source links
917
css('h2').each do |node|
@@ -202,39 +210,13 @@ def call
202210
end
203211
end
204212

205-
# Clean up code blocks
206-
css('pre').each do |node|
207-
wrapper = doc.document.create_element('div')
208-
wrapper['class'] = 'highlight'
209-
node.replace(wrapper)
210-
211-
div = doc.document.create_element('div')
212-
div['class'] = 'highlight-javascript notranslate'
213-
wrapper.add_child(div)
214-
215-
pre = doc.document.create_element('pre')
216-
pre['class'] = ''
217-
div.add_child(pre)
218-
219-
# Format the code content
220-
code = node.content.strip
221-
222-
# Add syntax highlighting spans
223-
highlighted_code = highlight_javascript(code)
224-
225-
pre.inner_html = highlighted_code
226-
end
227-
228213
# Add proper heading IDs and classes
229214
css('h1, h2, h3, h4').each do |node|
230215
node['id'] ||= node.content.strip.downcase.gsub(/[^\w]+/, '-')
231216
existing_class = node['class'].to_s
232217
node['class'] = "#{existing_class} section-header"
233218
end
234219

235-
# Remove interactive examples
236-
css('.threejs_example_container').remove
237-
238220
# Add note styling
239221
css('p').each do |node|
240222
if node.content.start_with?('Note:')
@@ -254,35 +236,8 @@ def call
254236
node.replace(wrapper)
255237
end
256238
end
257-
258-
# Remove the navigation arrows and links
259-
css('.nav').remove if at_css('.nav')
260-
# If the arrows are in a different container, adjust the selector accordingly
261-
262239
doc
263240
end
264-
265-
private
266-
267-
def highlight_javascript(code)
268-
code = code.gsub(/\b(function|return|var|let|const|if|else|for|while|do|switch|case|break|continue|new|try|catch|throw|this|typeof|instanceof|in|of|class|extends|super|import|export|default|null|undefined|true|false)\b/, '<span class="k">\1</span>') # keywords
269-
code = code.gsub(/\b(\d+(\.\d+)?)\b/, '<span class="mi">\1</span>') # numbers
270-
code = code.gsub(/'([^']*)'/, '<span class="s1">\'\1\'</span>') # single quotes
271-
code = code.gsub(/"([^"]*)"/, '<span class="s2">"\1"</span>') # double quotes
272-
code = code.gsub(/`([^`]*)`/, '<span class="s2">`\1`</span>') # template literals
273-
code = code.gsub(/\/\/[^\n]*/, '<span class="c1">\0</span>') # single line comments
274-
code = code.gsub(/\b(console|document|window|Array|Object|String|Number|Boolean|Function|Symbol|Map|Set|Promise|async|await)\b/, '<span class="nb">\1</span>') # built-ins
275-
code = code.gsub(/([a-zA-Z_$][a-zA-Z0-9_$]*)\s*\(/, '<span class="nx">\1</span>(') # function calls
276-
code = code.gsub(/\b(addEventListener|querySelector|getElementById|setTimeout|setInterval)\b/, '<span class="nx">\1</span>') # common methods
277-
278-
# Add line numbers
279-
lines = code.split("\n")
280-
numbered_lines = lines.map.with_index(1) do |line, i|
281-
"<span class=\"lineno\">#{i}</span>#{line}"
282-
end
283-
284-
numbered_lines.join("\n")
285-
end
286241
end
287242
end
288243
end

0 commit comments

Comments
 (0)