Skip to content

Commit

Permalink
fix broken obsidian-links and obsidian-img-links
Browse files Browse the repository at this point in the history
  • Loading branch information
guFalcon committed Apr 16, 2024
1 parent d86905a commit 8b66428
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
3 changes: 3 additions & 0 deletions md/test-md-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ Add them using an absolute image-url like so:
`https://raw.githubusercontent.com/UnterrainerInformatik/java-http-server/master/docs/standard-request-response-process.png`

![global-image|100x300](https://raw.githubusercontent.com/UnterrainerInformatik/java-http-server/master/docs/standard-request-response-process.png)

### Obsidian Link Images
![[Pasted image 20240315152828.png]]
### Local Images (path)
Add them using a relative path in your repository like so:
(The base of your path is the path of the current MD-file)
Expand Down
12 changes: 6 additions & 6 deletions obsidian.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export const callouts = {
// Key is the file-name without path (and no extension for the md-file-list).
// Value is an array of the relative paths to all the files with that name anywhere in the file-system.
// If there is only one file with that name, the array will contain only one element.
export let dirPrefix = "";
export const mdFilesMap = {};
export const filesMap = {};
export const mdFilesDir = {};
Expand Down Expand Up @@ -166,7 +167,8 @@ function makeSafeForCSS(name) {
/**
* If it's the root dir, dirPrefix should be an empty string.
*/
export async function scanFiles(dirPrefix, dir, root = dir) {
export async function scanFiles(prefix, dir, root = dir) {
dirPrefix = prefix;
scanFilesInternal(dir, root);
let mdFiles = await Promise.all(
Object.keys(mdFilesDir).map(async (file) => {
Expand All @@ -178,7 +180,6 @@ export async function scanFiles(dirPrefix, dir, root = dir) {
}
return {
[file]: {
dirPrefix: dirPrefix,
path: file,
pathWithoutExt: pwe,
folders: folders,
Expand Down Expand Up @@ -419,7 +420,7 @@ function preReplaceObsidianFileLinks(html, req) {
}
f = encodeURIComponent(f);
const serverUrl = `${req.protocol}://${req.get("host")}`;
return `[${alt ? alt : fileName}](${serverUrl}/${f})`;
return `[${alt ? alt : fileName}](${serverUrl}/${dirPrefix + f})`;
} else {
return match;
}
Expand Down Expand Up @@ -612,7 +613,7 @@ function replaceObsidianImageLinks(html, req) {
}
}
const serverUrl = `${req.protocol}://${req.get("host")}`;
return `<img alt="${fileName}" src="${serverUrl}/${f}" style="${
return `<img alt="${fileName}" src="${serverUrl}/${dirPrefix + f}" style="${
r ? `width: ${r.width}; height: ${r.height};` : ""
}" />`;
} else {
Expand Down Expand Up @@ -798,8 +799,7 @@ function insertDirFolder(folder, j) {

function insertDirLink(file, req, indent, i, files) {
let r = "";
console.log(file);
r += `<a href="/${file.dirPrefix + file.path}" class="${
r += `<a href="/${dirPrefix + file.path}" class="${
"/" + file.path === decodeURIComponent(req.path) ? "highlight" : ""
}">${indentStringFor(file.lastFolder === "" ? 0 : indent)}${
file.fileNameWithoutExtension
Expand Down

0 comments on commit 8b66428

Please sign in to comment.