obsidian#LinkCache TypeScript Examples

The following examples show how to use obsidian#LinkCache. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: utils.ts    From obsidian-switcher-plus with GNU General Public License v3.0 7 votes vote down vote up
export function getLinkType(linkCache: LinkCache): LinkType {
  let type = LinkType.None;

  if (linkCache) {
    // remove the display text before trying to parse the link target
    const linkStr = linkCache.link.split('|')[0];

    if (linkStr.includes('#^')) {
      type = LinkType.Block;
    } else if (linkStr.includes('#')) {
      type = LinkType.Heading;
    } else {
      type = LinkType.Normal;
    }
  }

  return type;
}
Example #2
Source File: symbolHandler.ts    From obsidian-switcher-plus with GNU General Public License v3.0 6 votes vote down vote up
private addLinksFromSource(linkData: LinkCache[], symbolList: SymbolInfo[]): void {
    const { settings } = this;
    linkData = linkData ?? [];

    if (settings.isSymbolTypeEnabled(SymbolType.Link)) {
      for (const link of linkData) {
        const type = getLinkType(link);
        const isExcluded = (settings.excludeLinkSubTypes & type) === type;

        if (!isExcluded) {
          symbolList.push({
            type: 'symbolInfo',
            symbol: link,
            symbolType: SymbolType.Link,
          });
        }
      }
    }
  }
Example #3
Source File: fileCachedMetadata.fixture.ts    From obsidian-switcher-plus with GNU General Public License v3.0 6 votes vote down vote up
export function getLinks(): LinkCache[] {
  const l1 = makeLink(
    'Format your notes#^e476cc',
    '[[Format your notes#^e476cc]]',
    'Format your notes > ^e476cc',
    makeLoc(12, 70, 172),
    makeLoc(12, 99, 201),
  );

  const l2 = makeLink(
    'internal like to no-exist',
    '[[internal like to no-exist|with-alt-text]]',
    'with-alt-text',
    makeLoc(12, 136, 238),
    makeLoc(12, 179, 281),
  );

  return [l1, l2];
}
Example #4
Source File: tasks.ts    From obsidian-checklist-plugin with MIT License 6 votes vote down vote up
formTodo = (line: string, file: FileInfo, links: LinkCache[], lineNum: number, tagMeta?: TagMeta): TodoItem => {
  const relevantLinks = links
    .filter((link) => link.position.start.line === lineNum)
    .map((link) => ({ filePath: link.link, linkName: link.displayText }))
  const linkMap = mapLinkMeta(relevantLinks)
  const rawText = extractTextFromTodoLine(line)
  const spacesIndented = getIndentationSpacesFromTodoLine(line)
  const tagStripped = removeTagFromText(rawText, tagMeta?.main)
  const md = new MD().use(commentPlugin).use(linkPlugin(linkMap)).use(tagPlugin).use(highlightPlugin)
  return {
    mainTag: tagMeta?.main,
    subTag: tagMeta?.sub,
    checked: todoLineIsChecked(line),
    filePath: file.file.path,
    fileName: file.file.name,
    fileLabel: getFileLabelFromName(file.file.name),
    fileCreatedTs: file.file.stat.ctime,
    rawHTML: md.render(tagStripped),
    line: lineNum,
    spacesIndented,
    fileInfo: file,
    originalText: rawText,
  }
}
Example #5
Source File: links-handler.ts    From obsidian-consistent-attachments-and-links with MIT License 6 votes vote down vote up
getAllCachedLinksToFile(filePath: string): { [notePath: string]: LinkCache[]; } {
		let allLinks: { [notePath: string]: LinkCache[]; } = {};
		let notes = this.app.vault.getMarkdownFiles();

		if (notes) {
			for (let note of notes) {
				if (note.path == filePath)
					continue;

				//!!! this can return undefined if note was just updated
				let links = this.app.metadataCache.getCache(note.path)?.links;

				if (links) {
					for (let link of links) {
						let linkFullPath = this.getFullPathForLink(link.link, note.path);
						if (linkFullPath == filePath) {
							if (!allLinks[note.path])
								allLinks[note.path] = [];
							allLinks[note.path].push(link);
						}
					}
				}
			}
		}

		return allLinks;
	}
Example #6
Source File: links-handler.ts    From obsidian-consistent-attachments-and-links with MIT License 6 votes vote down vote up
getAllBadLinks(): { [notePath: string]: LinkCache[]; } {
		let allLinks: { [notePath: string]: LinkCache[]; } = {};
		let notes = this.app.vault.getMarkdownFiles();

		if (notes) {
			for (let note of notes) {
				if (this.isPathIgnored(note.path))
					continue;

				//!!! this can return undefined if note was just updated
				let links = this.app.metadataCache.getCache(note.path)?.links;

				if (links) {
					for (let link of links) {
						if (link.link.startsWith("#")) //internal section link
							continue;

						if (this.checkIsCorrectWikiLink(link.original))
							continue;

						let file = this.getFileByLink(link.link, note.path);
						if (!file) {
							if (!allLinks[note.path])
								allLinks[note.path] = [];
							allLinks[note.path].push(link);
						}
					}
				}
			}
		}

		return allLinks;
	}
