Skip to content

Commit 60d265c

Browse files
authored
External link format fix (#6)
- added some comments in the code about the external link format requirements - 404 page enhancement Signed-off-by: Hofi <hofione@gmail.com>
2 parents 2528215 + 73eca9d commit 60d265c

File tree

3 files changed

+37
-34
lines changed

3 files changed

+37
-34
lines changed

_data/external_links.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ oi-patents:
4545

4646
oi-trademark:
4747
id: oi-trademark
48-
url: www.OneIdentity.com/legal/trademark-information.aspx
48+
url: https://www.OneIdentity.com/legal/trademark-information.aspx
4949
title: [ "One Identity Trademark" ]
5050

5151
sn-ccanndw-license:

_plugins/generate_tooltips.rb

+32-29
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ class << self
99

1010
def add_matching_files(file_names, folder_path, pattern)
1111
fullPattern = File.join(folder_path, '*') #pattern
12-
12+
1313
# NOTE: This is not a real reg-exp https://docs.ruby-lang.org/en/master/Dir.html#method-c-glob
1414
# and actually, how it works is a mess
15-
# Trying to use a manual solution instead, filtering * with useable regex
15+
# Trying to use a manual solution instead, filtering * with useable regex
1616
Dir.glob(fullPattern, File::FNM_EXTGLOB).each do |file|
1717
if file.match?(pattern)
1818
#puts "match: " + file
@@ -33,15 +33,15 @@ def save_from_markdownify(text)
3333
text = text.gsub(/\|/, "&#124;") # |
3434
end
3535

36-
def is_external_url?(url)
36+
def is_prefixed_url?(url)
3737
if url =~ %r{^\w+://}
3838
return true
3939
end
4040
return false
4141
end
4242

4343
def prefixed_url(url, base_url)
44-
if false == is_external_url?(url)
44+
if false == is_prefixed_url?(url)
4545
url = base_url + url
4646
end
4747
return url
@@ -58,12 +58,15 @@ def make_tooltip(page, page_links, id, url, needs_tooltip, match)
5858
url = page_links[id]["url"]
5959
url = prefixed_url(url, page.site.config["baseurl"])
6060
end
61-
62-
external_url = is_external_url?(url)
61+
62+
# NOTE: Now we treat every link that has protocol prefix part as an external one
63+
# that allows usage of direct links anywhere if needed (not recommended, plz use external_links.yml instead)
64+
# but, at the same time requires e.g. all the really external links to be fully qualified (even in external_links.yml as well)
65+
external_url = is_prefixed_url?(url)
6366
match = save_from_markdownify(match)
6467
replacement_text = '<a href="' + url + '" class="nav-link' + (needs_tooltip ? ' content-tooltip' : '') + '"' + (external_url ? ' target="_blank"' : '') + '>' + match + '</a>'
6568
puts "replacement_text: " + replacement_text
66-
69+
6770
return replacement_text
6871
end
6972

@@ -97,14 +100,14 @@ def process_markdown_parts(page, markdown)
97100
# Split the content by special Markdown blocks
98101
markdown_parts = markdown.split(special_markdown_blocks_pattern)
99102
#puts markdown_parts
100-
markdown_parts.each_with_index do |markdown_part, markdown_index|
103+
markdown_parts.each_with_index do |markdown_part, markdown_index|
101104
#puts "---------------\nmarkdown_index: " + markdown_index.to_s + "\n" + (markdown_index.even? ? "NONE " : "") + "markdown_part: " + markdown_part
102105

103106
page.data["page_links_ids_sorted_by_title"].each do |page_titles_data|
104107
#puts "page_titles_data: #{page_titles_data}"
105-
108+
106109
id = page_titles_data["id"]
107-
110+
108111
link_data = page_links[id]
109112
# id = link_data["id"] these must match too
110113
title = page_titles_data["title"] # link_data["title"] is an array of titles that all must be already in the page_links_ids_sorted_by_title array
@@ -118,14 +121,14 @@ def process_markdown_parts(page, markdown)
118121
pattern = pattern.gsub('\ ', '[\s]+')
119122
#puts "searching for #{pattern}"
120123

121-
if markdown_index.even?
124+
if markdown_index.even?
122125
# Content outside of special Markdown blocks, aka. pure text (NOTE: Also excludes the reqursively self added <a ...>title</a> tooltips/links)
123126

124127
# Search for known link titles
125128
# NOTE: Using multi line matching here will not help either if the pattern itself is in the middle broken/spaned to multiple lines, so using whitespace replacements now inside the patter to handle this, see above!
126129
full_pattern = /(^|[\s.,;:&'(])(#{pattern})([\s.,;:&')]|\z)(?![^<]*?<\/a>)/
127130
markdown_part = process_markdown_part(page, markdown_part, page_links, full_pattern, id, url, needs_tooltip, true)
128-
else
131+
else
129132
# Content inside of special Markdown blocks
130133

131134
# Handle own auto tooltip links [[ ]], [[ | ]], [[ |id ]]
@@ -168,7 +171,7 @@ def write_to_file(file_path, content)
168171
file.write(content)
169172
end
170173
end
171-
174+
172175
def process_nav_link_items(items, ndx, nav_links_dictionary)
173176
items.each do |item|
174177
item['nav_ndx'] = ndx
@@ -182,14 +185,14 @@ def process_nav_link_items(items, ndx, nav_links_dictionary)
182185
return ndx
183186
end
184187

185-
def is_excluded_title?(excluded_titles, page_title)
188+
def is_excluded_title?(excluded_titles, page_title)
186189
if excluded_titles and false == excluded_titles.empty?
187190
# exluded list items can be a regex patters here
188191
excluded_titles.each do |title|
189192
title = title.gsub(/\A'|'\z/, '')
190193
pattern = /^#{title}$/
191194
#pattern = Regexp.escape(title)
192-
if page_title.match?(pattern)
195+
if page_title.match?(pattern)
193196
return true
194197
end
195198
end
@@ -239,7 +242,7 @@ def gen_page_link_data(links_dir, link_files_pattern)
239242
#page_links_dictionary = {}
240243
link_file_names = []
241244
add_matching_files(link_file_names, links_dir, link_files_pattern)
242-
245+
243246
link_file_names.each do |file_name|
244247
#puts file_name
245248
yaml_content = YAML.load_file(file_name)
@@ -271,7 +274,7 @@ def gen_page_link_data(links_dir, link_files_pattern)
271274
}
272275

