File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -22,8 +22,41 @@ public function __invoke(string $uri)
2222 ])->filter ()->implode ("\n\n" );
2323 });
2424
25+ $ markdown = $ this ->appendMdExtensionToInternalLinks ($ markdown );
26+
2527 return response ($ markdown , 200 , [
2628 'Content-Type ' => 'text/markdown; charset=UTF-8 ' ,
2729 ]);
2830 }
31+
32+ private function appendMdExtensionToInternalLinks (string $ markdown ): string
33+ {
34+ return preg_replace_callback (
35+ '/(?<!!)\[([^\]]+)\]\(([^)]+)\)/ ' ,
36+ function ($ matches ) {
37+ $ text = $ matches [1 ];
38+ $ url = $ matches [2 ];
39+
40+ if ($ this ->shouldAppendMdExtension ($ url )) {
41+ $ url .= '.md ' ;
42+ }
43+
44+ return "[ $ text]( $ url) " ;
45+ },
46+ $ markdown
47+ );
48+ }
49+
50+ private function shouldAppendMdExtension (string $ url ): bool
51+ {
52+ if (preg_match ('/^https?:\/\// ' , $ url )) {
53+ return false ;
54+ }
55+
56+ if (preg_match ('/\.[a-z0-9]{2,4}$/i ' , $ url )) {
57+ return false ;
58+ }
59+
60+ return true ;
61+ }
2962}
You can’t perform that action at this time.
0 commit comments