1
Fork 0
mirror of https://github.com/glassflame/glassflame.github.io.git synced 2024-11-25 17:44:26 +00:00

Working wikilinks!

This commit is contained in:
Steffo 2023-10-29 04:20:38 +01:00
parent ce594624dc
commit 8d3343fa05
Signed by: steffo
GPG key ID: 2A24051445686895
3 changed files with 66 additions and 29 deletions

View file

@ -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
/**

View file

@ -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()
}

View file

@ -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<void>}
*/
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()
}
}