@@ -34,34 +34,50 @@ public function wikiPage(
3434 Request $ request ,
3535 Response $ response ,
3636 TwigEnvironment $ twig ,
37- string $ page = ' Home '
37+ string $ page = null ,
3838 ){
39- // Vars
40- $ valid = false ;
41- $ page_file = $ page . '.md ' ;
42- $ page_filepath = self ::WIKI_ROOT . '/ ' . $ page_file ;
43- $ page_title = StringHelper ::replace ($ page , ['- ' => ' ' ]);
44-
45- // Make sure the file is valid
46- foreach (\glob (self ::WIKI_ROOT . '/*.md ' ) as $ file ){
47- if ($ page_file === \basename ($ file )){
48- $ valid = true ;
39+ // Redirect to the "/wiki/home" if no page is given
40+ if ($ page === null ){
41+ $ response = $ response ->withHeader ('Location ' , '/wiki/home ' )->withStatus (302 );
42+ return $ response ;
43+ }
44+
45+ // Make sure wiki page URL is lowercase
46+ // Redirect the user if it isn't
47+ if (\strtolower ($ page ) !== $ page ){
48+ $ response = $ response ->withHeader ('Location ' , '/wiki/ ' . \strtolower ($ page ))->withStatus (302 );
49+ return $ response ;
50+ }
51+
52+ // Get the markdown file
53+ $ file = null ;
54+ foreach (\glob (self ::WIKI_ROOT . '/*.md ' ) as $ file_path ){
55+ if (strtolower (\basename ($ file_path )) === strtolower ($ page ) . '.md ' ){
56+ $ file = $ file_path ;
57+ break ;
4958 }
5059 }
51- if (!$ valid ){
60+
61+ // Make sure file is found
62+ if (!$ file ){
5263 throw new HttpNotFoundException ($ request );
5364 }
5465
55- // Get wiki page contents
56- $ page_contents = \file_get_contents ($ page_filepath );
66+ // Get the page title
67+ $ page_title = StringHelper ::replace (\basename (\substr ($ file , 0 , -3 )), ['- ' => ' ' ]);
68+
69+ // Get and handle 'wiki page' contents
70+ $ page_contents = \file_get_contents ($ file );
71+ $ page_contents = $ this ->makeGithubUrlsLowercase ($ page_contents );
5772 $ page_contents = $ this ->fixMarkdownHeaderTagsSEO ($ page_contents );
5873
59- // Get sidebar contents
74+ // TODO: markdown titles should be #hash-bang linkable
75+
76+ // Get and handle 'sidebar' contents
6077 $ sidebar_contents = \file_get_contents (self ::WIKI_ROOT . '/_Sidebar.md ' );
78+ $ sidebar_contents = $ this ->makeGithubUrlsLowercase ($ sidebar_contents );
6179 $ sidebar_contents = $ this ->fixMarkdownHeaderTagsSEO ($ sidebar_contents );
62-
63- // Replace sidebar URLs (/Home -> /wiki/Home)
64- $ sidebar_contents = \preg_replace ('/\[(.+)\]\((.+)\)/ ' , '[$1](/wiki/$2) ' , $ sidebar_contents );
80+ $ sidebar_contents = \preg_replace ('/\[(.+)\]\((.+)\)/ ' , '[$1](/wiki/$2) ' , $ sidebar_contents ); // Replace sidebar URLs (/home -> /wiki/home)
6581
6682 // Render
6783 $ response ->getBody ()->write (
@@ -76,4 +92,30 @@ public function wikiPage(
7692 return $ response ;
7793 }
7894
95+ private function makeGithubUrlsLowercase (string $ string ): string
96+ {
97+ return \preg_replace_callback ("~\[(.+?)\]\((.+?)\)~ " , function ($ matches ){
98+
99+ // Only lowercase URLs without a slash
100+ // This should work against any absolute URLs as well as "subdirectory-URLs".
101+ if (\str_contains ($ matches [2 ], '/ ' ) === false ){
102+
103+ $ url = $ matches [2 ];
104+
105+ // Handle hash-bang
106+ if (\str_contains ($ url , '# ' ) === true ){
107+ $ exp = explode ('# ' , $ url );
108+ $ url = strtolower ($ exp [0 ]) . '# ' . $ exp [1 ];
109+ } else {
110+ $ url = strtolower ($ url );
111+ }
112+
113+ // Return the new markdown
114+ return \sprintf ("[%s](%s) " , $ matches [1 ], $ url );
115+ }
116+
117+ return $ matches [0 ];
118+ }, $ string );
119+ }
120+
79121}
0 commit comments