Example #7
Source File: links-handler.ts    From obsidian-consistent-attachments-and-links with MIT License 6 votes vote down vote up
getAllGoodLinks(): { [notePath: string]: LinkCache[]; } {
		let allLinks: { [notePath: string]: LinkCache[]; } = {};
		let notes = this.app.vault.getMarkdownFiles();

		if (notes) {
			for (let note of notes) {
				if (this.isPathIgnored(note.path))
					continue;

				//!!! this can return undefined if note was just updated
				let links = this.app.metadataCache.getCache(note.path)?.links;

				if (links) {
					for (let link of links) {
						if (link.link.startsWith("#")) //internal section link
							continue;

						if (this.checkIsCorrectWikiLink(link.original))
							continue;

						let file = this.getFileByLink(link.link, note.path);
						if (file) {
							if (!allLinks[note.path])
								allLinks[note.path] = [];
							allLinks[note.path].push(link);
						}
					}
				}
			}
		}

		return allLinks;
	}
Example #8
Source File: links-handler.ts    From obsidian-consistent-attachments-and-links with MIT License 6 votes vote down vote up
getAllWikiLinks(): { [notePath: string]: LinkCache[]; } {
		let allLinks: { [notePath: string]: LinkCache[]; } = {};
		let notes = this.app.vault.getMarkdownFiles();

		if (notes) {
			for (let note of notes) {
				if (this.isPathIgnored(note.path))
					continue;

				//!!! this can return undefined if note was just updated
				let links = this.app.metadataCache.getCache(note.path)?.links;

				if (links) {
					for (let link of links) {
						if (!this.checkIsCorrectWikiLink(link.original))
							continue;

						if (!allLinks[note.path])
							allLinks[note.path] = [];
						allLinks[note.path].push(link);

					}
				}
			}
		}

		return allLinks;
	}
Example #9
Source File: linkAttributes.ts    From obsidian_supercharged_links with MIT License 6 votes vote down vote up
export function updateVisibleLinks(app: App, plugin: SuperchargedLinks) {
    const settings = plugin.settings;
    app.workspace.iterateRootLeaves((leaf) => {
        if (leaf.view instanceof MarkdownView && leaf.view.file) {
            const file: TFile = leaf.view.file;
            const cachedFile = app.metadataCache.getFileCache(file)
            if (cachedFile.links) {
                cachedFile.links.forEach((link: LinkCache) => {
                    const fileName = file.path.replace(/(.*).md/, "$1")
                    const dest = app.metadataCache.getFirstLinkpathDest(link.link, fileName)
                    if (dest) {
                        const new_props = fetchTargetAttributesSync(app, settings, dest, false)
                        const internalLinks = leaf.view.containerEl.querySelectorAll(`a.internal-link[href="${link.link}"]`)
                        internalLinks.forEach((internalLink: HTMLElement) => setLinkNewProps(internalLink, new_props))
                    }
                })
            }
        }
    })
}
Example #10
Source File: links-handler.ts    From obsidian-consistent-attachments-and-links with MIT License 5 votes vote down vote up
async getAllBadSectionLinks(): Promise<{ [notePath: string]: LinkCache[]; }> {
		let allLinks: { [notePath: string]: LinkCache[]; } = {};
		let notes = this.app.vault.getMarkdownFiles();

		if (notes) {
			for (let note of notes) {
				if (this.isPathIgnored(note.path))
					continue;

				//!!! this can return undefined if note was just updated
				let links = this.app.metadataCache.getCache(note.path)?.links;
				if (links) {
					for (let link of links) {
						if (this.checkIsCorrectWikiLink(link.original))
							continue;

						let li = this.splitLinkToPathAndSection(link.link);
						if (!li.hasSection)
							continue;

						let file = this.getFileByLink(link.link, note.path);
						if (file) {
							if (file.extension === "pdf" && li.section.startsWith("page=")) {
								continue;
							}

							let text = await this.app.vault.read(file);
							let section = Utils.normalizeLinkSection(li.section);

							if (section.startsWith("^")) //skip ^ links
								continue;

							let regex = /[ !@$%^&*()-=_+\\/;'\[\]\"\|\?.\,\<\>\`\~\{\}]/gim;
							text = text.replace(regex, '');
							section = section.replace(regex, '');

							if (!text.contains("#" + section)) {
								if (!allLinks[note.path])
									allLinks[note.path] = [];
								allLinks[note.path].push(link);
							}
						}
					}
				}
			}
		}

		return allLinks;
	}
Example #11
Source File: links-handler.ts    From obsidian-consistent-attachments-and-links with MIT License 5 votes vote down vote up
async getLinksFromNote(notePath: string): Promise<LinkCache[]> {
		let file = this.getFileByPath(notePath);
		if (!file) {
			console.error(this.consoleLogPrefix + "cant get embeds, file not found: " + notePath);
			return;
		}

		let text = await this.app.vault.read(file);

		let links: LinkCache[] = [];

		let elements = text.match(markdownLinkOrEmbedRegexG);
		if (elements != null && elements.length > 0) {
			for (let el of elements) {
				let alt = el.match(markdownLinkOrEmbedRegex)[1];
				let link = el.match(markdownLinkOrEmbedRegex)[2];

				let emb: LinkCache = {
					link: link,
					displayText: alt,
					original: el,
					position: {
						start: {
							col: 0,//todo
							line: 0,
							offset: 0
						},
						end: {
							col: 0,//todo
							line: 0,
							offset: 0
						}
					}
				};

				links.push(emb);
			}
		}
		return links;
	}