diff --git a/src/elements/browse.mjs b/src/elements/browse.mjs index be5f36a..bdc75d5 100644 --- a/src/elements/browse.mjs +++ b/src/elements/browse.mjs @@ -34,21 +34,6 @@ export class BrowseElement extends HTMLBodyElement { return url } - urlName(name) { - if(name.startsWith(".")) { - const path = filePath(name).join("/") - return this.urlFor({path}) - } - else if(!name.includes("/")) { - const path = `${fileDetails(this.parameters.path).directory}/${name}.md` - return this.urlFor({path}) - } - else { - const path = filePath(name).join("/") - return this.urlFor({path}) - } - } - // TODO: Add a landing page /** diff --git a/src/elements/markdown/wikilink.mjs b/src/elements/markdown/wikilink.mjs index 9b0c140..47d9659 100644 --- a/src/elements/markdown/wikilink.mjs +++ b/src/elements/markdown/wikilink.mjs @@ -1,6 +1,9 @@ import { CustomElement } from "../base.mjs"; import {findFirstAncestor} from "../../utils/trasversal.mjs"; import {BrowseElement} from "../browse.mjs"; +import {VaultElement} from "../vault.mjs"; +import {filePath} from "../../utils/file.mjs"; +import {DisplayElement} from "../display.mjs"; /** @@ -12,17 +15,34 @@ export class WikilinkElement extends CustomElement { } /** - * Element handling the page navigation. - * @type {BrowseElement} + * Element displaying the this one. + * Can be recalculated with {@link recalculateAncestors}. + * @type {DisplayElement} */ - browseElement + display /** - * Recalculate the value of {@link browseElement} using this element's current position in the DOM. + * Element representing the Obsidian Vault. + * Can be recalculated with {@link recalculateAncestors}. + * @type {VaultElement} + */ + vault + + /** + * Element handling the page navigation. + * Can be recalculated with {@link recalculateAncestors}. + * @type {BrowseElement} + */ + browse + + /** + * Recalculate the value of {@link browse} and {@link vault} using this element's current position in the DOM. * @returns {void} */ - recalculateBrowseElement() { - this.browseElement = findFirstAncestor(this, BrowseElement) + recalculateAncestors() { + this.display = findFirstAncestor(this, DisplayElement) + this.vault = findFirstAncestor(this, VaultElement) + this.browse = findFirstAncestor(this, BrowseElement) } /** @@ -69,13 +89,28 @@ export class WikilinkElement extends CustomElement { } resetAnchorElementProperties() { - this.anchorElement.href = this.browseElement.urlName(this.target) + let path = null + if(this.target.startsWith(".")) { + path = filePath(this.display.path + "/" + this.target).join("/") + } + else if(this.target.includes("/")) { + path = filePath(this.target).join("/") + } + else { + if(this.vault.fileIndex !== null) { + path = this.vault.fileIndex.basenames[this.target] + } + } + + if(path !== null) { + this.anchorElement.href = this.browse.urlFor({path}).toString() + } this.anchorElement.innerText = this.text } onConnect() { super.onConnect() - this.recalculateBrowseElement() + this.recalculateAncestors() this.recalculateAnchorElement() this.resetAnchorElementProperties() } diff --git a/src/elements/vault.mjs b/src/elements/vault.mjs index 11d0c23..48883b1 100644 --- a/src/elements/vault.mjs +++ b/src/elements/vault.mjs @@ -119,7 +119,6 @@ export class VaultElement extends CustomElement { this.vaultElement = this.instance.querySelector(this.constructor.VAULT_SELECTOR) } - /** * The accent color of this vault. * Can be set manually, or updated via {@link refetchAppearance}. @@ -145,12 +144,30 @@ export class VaultElement extends CustomElement { } } - onConnect() { + /** + * Index containing information about the files available in the Vault. + * Used to resolve Wikilinks. + * @type {{basenames: {[basename: string]: string}, paths: []} | null} + */ + fileIndex + + /** + * Update {@link fileIndex} by fetching the `steffo-file-index.json` file located at the root of the Vault. + * @returns {Promise} + */ + async refetchSteffoFileIndex() { + const response = await this.fetchCooldown("steffo-file-index.json") + if(response.status >= 400) { + this.fileIndex = null + } + this.fileIndex = await response.json() + } + + async onConnect() { super.onConnect() this.recalculateVaultElement() - // noinspection JSIgnoredPromiseFromCall - this.#fetchQueueScheduler() - // noinspection JSIgnoredPromiseFromCall - this.refetchAppearance() + this.#fetchQueueScheduler().then() + await this.refetchAppearance() + await this.refetchSteffoFileIndex() } } \ No newline at end of file