Skip to content

Commit 6210741

Browse files
committed
Append .md to internal links
1 parent 9dc08ca commit 6210741

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

app/Http/Controllers/DocsMarkdownController.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)