Skip to content

Commit 434deee

Browse files
committed
fix
1 parent a63001e commit 434deee

File tree

5 files changed

+90
-40
lines changed

5 files changed

+90
-40
lines changed

app.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ function getStartPage() {
157157
app.use(express.urlencoded({ extended: true }));
158158
app.use(express.json());
159159

160+
app.use((req, res, next) => {
161+
res.setHeader('X-Frame-Options', 'SAMEORIGIN');
162+
res.setHeader('Content-Security-Policy', "frame-ancestors 'self'");
163+
next();
164+
});
165+
160166
initKeycloak(app).then(() => {
161167
// Protect all routes and serve them statically after authentication.
162168
// Order matters when dealing with middleware!

md/assets/Test.docx

13.2 KB
Binary file not shown.

md/assets/Test.pdf

51.5 KB
Binary file not shown.

md/test-md-file.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,9 @@ Add them using a relative path in your repository like so:
104104
`assets/test-local-image.jpg`
105105

106106
Example:
107-
![local-image](/md/assets/test-local-image.jpg)
107+
![local-image](/md/assets/test-local-image.jpg)
108+
## Other files treated like images by Obsidian
109+
### PDF
110+
![[Test.pdf]]
111+
### Docx
112+
![[Test.docx]]

obsidian.js

Lines changed: 78 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const internalSubstitutions = {
2727
},
2828
};
2929
let codeList = [];
30-
let openNavTreeScript = '';
30+
let openNavTreeScript = "";
3131

3232
export const callouts = {
3333
note: {
@@ -669,31 +669,68 @@ function replaceObsidianImageLinks(html, req) {
669669
}
670670
}
671671
const serverUrl = `${req.protocol}://${req.get("host")}`;
672-
return getHtmlFor(getFileExtension(f), fileName, serverUrl, dirPrefix, f);
672+
return getHtmlFor(getFileExtension(f), fileName, serverUrl, dirPrefix, f, r);
673673
} else {
674674
return match;
675675
}
676676
});
677677
}
678678

