Skip to content

Commit 8afd5be

Browse files
matiasgarciaisaiaSijastraight-shoota
authored
Add --base-path for crystal docs (#16091)
The 404 page of the docs is served at different (sub)paths in a website, relative references to assets (CSS and JS files) don't work for subdirectories and we must set a base href to correctly resolve relative URLs. Co-authored-by: Sijawusz Pur Rahnama <sija@sija.pl> Co-authored-by: Johannes Müller <straightshoota@gmail.com>
1 parent 0f64bed commit 8afd5be

File tree

5 files changed

+10
-2
lines changed

5 files changed

+10
-2
lines changed

src/compiler/crystal/command/docs.cr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ class Crystal::Command
6060
project_info.canonical_base_url = value
6161
end
6262

63+
opts.on("--base-path=PATH", "Set the base path - must be absolute for finding assets on the 404 page") do |value|
64+
project_info.base_path = value
65+
end
66+
6367
opts.on("--sitemap-base-url=URL", "-b URL", "Set the sitemap base URL and generates sitemap") do |value|
6468
sitemap_base_url = value
6569
end

src/compiler/crystal/tools/doc/generator.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class Crystal::Doc::Generator
9191
File.write File.join(@output_dir, "index.json"), main_index
9292
File.write File.join(@output_dir, "search-index.js"), main_index.to_jsonp
9393

94-
File.write File.join(@output_dir, "404.html"), MainTemplate.new(Error404Template.new.to_s, types, project_info)
94+
File.write File.join(@output_dir, "404.html"), MainTemplate.new(Error404Template.new.to_s, types, project_info, base_path: project_info.base_path)
9595
end
9696

9797
def generate_sitemap(types)

src/compiler/crystal/tools/doc/html/main.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<!DOCTYPE html>
22
<html lang="en">
33
<head>
4+
<%- if base_path = self.base_path -%>
5+
<base href="<%= base_path %>" />
6+
<%- end -%>
47
<%= HeadTemplate.new(project_info, nil) %>
58
<meta name="repository-name" content="<%= project_info.name %>">
69
<title><%= project_info.name %> <%= project_info.version %></title>

src/compiler/crystal/tools/doc/project_info.cr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module Crystal::Doc
66
property refname : String? = nil
77
property source_url_pattern : String? = nil
88
property canonical_base_url : String? = nil
9+
property base_path : String? = nil
910

1011
def initialize(@name : String? = nil, @version : String? = nil, @refname : String? = nil, @source_url_pattern : String? = nil)
1112
end

src/compiler/crystal/tools/doc/templates.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ module Crystal::Doc
5757
ECR.def_to_s "#{__DIR__}/html/_other_types.html"
5858
end
5959

60-
record MainTemplate, body : String, types : Array(Type), project_info : ProjectInfo do
60+
record MainTemplate, body : String, types : Array(Type), project_info : ProjectInfo, base_path : String? = nil do
6161
ECR.def_to_s "#{__DIR__}/html/main.html"
6262
end
6363

0 commit comments

Comments
 (0)