Skip to content

Commit fda0fae

Browse files
committed
implemented pretty urls
1 parent dab0516 commit fda0fae

File tree

5 files changed

+95
-45
lines changed

5 files changed

+95
-45
lines changed

Changelog.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 1.6
2+
- get rid of mb_strlen, fixed issue [#151](https://github.com/secure-77/Perlite/issues/151)
3+
- hide X / Twitter when not set, issue [#152](https://github.com/secure-77/Perlite/issues/152) thanks to @EKNr1
4+
- implemented settings.php, issue [#119](https://github.com/secure-77/Perlite/issues/119) thanks to @EKNr1
5+
6+
7+
18
## 1.5.9
29
- added YouTube link support #133 thanks to @rsubr
310
- added image postion support thanks to @ar0x4

perlite/.js/perlite.js

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ function getContent(str, home = false, popHover = false, anchor = "") {
6565
return;
6666
} else {
6767

68-
requestPath = "content.php?mdfile=" + str;
68+
requestPath = "/content.php?mdfile=" + str;
6969

7070
if (home) {
7171
if ($("div.no-mobile").css("display") == "none") {
7272
return
7373
}
74-
requestPath = "content.php?home";
74+
requestPath = "/content.php?home";
7575

7676
}
7777

@@ -177,7 +177,15 @@ function getContent(str, home = false, popHover = false, anchor = "") {
177177

178178
// update the url
179179
if (home == false) {
180-
window.history.pushState({}, "", location.protocol + '//' + location.host + location.pathname + "?link=" + str + anchor);
180+
// //window.history.pushState({}, "", location.protocol + '//' + location.host + location.pathname + "?link=" + str + anchor);
181+
182+
decodedURI = decodeURIComponent(str).substring(1)
183+
decodedURI = decodedURI.replaceAll('~','%80')
184+
decodedURI = decodedURI.replaceAll('-','~')
185+
decodedURI = decodedURI.replaceAll(' ','-')
186+
window.history.pushState({}, "", location.protocol + '//' + location.host + '/' + decodedURI + anchor);
187+
188+
181189
}
182190

183191

@@ -304,7 +312,11 @@ function getContent(str, home = false, popHover = false, anchor = "") {
304312
if (urlParams.has('link')) {
305313
var target = urlParams.get('link');
306314
target = encodeURIComponent(target);
315+
} else {
316+
target = decodeURIComponent(window.location.pathname)
307317
}
318+
319+
308320
// get content of link
309321
if (target) {
310322
getContent(target, false, true)
@@ -401,7 +413,7 @@ function getContent(str, home = false, popHover = false, anchor = "") {
401413

402414
var linkElement = mermaidLinks[f]
403415

404-
if (linkElement.getAttribute("href").startsWith("?link")) {
416+
if (linkElement.getAttribute("href").startsWith('/')) {
405417

406418
var textonly = '[[' + linkElement.innerHTML + ']]';
407419
linkElement.replaceWith(textonly)
@@ -789,7 +801,7 @@ function renderGraph(modal, path = "", filter_emptyNodes = false, show_tags = tr
789801
search(node.title);
790802

791803
} else {
792-
var glink = '?link=' + encodeURIComponent('/' + node.title);
804+
var glink = '/' + node.title;
793805
window.open(glink, "_self");
794806
}
795807
});
@@ -1128,6 +1140,16 @@ $(document).ready(function () {
11281140
var target = "";
11291141
if (urlParams.has('link')) {
11301142
var target = urlParams.get('link');
1143+
1144+
} else {
1145+
1146+
decodedURI = decodeURIComponent(window.location.pathname);
1147+
decodedURI = decodedURI.replaceAll('-',' ')
1148+
decodedURI = decodedURI.replaceAll('~','-')
1149+
decodedURI = decodedURI.replaceAll('%80','~')
1150+
1151+
target = decodedURI
1152+
11311153
}
11321154

11331155
if (target != "") {
@@ -1827,7 +1849,7 @@ $(document).ready(function () {
18271849
// info modal
18281850
$('.clickable-icon.side-dock-ribbon-action[aria-label="Help"]').click(function (e) {
18291851
$.ajax({
1830-
url: "content.php?about", success: function (result) {
1852+
url: "/content.php?about", success: function (result) {
18311853

18321854
$("div.aboutContent").html(result);
18331855
$("#about").css("display", "flex");
@@ -1854,8 +1876,18 @@ $(document).ready(function () {
18541876

18551877
min = Math.ceil(0);
18561878
max = Math.floor(nodesCount);
1857-
randomNode = Math.floor(Math.random() * (max - min) + min)
1858-
target = '/' + nodes[randomNode]['title']
1879+
1880+
// don't load tags as random nodes
1881+
do {
1882+
tag = true
1883+
randomNode = Math.floor(Math.random() * (max - min) + min)
1884+
if (nodes[randomNode]['title'].substring(0,1) != "#") {
1885+
tag = false
1886+
}
1887+
1888+
} while (tag);
1889+
1890+
target = '/' + nodes[randomNode]['title']
18591891
target = encodeURIComponent(target);
18601892
getContent(target)
18611893

@@ -1914,5 +1946,13 @@ $(document).ready(function () {
19141946
// init mermaid
19151947
mermaid.initialize({ startOnLoad: false, 'securityLevel': 'Strict', 'theme': 'dark' });
19161948

1949+
1950+
window.addEventListener("popstate", function(event) {
1951+
1952+
// Get the current URL
1953+
const currentPath = window.location.pathname;
1954+
getContent(currentPath);
1955+
});
1956+
19171957
});
19181958

perlite/content.php

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -115,53 +115,54 @@ function parseContent($requestFile)
115115

116116
$allowedImageTypes = '(\.png|\.jpg|\.jpeg|\.svg|\.gif|\.bmp|\.tif|\.tiff|\.webp)';
117117

118+
$src_path = '/' . $path;
118119

119120
// embedded pdf links
120-
$replaces = '<embed src="' . $path . '/\\2" type="application/pdf" style="min-height:100vh;width:100%">';
121+
$replaces = '<embed src="' . $src_path . '/\\2" type="application/pdf" style="min-height:100vh;width:100%">';
121122
$pattern = array('/(\!\[\[)(.*?.(?:pdf))(\]\])/');
122123
$content = preg_replace($pattern, $replaces, $content);
123124

124125
// embedded mp4 links
125126
$replaces = '
126-
<video controls src="' . $path . '/\\2" type="video/mp4">
127-
<a class="internal-link" target="_blank" rel="noopener noreferrer" href="' . $path . '/' . '\\2">Your browser does not support the video tag: Download \\2</a>
127+
<video controls src="' . $src_path . '/\\2" type="video/mp4">
128+
<a class="internal-link" target="_blank" rel="noopener noreferrer" href="' . $src_path . '/' . '\\2">Your browser does not support the video tag: Download \\2</a>
128129
</video>';
129130
$pattern = array('/(\!\[\[)(.*?.(?:mp4))(\]\])/');
130131
$content = preg_replace($pattern, $replaces, $content);
131132

132133

133134
// embedded m4a links
134135
$replaces = '
135-
<video controls src="' . $path . '/\\2" type="audio/x-m4a">
136-
<a class="internal-link" target="_blank" rel="noopener noreferrer" href="' . $path . '/' . '\\2">Your browser does not support the audio tag: Download \\2</a>
136+
<video controls src="' . $src_path . '/\\2" type="audio/x-m4a">
137+
<a class="internal-link" target="_blank" rel="noopener noreferrer" href="' . $src_path . '/' . '\\2">Your browser does not support the audio tag: Download \\2</a>
137138
</video>';
138139
$pattern = array('/(\!\[\[)(.*?.(?:m4a))(\]\])/');
139140
$content = preg_replace($pattern, $replaces, $content);
140141

141142

142143
// links to other files with Alias
143-
$replaces = '<a class="internal-link" target="_blank" rel="noopener noreferrer" href="' . $path . '/' . '\\2">\\3</a>';
144+
$replaces = '<a class="internal-link" target="_blank" rel="noopener noreferrer" href="' . $src_path . '/' . '\\2">\\3</a>';
144145
$pattern = array('/(\[\[)(.*?.(?:' . $linkFileTypes . '))\|(.*)(\]\])/');
145146
$content = preg_replace($pattern, $replaces, $content);
146147

147148
// links to other files without Alias
148-
$replaces = '<a class="internal-link" target="_blank" rel="noopener noreferrer" href="' . $path . '/' . '\\2">\\2</a>';
149+
$replaces = '<a class="internal-link" target="_blank" rel="noopener noreferrer" href="' . $src_path . '/' . '\\2">\\2</a>';
149150
$pattern = array('/(\[\[)(.*?.(?:' . $linkFileTypes . '))(\]\])/');
150151
$content = preg_replace($pattern, $replaces, $content);
151152

152153
// img links with external target link
153-
$replaces = 'noreferrer"><img class="images" width="\\4" height="\\5" alt="image not found" src="' . $path . '/\\2\\3' . '"/>';
154+
$replaces = 'noreferrer"><img class="images" width="\\4" height="\\5" alt="image not found" src="' . $src_path . '/\\2\\3' . '"/>';
154155
$pattern = array('/noreferrer">(\!?\[\[)(.*?)'.$allowedImageTypes.'\|?(\d*)x?(\d*)(\]\])/');
155156
$content = preg_replace($pattern, $replaces, $content);
156157

157158
// img links with size
158-
$replaces = '<p><a href="#" class="pop"><img class="images" width="\\4" height="\\5" alt="image not found" src="' . $path . '/\\2\\3' . '"/></a></p>';
159+
$replaces = '<p><a href="#" class="pop"><img class="images" width="\\4" height="\\5" alt="image not found" src="' . $src_path . '/\\2\\3' . '"/></a></p>';
159160
$pattern = array('/(\!?\[\[)(.*?)'.$allowedImageTypes.'\|?(\d*)x?(\d*)(\]\])/');
160161
$content = preg_replace($pattern, $replaces, $content);
161162

162163
// centerise or right align images with "center"/"right" directive
163164
$pattern = '/(\!?\[\[)(.*?)'.$allowedImageTypes.'\|?(center|right)\|?(\d*)x?(\d*)(\]\])/';
164-
$replaces = function ($matches) use ($path) {
165+
$replaces = function ($matches) use ($src_path) {
165166
$class = "images"; // Default class for all images
166167
if (strpos($matches[4], 'center') !== false) {
167168
$class .= " center"; // Add 'center' class
@@ -170,17 +171,17 @@ function parseContent($requestFile)
170171
}
171172
$width = $matches[5] ?? 'auto';
172173
$height = $matches[6] ?? 'auto';
173-
return '<p><a href="#" class="pop"><img class="' . $class . '" src="' . $path . '/' . $matches[2] . $matches[3] . '" width="' . $width . '" height="' . $height . '"/></a></p>';
174+
return '<p><a href="#" class="pop"><img class="' . $class . '" src="' . $src_path . '/' . $matches[2] . $matches[3] . '" width="' . $width . '" height="' . $height . '"/></a></p>';
174175
};
175176
$content = preg_replace_callback($pattern, $replaces, $content);
176177

177178
// img links with captions and size
178-
$replaces = '<p><a href="#" class="pop"><img class="images" width="\\5" height="\\6" alt="\\4" src="' . $path . '/\\2\\3' . '"/></a></p>';
179+
$replaces = '<p><a href="#" class="pop"><img class="images" width="\\5" height="\\6" alt="\\4" src="' . $src_path . '/\\2\\3' . '"/></a></p>';
179180
$pattern = array('/(\!?\[\[)(.*?)'.$allowedImageTypes.'\|?(.+\|)\|?(\d*)x?(\d*)(\]\])/');
180181
$content = preg_replace($pattern, $replaces, $content);
181182

182183
// img links with captions
183-
$replaces = '<p><a href="#" class="pop"><img class="images" alt="\\4" src="' . $path . '/\\2\\3' . '"/></a></p>';
184+
$replaces = '<p><a href="#" class="pop"><img class="images" alt="\\4" src="' . $src_path . '/\\2\\3' . '"/></a></p>';
184185
$pattern = array('/(\!?\[\[)(.*?)'.$allowedImageTypes.'\|?(.+|)(\]\])/');
185186
$content = preg_replace($pattern, $replaces, $content);
186187

@@ -326,8 +327,9 @@ function ($matches) use ($path, $sameFolder) {
326327

327328

328329
$urlPath = $newAbPath . '/' . $linkFile;
329-
if (substr($urlPath, 0, 1) != '/') {
330-
$urlPath = '/' . $urlPath;
330+
if (substr($urlPath, 0, 1) == '/') {
331+
#$urlPath = '/' . $urlPath;
332+
$urlPath = substr($urlPath, 1);
331333
}
332334

333335
$refName = '';
@@ -341,12 +343,13 @@ function ($matches) use ($path, $sameFolder) {
341343
$refName = '#' . $refName;
342344
$href = 'href="';
343345
} else {
344-
$href = 'href="?link=';
346+
#$href = 'href="?link=';
347+
$href = 'href="/';
345348
}
346349

347350
$urlPath = str_replace('&amp;', '&', $urlPath);
348351

349-
$urlPath = rawurlencode($urlPath);
352+
#$urlPath = rawurlencode($urlPath);
350353
$urlPath = str_replace('%23', '#', $urlPath);
351354

352355
return '<a class="internal-link' . $popupClass . '"' . $href . $urlPath . $refName . '">' . $linkName . '</a>' . $popUpIcon;

perlite/helper.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
if ($siteLogo and empty($customSection)) {
8080
$customSection = '<div class="sm-site-title">&nbsp;</div>
8181
<div class="custom-page">
82-
<img class="custom-page-logo" src="' . $siteLogo . '" alt="Custom Logo">
82+
<img class="custom-page-logo" src="/' . $siteLogo . '" alt="Custom Logo">
8383
<div> &nbsp;</div>';
8484

8585
$customSection = $customSection . '
@@ -91,7 +91,7 @@
9191
$customSection = $customSection . '
9292
<li>
9393
<a href="' . $siteGithub . '">
94-
<img class="social-logo" src=".styles\github-color.svg" alt="Github Logo">
94+
<img class="social-logo" src="/.styles/github-color.svg" alt="Github Logo">
9595
</a>
9696
</li>';
9797
}
@@ -100,15 +100,15 @@
100100
$customSection = $customSection . '
101101
<li>
102102
<a href="https://twitter.com/' . substr($siteTwitter, 1) . '">
103-
<img class="social-logo" src=".styles\x-color.svg" alt="X Logo">
103+
<img class="social-logo" src="/.styles/x-color.svg" alt="X Logo">
104104
</a>
105105
</li>';
106106
}
107107

108108
$customSection = $customSection . '
109109
<li>
110110
<a href="' . $siteHomepage . '">
111-
<img class="social-logo" src=".styles\fontawesome-color.svg" alt="Homepage Logo">
111+
<img class="social-logo" src="/.styles/fontawesome-color.svg" alt="Homepage Logo">
112112
</a>
113113
</li>
114114
</ul>';
@@ -695,7 +695,7 @@ function loadSettings($rootDir)
695695

696696
$folderName = getFolderInfos($folder)[2];
697697
$folderClean = str_replace(' ', '_', $folderName);
698-
$themePath = $rootDir . '/.obsidian/themes/' . $folderName . '/theme.css';
698+
$themePath = '/' . $rootDir . '/.obsidian/themes/' . $folderName . '/theme.css';
699699

700700
if ($defaultTheme === $folderName) {
701701

perlite/index.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@
3232
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover">
3333

3434
<?php echo loadSettings($rootDir); ?>
35-
<link rel="stylesheet" href=".styles/app.css" type="text/css">
36-
<link id="highlight-js" rel="stylesheet" href=".styles/atom-one-dark.min.css" type="text/css">
37-
<link rel="stylesheet" href=".styles/perlite.css" type="text/css">
38-
<link rel="stylesheet" href=".styles/vis-network.min.css" type="text/css">
39-
<link rel="stylesheet" href=".styles/katex.min.css" type="text/css">
40-
<link rel="icon" type="image/x-icon" href="favicon.ico">
35+
<link rel="stylesheet" href="/.styles/app.css" type="text/css">
36+
<link id="highlight-js" rel="stylesheet" href="/.styles/atom-one-dark.min.css" type="text/css">
37+
<link rel="stylesheet" href="/.styles/perlite.css" type="text/css">
38+
<link rel="stylesheet" href="/.styles/vis-network.min.css" type="text/css">
39+
<link rel="stylesheet" href="/.styles/katex.min.css" type="text/css">
40+
<link rel="icon" type="image/x-icon" href="/favicon.ico">
4141

42-
<script src=".js/jquery.min.js"></script>
43-
<script src=".js/highlight.min.js"></script>
44-
<script src=".js/vis-network.min.js"></script>
45-
<script src=".js/katex.min.js"></script>
46-
<script src=".js/auto-render.min.js"></script>
47-
<script src=".js/vis-network.min.js"></script>
42+
<script src="/.js/jquery.min.js"></script>
43+
<script src="/.js/highlight.min.js"></script>
44+
<script src="/.js/vis-network.min.js"></script>
45+
<script src="/.js/katex.min.js"></script>
46+
<script src="/.js/auto-render.min.js"></script>
47+
<script src="/.js/vis-network.min.js"></script>
4848
<!-- <script src=".js/mermaid.min.js"></script> -->
4949
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/mermaid.min.js"></script>
5050

@@ -93,7 +93,7 @@ class="logo-full">
9393
<div class="workspace is-left-sidedock-open">
9494
<div class="workspace-ribbon side-dock-ribbon mod-left">
9595

96-
<a href="."><img src="logo.svg" height="25" class="logo" alt="Perlite Logo"></a>
96+
<a href="."><img src="/logo.svg" height="25" class="logo" alt="Perlite Logo"></a>
9797
<div class="sidebar-toggle-button mod-left sidebar" aria-label="" aria-label-position="right">
9898

9999

@@ -833,7 +833,7 @@ class="svg-icon right-triangle">
833833
<div class="modal-bg" style="opacity: 0.85;"></div>
834834
<div class="modal">
835835
<div class="modal-close-button"></div>
836-
<div class="modal-title"> <a href="."><img src="logo.svg" height="35" alt="Perlite Logo"
836+
<div class="modal-title"> <a href="."><img src="/logo.svg" height="35" alt="Perlite Logo"
837837
style="padding-top: 10px"></a> Perlite</div>
838838
<div class="aboutContent modal-content"></div>
839839
</div>
@@ -1020,7 +1020,7 @@ class="svg-icon right-triangle">
10201020

10211021

10221022
</div>
1023-
<script src=".js/perlite.js"></script>
1023+
<script src="/.js/perlite.js"></script>
10241024
</body>
10251025

10261026
</html>

0 commit comments

Comments
 (0)