Skip to content

Commit 4c6e49e

Browse files
authored
Merge pull request #69127 from wgordon17/add-edit-button
CCSSTRAT-176: Introduce easy 'Edit' buttons on built site
2 parents d4c1a2f + 158e3d4 commit 4c6e49e

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

_converters/custom-html5-converter.rb

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
require 'asciidoctor'
2+
require 'ascii_binder/distro_map'
3+
require 'ascii_binder/helpers'
4+
5+
include AsciiBinder::Helpers
6+
7+
Asciidoctor::Document.prepend (Module.new do
8+
attr_writer :sourcemap
9+
end) unless Asciidoctor::Document.method_defined? :sourcemap=
10+
11+
# A preprocessor that enables the sourcemap feature if not already enabled via the API.
12+
Asciidoctor::Extensions.register do
13+
preprocessor do
14+
process do |doc, reader|
15+
doc.sourcemap = true
16+
nil
17+
end
18+
end
19+
end
20+
21+
class CustomHtml5Converter < (Asciidoctor::Converter.for 'html5')
22+
register_for 'html5'
23+
24+
def initialize backend, opts = {}
25+
# A new converter is initialized for every new assembly file
26+
# Create a new set to prevent showing multiple edit buttons for the
27+
# same module file
28+
@seen_files = Set.new
29+
@branch_name = ENV.fetch("GIT_BRANCH", "main")
30+
@github_repo = ENV.fetch("GIT_REPO", "openshift/openshift-docs")
31+
distro_edit_enabled = ENV.fetch("DISTRO_EDIT_ENABLED", "").split(",")
32+
distro_map = AsciiBinder::DistroMap.new(File.join(docs_root_dir, DISTRO_MAP_FILENAME))
33+
# current_distro will be `nil` if not intersect is found
34+
current_distro = (distro_map.distro_keys & opts[:document].attributes.keys).first
35+
@show_edit_button = distro_edit_enabled.include? current_distro
36+
super
37+
end
38+
39+
# "embedded" seems to capture a fully complete document,
40+
# so placing an edit button at the top encompasses a whole-page
41+
# edit
42+
def convert_embedded node
43+
parent_output = super node
44+
if @show_edit_button && node.source_location.nil?
45+
%(<div class="edit-page-button edit-button">
46+
<a href="https://github.com/#{ @github_repo }/edit/#{ @branch_name }/#{ node.attributes["repo_path"] }" target="_blank">
47+
<span class="material-icons-outlined" title="Suggest a page edit">edit</span>
48+
</a>
49+
</div>
50+
#{ parent_output })
51+
else
52+
parent_output
53+
end
54+
end
55+
56+
# Focusing on "section" conversions prevent including modules
57+
# for content such as "snippets", etc. and focuses primarily
58+
# on modules
59+
def convert_section node
60+
parent_output = super node
61+
if @show_edit_button && node.source_location
62+
file_path = node.source_location.path.include?("modules") ? node.source_location.path : node.source_location.file.sub(node.source_location.dir + "/", "")
63+
64+
if @seen_files.empty? || !@seen_files.include?(file_path)
65+
@seen_files << file_path
66+
%(<div class="edit-section-button edit-button">
67+
<a href="https://github.com/#{ @github_repo }/edit/#{ @branch_name }/#{ file_path }" target="_blank">
68+
<span class="material-icons-outlined" title="Suggest a section edit">edit</span>
69+
</a>
70+
</div>
71+
#{ parent_output })
72+
end
73+
else
74+
parent_output
75+
end
76+
end
77+
end

0 commit comments

Comments
 (0)