Skip to content

Commit

Permalink
1.1.1 Fixes for Obsidian 1.1.5 and experimental feature: ghost links
Browse files Browse the repository at this point in the history
  • Loading branch information
TfT Hacker committed Dec 16, 2022
1 parent 69f825d commit fc4afe0
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 35 deletions.
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"id": "obsidian42-strange-new-worlds",
"name": "Obsidian42 - Strange New Worlds",
"version": "1.1.0",
"minAppVersion": "1.0.0",
"version": "1.1.1",
"minAppVersion": "1.1.0",
"description": "Revealing networked thought and the strange new worlds created by your vault",
"author": "TfTHacker",
"authorUrl": "https://twitter.com/TfTHacker",
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@
"license": "MIT",
"devDependencies": {
"@codemirror/commands": "^6.1.2",
"@codemirror/language": "^6.3.1",
"@codemirror/language": "^6.3.2",
"@codemirror/search": "^6.2.3",
"@codemirror/state": "^6.1.3",
"@codemirror/view": "^6.5.0",
"@types/node": "^18.11.9",
"@typescript-eslint/eslint-plugin": "^5.43.0",
"@typescript-eslint/parser": "^5.43.0",
"@codemirror/state": "^6.1.4",
"@codemirror/view": "^6.7.1",
"@types/node": "^18.11.15",
"@typescript-eslint/eslint-plugin": "^5.46.1",
"@typescript-eslint/parser": "^5.46.1",
"builtin-modules": "^3.2.0",
"esbuild": "0.15.14",
"esbuild": "0.16.7",
"obsidian": "^0.16.3",
"tslib": "2.4.1",
"typescript": "4.8.4"
"typescript": "4.9.4"
},
"dependencies": {
"tippy.js": "^6.3.7"
Expand Down
49 changes: 39 additions & 10 deletions src/indexer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This module builds on Obsidians cache to provide more specific link information

import { CachedMetadata, HeadingCache, stripHeading, TFile, Pos} from "obsidian";
import { CachedMetadata, HeadingCache, stripHeading, TFile, Pos, parseLinktext} from "obsidian";
import SNWPlugin from "./main";
import {Link, TransformedCache} from "./types";

Expand Down Expand Up @@ -30,7 +30,28 @@ export function getSnwAllLinksResolutions(){
export function buildLinksAndReferences(): void {
if(thePlugin.showCountsActive!=true) return;

allLinkResolutions = thePlugin.app.fileManager.getAllLinkResolutions(); //cache this for use in other pages
allLinkResolutions = [];
thePlugin.app.metadataCache.iterateReferences((src,refs)=>{
const resolvedFilePath = parseLinktext(refs.link);
if(resolvedFilePath?.path) {
const resolvedTFile = thePlugin.app.metadataCache.getFirstLinkpathDest(resolvedFilePath.path, "/");
const ghlink = !resolvedTFile ? resolvedFilePath.path : ""; // file doesnt exist, its a ghost link

allLinkResolutions.push(
{
reference: {
displayText: refs.displayText,
link: refs.link,
position: refs.position
},
resolvedFile: resolvedTFile,
ghostLink: ghlink,
sourceFile: thePlugin.app.metadataCache.getFirstLinkpathDest(src, "/"),
excludedFile: false
}
)
}
})

// START: Remove file exclusions for frontmatter snw-index-exclude
const snwIndexExceptionsList = Object.entries(app.metadataCache.metadataCache).filter((e)=>{
Expand All @@ -40,24 +61,32 @@ export function buildLinksAndReferences(): void {
return snwIndexExceptionsList.find(f=>f[0]===e[1].hash);
});



for (let i = 0; i < allLinkResolutions.length; i++) {
allLinkResolutions[i].excludedFile = false;
const fileName = allLinkResolutions[i].resolvedFile.path;
for (let e = 0; e < snwIndexExceptions.length; e++) {
if(fileName==snwIndexExceptions[e][0]) {
allLinkResolutions[i].excludedFile = true;
break;
if(allLinkResolutions[i]?.resolvedFile?.path){
const fileName = allLinkResolutions[i].resolvedFile.path;
for (let e = 0; e < snwIndexExceptions.length; e++) {
if(fileName==snwIndexExceptions[e][0]) {
allLinkResolutions[i].excludedFile = true;
break;
}
}
}
}
}
// END: Exclusions


const refs = allLinkResolutions.reduce((acc: {[x:string]: Link[]}, link : Link): { [x:string]: Link[] } => {
let keyBasedOnLink = "";
let keyBasedOnFullPath = ""

keyBasedOnLink = link.reference.link;
keyBasedOnFullPath = link.resolvedFile.path.replace(link.resolvedFile.name,"") + link.reference.link;
if(link?.resolvedFile)
keyBasedOnFullPath = link.resolvedFile.path.replace(link.resolvedFile.name,"") + link.reference.link;
else
keyBasedOnFullPath = link.ghostLink;

if(keyBasedOnLink===keyBasedOnFullPath) {
keyBasedOnFullPath=null;
Expand Down Expand Up @@ -203,6 +232,6 @@ export function getSNWCacheByFile(file: TFile): TransformedCache {
transformedCache.cacheMetaData = cachedMetaData;
transformedCache.createDate = Date.now();
cacheCurrentPages.set(file.path, transformedCache);

return transformedCache;
}
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ declare module "obsidian" {
position: Pos
}
}
iterateReferences:( cb: (sourcePath: string, reference: ReferenceCache) => void ) => void
}

interface Vault {
Expand All @@ -50,7 +51,8 @@ export interface Link {
position: Pos
}
resolvedFile: TFile
resolvedPaths: string[]
// resolvedPaths: string[]
ghostLink: string
sourceFile: TFile
excludedFile: boolean;
}
Expand Down
5 changes: 4 additions & 1 deletion src/ui/components/uic-ref-area.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ const getRefAreaItems = async (refType: string, key: string, filePath: string):

if(refType==="File") {
const allLinks: Link[] = getSnwAllLinksResolutions();
const incomingLinks = allLinks.filter(f=>f?.resolvedFile.path===filePath);
const incomingLinks = allLinks.filter(f=>{
if(!f?.resolvedFile) return false;
return f?.resolvedFile?.path===filePath;
});
countOfRefs = incomingLinks.length;
linksToLoop = incomingLinks;
} else {
Expand Down
5 changes: 4 additions & 1 deletion src/ui/headerRefCount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ function processHeader(mdView: MarkdownView) {
const allLinks: Link[] = getSnwAllLinksResolutions();
if(allLinks==undefined) return;

const incomingLinks = allLinks.filter(f=>f?.resolvedFile.path===mdView.file.path);
const incomingLinks = allLinks.filter(f=>{
if(!f?.resolvedFile) return false;
return f?.resolvedFile?.path===mdView.file.path;
});

let incomingLinksCount = incomingLinks.length;

Expand Down
6 changes: 1 addition & 5 deletions src/view-extensions/gutters-cm6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,7 @@ const ReferenceGutterExtension = gutter({
if(embed.position.start.line +1 === lineNumberInFile) {
for (const ref of transformedCache.embeds) {
if(ref?.references[0]?.excludedFile!=true && ref?.references.length>0 && ref?.pos.start.line+1 === lineNumberInFile) {
// @ts-ignore
let refOriginalLink = ref.references[0].reference.original;
if(refOriginalLink.substring(0,1)!="!")
refOriginalLink = "!" + refOriginalLink;
if( editorView.state.doc.lineAt(line.from).text.trim() === refOriginalLink) {
if( editorView.state.doc.lineAt(line.from).text.trim() === `![[${ref.key}]]`) {
if(thePlugin.snwAPI.enableDebugging.GutterEmbedCounter)
thePlugin.snwAPI.console("ReferenceGutterExtension New gutter", ref.references.length, "embed", ref.key, ref.key, "snw-embed-special" );
return new referenceGutterMarker(ref.references.length, "embed", ref.key, ref.references[0].resolvedFile.path.replace(".md",""), "snw-embed-special");
Expand Down
4 changes: 3 additions & 1 deletion src/view-extensions/references-cm6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ const constructWidgetForInlineReference = (refType: string, key: string, referen
}

if(matchKey===key) {
if(ref?.references[0]?.excludedFile!=true && ref?.references.length>=thePlugin.settings.minimumRefCountThreshold)
if( ref?.references[0]?.excludedFile!=true &&
ref?.references[0]?.resolvedFile &&
ref?.references.length>=thePlugin.settings.minimumRefCountThreshold)
return new InlineReferenceWidget(ref.references.length, ref.type, ref.key, ref.references[0].resolvedFile.path.replace(".md",""), null, ref.pos.start.line);
else
return null;
Expand Down
8 changes: 4 additions & 4 deletions src/view-extensions/references-preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class snwChildComponent extends MarkdownRenderChild {
if ( value.references[0]?.excludedFile!=true && value.references.length >= minRefCountThreshold &&
(value.pos.start.line >= this.sectionInfo?.lineStart && value.pos.end.line <= this.sectionInfo?.lineEnd) &&
!isThisAnEmbed ) {
const referenceElement = htmlDecorationForReferencesElement(value.references.length, "block", value.key, value.references[0].resolvedFile.path.replace(".md",""), "", value.pos.start.line);
const referenceElement = htmlDecorationForReferencesElement(value.references.length, "block", value.key, value.references[0]?.resolvedFile?.path.replace(".md",""), "", value.pos.start.line);
let blockElement: HTMLElement = this.containerEl.querySelector('p')
if (!blockElement) {
blockElement = this.containerEl.querySelector("li");
Expand All @@ -109,7 +109,7 @@ class snwChildComponent extends MarkdownRenderChild {
const embedKey = element.getAttribute('src');
for (const value of transformedCache.embeds) {
if (value.references[0]?.excludedFile!=true && value.references.length >= minRefCountThreshold && embedKey.endsWith(value.key)) {
const referenceElement = htmlDecorationForReferencesElement(value.references.length, "embed", value.key, value.references[0].resolvedFile.path.replace(".md",""), "", value.pos.start.line);
const referenceElement = htmlDecorationForReferencesElement(value.references.length, "embed", value.key, value.references[0]?.resolvedFile?.path.replace(".md",""), "", value.pos.start.line);
referenceElement.addClass('snw-embed-preview');
element.after(referenceElement);
break;
Expand All @@ -124,7 +124,7 @@ class snwChildComponent extends MarkdownRenderChild {
const textContext = headerKey.getAttribute("data-heading")
for (const value of transformedCache.headings) {
if (value.references[0]?.excludedFile!=true && value.references.length >= minRefCountThreshold && value.headerMatch === textContext) {
const referenceElement = htmlDecorationForReferencesElement(value.references.length, "heading", value.key, value.references[0].resolvedFile.path.replace(".md",""), "", value.pos.start.line);
const referenceElement = htmlDecorationForReferencesElement(value.references.length, "heading", value.key, value.references[0]?.resolvedFile?.path.replace(".md",""), "", value.pos.start.line);
referenceElement.addClass("snw-heading-preview");
this.containerEl.querySelector("h1,h2,h3,h4,h5,h6").insertAdjacentElement("beforeend", referenceElement);
break;
Expand All @@ -138,7 +138,7 @@ class snwChildComponent extends MarkdownRenderChild {
const link = element.getAttribute('data-href');
for (const value of transformedCache.links) {
if (value.references[0]?.excludedFile!=true && value.references.length >= minRefCountThreshold && (value.key === link || (value?.original!=undefined && value?.original.contains(link)))) {
const referenceElement = htmlDecorationForReferencesElement(value.references.length, "link", value.key, value.references[0].resolvedFile.path.replace(".md",""), "", value.pos.start.line);
const referenceElement = htmlDecorationForReferencesElement(value.references.length, "link", value.key, value.references[0]?.resolvedFile?.path.replace(".md",""), "", value.pos.start.line);
referenceElement.addClass('snw-link-preview');
element.after(referenceElement);
break;
Expand Down
5 changes: 3 additions & 2 deletions versions.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"0.0.01": "0.13.8",
"0.0.06": "0.16.2"
"1.1.1" : "1.1.5",
"0.0.06": "0.16.2",
"0.0.01": "0.13.8"
}

0 comments on commit fc4afe0

Please sign in to comment.