-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
174 lines (157 loc) · 4.36 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/* Look if we provide the filename */
$req = $_GET['f'] ?? '';
if (
empty($req) && // If we didn't have anything in ?f=
!str_starts_with($_SERVER['REQUEST_URI'], '/index.php') // And it's not a call to index.php
) {
// Use the subpath
$req = $_SERVER['REQUEST_URI'];
}
$filename = '';
if (!empty($req)) {
$filename = ltrim($req, '/'); // Get file name
if (substr($filename, -1) === '/') $filename = substr($filename, 0, -1); // If there's a trailing slash
if (substr($filename, -3) === '.md') $filename = substr($filename, 0, -3); // Remove extension
}
/* Load dependencies */
require('lib/Parsedown.php');
require('lib/ParsedownExtra.php');
require('lib/ParsedownFilter.php');
$parser = new ParsedownFilter('ourFilters');
/* Settings */
$parser->setBreaksEnabled(true); // Use <br /> on new lines
// This function is the filter called in PardownFilter so we can add attributes
function ourFilters(&$el)
{
switch ($el['name']) {
case 'a':
// Add rel and target to external links
$url = $el['attributes']['href'];
if (strpos($url, '://') === false) { // If it doesn't have a scheme...
// Let's make sure it's not a "//" shortcut
if ((($url[0] === '/') && ($url[1] !== '/')) || ($url[0] !== '/')) {
return;
}
}
// If the link doesn't contain our current host, then it's external
if (strpos($url, $_SERVER['SERVER_NAME']) === false) {
$el['attributes']['rel'] = 'noopener nofollow';
$el['attributes']['target'] = '_blank';
}
break;
case 'img':
// Add lazy loading attribute to images
$el['attributes']['loading'] = 'lazy';
break;
case 'blockquote':
// Add bootstrap class
$el['attributes']['class'] = 'blockquote';
break;
case 'table':
// Add bootstrap class
$el['attributes']['class'] = 'table table-borderless table-hover table-sm';
break;
case 'h1':
case 'h2':
case 'h3':
case 'h4':
case 'h5':
case 'h6':
// Create a slug and set as id of headings
$pattern = '/[^A-Za-z0-9àäéèêôõùüû\\-]/';
$slug = $el['handler']['argument'];
$slug = str_replace(' ', '-', $slug);
$slug = preg_replace($pattern, '', $slug);
// Remove double and trailing "-"
$slug = preg_replace('/\-+/', '-', $slug);
$slug = rtrim($slug, '-');
// Make it lowercase and set it
$slug = strtolower($slug);
$el['attributes']['id'] = $slug;
break;
}
}
/* Try to load document */
$dir = __DIR__ . '/data/'; // Where the .md are stored
$markdown = @file_get_contents($dir . $filename . '.md'); // Read the file
if (empty($filename)) { // Home page, since no document was asked
$markdown = '# Notes
Just a place for notes, _long messages_ and `code`.';
} else if (empty($markdown)) { // File is empty or doesn't exist
header('HTTP/1.0 404 Not Found');
$markdown = '# 404!
There\'s nothing here...';
}
// Parse our markdown
$output = $parser->text($markdown);
// Check if we have codeblocks to load highlight.js
$hasCodeblock = strpos($output, '<code class="language-') !== false;
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Notes<?php if (!empty($filename)) echo ' > ' . $filename; ?></title>
<link href="/assets/bootstrap.min.css" rel="stylesheet">
<style>
body {
margin: 15px;
cursor: default;
}
h2:target,
h3:target,
h4:target,
h5:target,
h6:target {
background-color: yellow;
}
a {
cursor: pointer;
}
code {
border: 1px solid #ccc;
}
pre>code {
border: none;
}
del {
background: black;
color: black;
text-decoration: none;
}
del:hover,
del:active {
color: white;
}
/* I don't like when images are too high */
img {
max-width: 100%;
max-height: 400px;
height: auto;
}
</style>
<?php if ($hasCodeblock) { ?>
<link rel="stylesheet" href="/assets/github.min.css">
<script src="/assets/highlight.min.js"></script>
<?php } ?>
</head>
<body>
<?php
echo $output . PHP_EOL;
if ($hasCodeblock) {
?>
<script>
hljs.configure({
cssSelector: 'pre code[class^="language-"]'
})
hljs.highlightAll()
</script>
<?php } ?>
<!--
Using MKody's spaghetti code
https://git.rita.moe/kody/mdNotes
-->
</body>
</html>