273276
# Add the page_link_data object to the ID dictionary
274-
# NOTE: Title duplications are allowed now [[title|id]] format must be used
277+
# NOTE: Title duplications are allowed now [[title|id]] format must be used
275278
# to get the propwer matching tooltip for duplicated title items
276279
page_links_dictionary[page_id] = page_link_data
277280
end
@@ -341,8 +344,8 @@ def JekyllTooltipGen_debug_page_info(page, details = true)
341344
def JekyllTooltipGen_debug_filter_pages?(page)
342345
debug_pages = {
343346
# "doc/README.md" => true,
344-
}
345-
debug_ok = true
347+
}
348+
debug_ok = true
346349
# Comment this line out if not debugging!!!
347350
# debug_ok = (debug_pages[page.relative_path] != nil && debug_pages[page.relative_path])
348351
return debug_ok
@@ -362,15 +365,15 @@ def JekyllTooltipGen_hack_description_in(page_has_subtitle, page_has_description
362365
if page_has_description || page_has_subtitle
363366
# NOTE: Additional line breaks are essential here otherwise special constructs like tables, liquid notice or code block, etc. might break
364367
# Added double \n\n just to be prepared for the case if there's no \n at all at the file ending
365-
page.content = page.content + "\n\n" + desc_hack_separator + description
368+
page.content = page.content + "\n\n" + desc_hack_separator + description
366369
end
367370
end
368371

369372
def JekyllTooltipGen_hack_description_out(page_has_subtitle, page_has_description, page, desc_hack_separator)
370373
description = nil
371374

372375
content_parts = page.content.split(desc_hack_separator)
373-
content_parts.each_with_index do |content_part, content_part_index|
376+
content_parts.each_with_index do |content_part, content_part_index|
374377
#puts "---------------\ncontent_part_index: " + content_part_index.to_s + "\ncontent_part: " + content_part
375378
if content_part_index.even?
376379
page.content = content_part
@@ -412,7 +415,7 @@ def JekyllTooltipGen_hack_description_out(page_has_subtitle, page_has_descriptio
412415

413416
# 1st pass
414417
#
415-
# This is used now to
418+
# This is used now to
416419
# - set the page nav_ndx correctly to support our custom bottom collection elements navigator
417420
# - set additional page data elements that will be used during all the passes
418421
#
@@ -424,7 +427,7 @@ def JekyllTooltipGen_hack_description_out(page_has_subtitle, page_has_descriptio
424427
$JekyllTooltipGen_should_build_tooltips = (ENV['JEKYLL_BUILD_TOOLTIPS'] == 'yes')
425428
$JekyllTooltipGen_should_build_persistent_tooltips = (ENV['JEKYLL_BUILD_PERSISTENT_TOOLTIPS'] == 'yes')
426429
end
427-
next if false == $JekyllTooltipGen_should_build_tooltips
430+
next if false == $JekyllTooltipGen_should_build_tooltips
428431

429432
if $JekyllTooltipGen_markdown_extensions == nil
430433
$JekyllTooltipGen_markdown_extensions = site.config['markdown_ext'].split(',').map { |ext| ".#{ext.strip}" }
@@ -443,25 +446,25 @@ def JekyllTooltipGen_hack_description_out(page_has_subtitle, page_has_descriptio
443446

444447
next if false == $JekyllTooltipGen_markdown_extensions.include?(File.extname(page.relative_path)) && File.extname(page.relative_path) != ".html"
445448

446-
page_url = page.url.gsub(/\.[^.]+$/, '')
449+
page_url = page.url.gsub(/\.[^.]+$/, '')
447450
if (link_data = $JekyllTooltipGen_nav_links[page_url]) != nil
448451
page.data['nav_ndx'] = link_data['nav_ndx'] # page_pagination.html will use this as sort value for navigation ordering
449452
end
450453
page.data["page_links"] = $JekyllTooltipGen_page_links
451454
page.data["page_links_ids_sorted_by_title"] = $JekyllTooltipGen_page_links_ids_sorted_by_title
452-
# puts "collection: " + (page.respond_to?(:collection) ? page.collection.label : "") + ", nav_ndx: " + (link_data != nil ? link_data['nav_ndx'].to_s : "") + ", page_url: #{page_url}, page: #{page.relative_path}"
455+
# puts "collection: " + (page.respond_to?(:collection) ? page.collection.label : "") + ", nav_ndx: " + (link_data != nil ? link_data['nav_ndx'].to_s : "") + ", page_url: #{page_url}, page: #{page.relative_path}"
453456
end
454457
end
455458
end
456459

457460
# 2nd pass
458461
#
459-
# This is used now to
462+
# This is used now to
460463
# - add the description to the page end to get it rendred correclty the same way, together with the page content (will be removed/handled in the 3rd pass)
461464
# - render the page content manually and create the autolinks and tooltips
462465
#
463466
Jekyll::Hooks.register [:pages, :documents], :pre_render do |page, payload|
464-
next if false == $JekyllTooltipGen_should_build_tooltips
467+
next if false == $JekyllTooltipGen_should_build_tooltips
465468
next if false == $JekyllTooltipGen_markdown_extensions.include?(File.extname(page.relative_path)) && File.extname(page.relative_path) != ".html"
466469
next if false == JekyllTooltipGen_debug_filter_pages?(page)
467470

@@ -487,14 +490,14 @@ def JekyllTooltipGen_hack_description_out(page_has_subtitle, page_has_descriptio
487490

488491
# 3rd pass
489492
#
490-
# This is used now to
493+
# This is used now to
491494
# - remove the added hackish description block from the page end and replace the description with the now correctly rendered one
492495
#
493496
Jekyll::Hooks.register [:pages, :documents], :post_convert do |page|
494497
next if false == $JekyllTooltipGen_should_build_tooltips
495498
next if false == $JekyllTooltipGen_markdown_extensions.include?(File.extname(page.relative_path)) && File.extname(page.relative_path) != ".html"
496499
next if false == JekyllTooltipGen_debug_filter_pages?(page)
497-
500+
498501
page_has_subtitle = (page.data["subtitle"] && false == page.data["subtitle"].empty?)
499502
page_has_description = (page.data["description"] && false == page.data["description"].empty?)
500503
JekyllTooltipGen_hack_description_out(page_has_subtitle, page_has_description, page, JekyllTooltipGen_desc_hack_separator)

doc/404.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
---
22
title: 404 - Page Not Found
3-
description: >-
4-
Uh oh!</br>
5-
Houston, we have a problem... This page seems to be lost in space.
63
id: 404
74
permalink: /404
85
layout: single
96
toc: false
107
search: false
118
---
129

10+
Uh oh!\
11+
Houston, we have a problem... This page seems to be lost in space.
12+
1313
![404 - Lost In Space]({{img_folder}}/lost_in_space.png){: .align-center}
1414

15-
Fear not, intrepid traveler!
15+
Fear not, intrepid traveler!\
1616
Here are a few ways to escape this digital abyss:
1717

1818
> * Check the URL again. Typos happen to the best of us!

0 commit comments

Comments
 (0)