679-
const imageFileTypes = ['png', 'jpg', 'jpeg', 'apng', 'avif', 'gif', 'jfif', 'pjpeg', 'pjp', 'svg', 'webp', 'bmp', 'ico', 'tiff', 'tif', 'heif', 'heic'];
680-
681-
function getHtmlFor(fileType, fileName, serverUrl, dirPrefix, file) {
682-
const type = fileType.toLowerCase();
683-
if (imageFileTypes.includes(type)) {
684-
return `<img src="${serverUrl}/${dirPrefix + file}" alt="${fileName}" style="${r ? `width: ${r.width}; height: ${r.height};` : ""}"/>`;
685-
} else if (type === "svg") {
686-
return `<img src="${serverUrl}/${dirPrefix + file}" alt="${fileName}" />`;
687-
} else if (type === "pdf") {
688-
return `<iframe src="${serverUrl}/${dirPrefix + file}" style="${r ? `width: ${r.width}; height: ${r.height};` : ""}"></iframe>`;
689-
} else if (type === "mp4") {
690-
return `<video controls><source src="${serverUrl}/${dirPrefix + file}" type="video/mp4"></video>`;
691-
} else if (type === "webm") {
692-
return `<video controls><source src="${serverUrl}/${dirPrefix + file}" type="video/webm"></video>`;
693-
} else if (type === "ogg") {
694-
return `<audio controls><source src="${serverUrl}/${dirPrefix + file}" type="audio/ogg"></audio>`;
679+
function getFileExtension(filePath) {
680+
const parts = filePath.split(".");
681+
if (parts.length > 1) {
682+
return parts[parts.length - 1].toLowerCase();
683+
}
684+
return null;
685+
}
686+
687+
const imageFileTypes = [
688+
"png",
689+
"jpg",
690+
"jpeg",
691+
"apng",
692+
"avif",
693+
"gif",
694+
"jfif",
695+
"pjpeg",
696+
"pjp",
697+
"svg",
698+
"webp",
699+
"bmp",
700+
"ico",
701+
"tiff",
702+
"tif",
703+
"heif",
704+
"heic",
705+
];
706+
707+
function getHtmlFor(fileType, fileName, serverUrl, dirPrefix, file, r) {
708+
if (fileType !== null) {
709+
const type = fileType.toLowerCase();
710+
console.log(type)
711+
if (imageFileTypes.includes(type)) {
712+
return `<img src="${serverUrl}/${
713+
dirPrefix + file
714+
}" alt="${fileName}" style="${
715+
r ? `width: ${r.width}; height: ${r.height};` : ""
716+
}"/>`;
717+
} else if (type === "svg") {
718+
return `<img src="${serverUrl}/${dirPrefix + file}" alt="${fileName}" />`;
719+
} else if (type === "mp4") {
720+
return `<video controls><source src="${serverUrl}/${
721+
dirPrefix + file
722+
}" type="video/mp4"></video>`;
723+
} else if (type === "webm") {
724+
return `<video controls><source src="${serverUrl}/${
725+
dirPrefix + file
726+
}" type="video/webm"></video>`;
727+
} else if (type === "ogg") {
728+
return `<audio controls><source src="${serverUrl}/${
729+
dirPrefix + file
730+
}" type="audio/ogg"></audio>`;
731+
}
695732
}
696-
return `${serverUrl}/${dirPrefix + file}`;
733+
return `<a href="${serverUrl}/${dirPrefix + file}">${fileName}</a>`;
697734
}
698735

699736
function replacePreMarkCallouts(html) {
@@ -798,8 +835,8 @@ async function getDirectoryListing(req) {
798835
);
799836
const filteredFiles = files.filter((f) => f !== null);
800837
const p = await markPathForSelectedPage(req, files);
801-
802-
openNavTreeScript = "<script lang=\"javascript\">\n"
838+
839+
openNavTreeScript = '<script lang="javascript">\n';
803840
openNavTreeScript += `toggleDirList('sidebar-dirlist');\n`;
804841
let r = await getDirectoryListingInternal(p, req, filteredFiles, []);
805842
openNavTreeScript += "</script>";
@@ -815,23 +852,23 @@ async function markPathForSelectedPage(req, files) {
815852
// console.log("getSelectedPage", req);
816853
// console.log("getSelectedPage", files);
817854
let path = decodeURIComponent(req.path);
818-
if(path.startsWith("/md/")) {
855+
if (path.startsWith("/md/")) {
819856
path = path.slice(4);
820857
}
821858
while (path.startsWith("/")) {
822859
path = path.slice(1);
823860
}
824861
const r = {
825862
path: [],
826-
file: null
827-
}
863+
file: null,
864+
};
828865
const splitPath = path.split("/");
829866
r.file = splitPath.pop();
830867
for (let i = 0; i < splitPath.length; i++) {
831868
const f = {
832869
dir: splitPath[i],
833-
level: i
834-
}
870+
level: i,
871+
};
835872
r.path.push(f);
836873
}
837874
// console.log("markPathForSelectedPage", r);
@@ -883,7 +920,12 @@ async function getDirectoryListingInternal(path, req, files, folders) {
883920
const subfolderFiles = files.filter(
884921
(f, index) => f.folders.startsWith(folders) && index > i
885922
);
886-
html += await getDirectoryListingInternal(path, req, subfolderFiles, folders);
923+
html += await getDirectoryListingInternal(
924+
path,
925+
req,
926+
subfolderFiles,
927+
folders
928+
);
887929

888930
// Update the last processed file index
889931
lastProcessedFileIndex = i + subfolderFiles.length;
@@ -921,15 +963,16 @@ function insertDirFolder(folder, j) {
921963
function insertDirLink(file, req, indent, i, files) {
922964
let r = "";
923965
let correctedPath = decodeURIComponent(req.path);
924-
if(correctedPath.startsWith("/md/")) {
966+
if (correctedPath.startsWith("/md/")) {
925967
correctedPath = correctedPath.slice(4);
926968
}
927969
let highlight = "";
928-
if(file.path === correctedPath)
929-
highlight = "highlight";
930-
r += `<a href="/${dirPrefix + file.path}" class="${highlight}">${indentStringFor(file.lastFolder === "" ? 0 : indent)}${
931-
file.fileNameWithoutExtension
932-
}</a>`;
970+
if (file.path === correctedPath) highlight = "highlight";
971+
r += `<a href="/${
972+
dirPrefix + file.path
973+
}" class="${highlight}">${indentStringFor(
974+
file.lastFolder === "" ? 0 : indent
975+
)}${file.fileNameWithoutExtension}</a>`;
933976
// Only add <br> if it isn't the last file in the folder
934977
if (i < files.length - 1 && files[i + 1].folders === file.folders) {
935978
r += "<br>";
@@ -1066,16 +1109,12 @@ async function getTopdownMenu(req) {
10661109
<button class="sl-button" style="height: 32px; margin: 0px;" onclick="openAsPresentation(true)">${lucideIcon(
10671110
"Printer"
10681111
)}
1069-
${lucideIcon(
1070-
"Presentation"
1071-
)}
1112+
${lucideIcon("Presentation")}
10721113
</button>
10731114
<button class="sl-button" style="height: 32px; margin: 0px; margin-left: 6px" onclick="openAsDocument(true)">${lucideIcon(
10741115
"Printer"
10751116
)}
1076-
${lucideIcon(
1077-
"ReceiptText"
1078-
)}
1117+
${lucideIcon("ReceiptText")}
10791118
</button>
10801119
</div>
10811120
</div>

0 commit comments

Comments
 (0)