-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Canvas Card and Md Files Content Scan Implementation
- Loading branch information
Ozan Tellioglu
committed
Feb 7, 2023
1 parent
496389a
commit 0dcd3bf
Showing
2 changed files
with
161 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import { TFile, App } from 'obsidian'; | ||
|
||
/* -------------------- LINK DETECTOR -------------------- */ | ||
|
||
type LinkType = 'markdown' | 'wiki' | 'wikiTransclusion' | 'mdTransclusion'; | ||
|
||
export interface LinkMatch { | ||
type: LinkType; | ||
match: string; | ||
linkText: string; | ||
sourceFilePath: string; | ||
} | ||
|
||
/** | ||
* | ||
* @param mdFile : File, of which the text content is scanned | ||
* @param app : Obsidian App | ||
* @param fileText : Optional, If file is not Md format, provide fileText to scan | ||
* @returns Promise<LinkMatch[]> | ||
*/ | ||
export const getAllLinkMatchesInFile = async (mdFile: TFile, app: App, fileText?: String): Promise<LinkMatch[]> => { | ||
const linkMatches: LinkMatch[] = []; | ||
if (fileText === undefined) { | ||
fileText = await app.vault.read(mdFile); | ||
} | ||
|
||
// --> Get All WikiLinks | ||
let wikiRegex = /\[\[.*?\]\]/g; | ||
let wikiMatches = fileText.match(wikiRegex); | ||
if (wikiMatches) { | ||
let fileRegex = /(?<=\[\[).*?(?=(\]|\|))/; | ||
let altRegex = /(?<=\|).*(?=]])/; | ||
|
||
for (let wikiMatch of wikiMatches) { | ||
// --> Check if it is Transclusion | ||
if (matchIsWikiTransclusion(wikiMatch)) { | ||
let fileName = getTransclusionFileName(wikiMatch); | ||
if (fileName !== '') { | ||
let linkMatch: LinkMatch = { | ||
type: 'wikiTransclusion', | ||
match: wikiMatch, | ||
linkText: fileName, | ||
sourceFilePath: mdFile.path, | ||
}; | ||
linkMatches.push(linkMatch); | ||
continue; | ||
} | ||
} | ||
// --> Normal Internal Link | ||
let fileMatch = wikiMatch.match(fileRegex); | ||
if (fileMatch) { | ||
// Web links are to be skipped | ||
if (fileMatch[0].startsWith('http')) continue; | ||
let linkMatch: LinkMatch = { | ||
type: 'wiki', | ||
match: wikiMatch, | ||
linkText: fileMatch[0], | ||
sourceFilePath: mdFile.path, | ||
}; | ||
linkMatches.push(linkMatch); | ||
} | ||
} | ||
} | ||
|
||
// --> Get All Markdown Links | ||
let markdownRegex = /\[(^$|.*?)\]\((.*?)\)/g; | ||
let markdownMatches = fileText.match(markdownRegex); | ||
if (markdownMatches) { | ||
let fileRegex = /(?<=\().*(?=\))/; | ||
let altRegex = /(?<=\[)(^$|.*?)(?=\])/; | ||
for (let markdownMatch of markdownMatches) { | ||
// --> Check if it is Transclusion | ||
if (matchIsMdTransclusion(markdownMatch)) { | ||
let fileName = getTransclusionFileName(markdownMatch); | ||
|
||
if (fileName !== '') { | ||
let linkMatch: LinkMatch = { | ||
type: 'mdTransclusion', | ||
match: markdownMatch, | ||
linkText: fileName, | ||
|
||
sourceFilePath: mdFile.path, | ||
}; | ||
linkMatches.push(linkMatch); | ||
continue; | ||
} | ||
} | ||
// --> Normal Internal Link | ||
let fileMatch = markdownMatch.match(fileRegex); | ||
if (fileMatch) { | ||
// Web links are to be skipped | ||
if (fileMatch[0].startsWith('http')) continue; | ||
let linkMatch: LinkMatch = { | ||
type: 'markdown', | ||
match: markdownMatch, | ||
linkText: fileMatch[0], | ||
|
||
sourceFilePath: mdFile.path, | ||
}; | ||
linkMatches.push(linkMatch); | ||
} | ||
} | ||
} | ||
return linkMatches; | ||
}; | ||
|
||
/* ---------- HELPERS ---------- */ | ||
|
||
const wikiTransclusionRegex = /\[\[(.*?)#.*?\]\]/; | ||
const wikiTransclusionFileNameRegex = /(?<=\[\[)(.*)(?=#)/; | ||
const wikiTransclusionBlockRef = /(?<=#).*?(?=]])/; | ||
|
||
const mdTransclusionRegex = /\[.*?]\((.*?)#.*?\)/; | ||
const mdTransclusionFileNameRegex = /(?<=\]\()(.*)(?=#)/; | ||
const mdTransclusionBlockRef = /(?<=#).*?(?=\))/; | ||
|
||
const matchIsWikiTransclusion = (match: string): boolean => { | ||
return wikiTransclusionRegex.test(match); | ||
}; | ||
|
||
const matchIsMdTransclusion = (match: string): boolean => { | ||
return mdTransclusionRegex.test(match); | ||
}; | ||
|
||
/** | ||
* @param match | ||
* @returns file name if there is a match or empty string if no match | ||
*/ | ||
const getTransclusionFileName = (match: string): string => { | ||
let isWiki = wikiTransclusionRegex.test(match); | ||
let isMd = mdTransclusionRegex.test(match); | ||
if (isWiki || isMd) { | ||
let fileNameMatch = match.match(isWiki ? wikiTransclusionFileNameRegex : mdTransclusionFileNameRegex); | ||
if (fileNameMatch) return fileNameMatch[0]; | ||
} | ||
return ''; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters