Skip to content

Commit

Permalink
added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dario-baumberger committed Feb 2, 2024
1 parent 40e30f4 commit dc648b0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
37 changes: 31 additions & 6 deletions src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ const MD_TAG = /!\[.*?\]\((.*?)\)/;
const OBSIDIAN_TAG = /!\[\[(.*?)\]\]/;
const IMG_TAG = /<img([\w\W]+?)\/>/;

/**
* recognize and return text on images
*/
export async function getText(
imagePath: string,
language = "eng"
Expand All @@ -19,6 +22,9 @@ export async function getText(
}
}

/**
* check if is a valid url
*/
export function isValidUrl(url: string): boolean {
const pattern = new RegExp(
"^(https?:\\/\\/)([\\w.-]+)(:\\d+)?(\\/[\\w#!:.?+=&%@!-/]*)?$",
Expand All @@ -28,42 +34,61 @@ export function isValidUrl(url: string): boolean {
return pattern.test(url);
}

/**
* check if string is a obsidian image tag
*/
export function isObsidianTag(string: string): boolean {
const pattern = OBSIDIAN_TAG;

return pattern.test(string);
}

/**
* Check if string is a markdown tag
*/
export function isMarkdownTag(string: string): boolean {
const pattern = MD_TAG;

return pattern.test(string);
return MD_TAG.test(string);
}

/**
* Check if string is an image tag
*/
export function isImgTag(string: string): boolean {
const pattern = IMG_TAG;

return pattern.test(string);
return IMG_TAG.test(string);
}

/**
* get value from src attribute from images
*/
export function extractSrc(selection: string): string | undefined {
const match = selection.match(/src\s*=\s*['"](.+?)['"]/);

return match && match[1] ? match[1] : undefined;
}

/**
* get path from markdown markup
*/
export function extractMdPath(selection: string): string | undefined {
const match = selection.match(MD_TAG);

return match && match[1] ? match[1] : undefined;
}

/**
* get path from obsidian markup
* get only file name of also have defined size
*/
export function extractObsidianPath(selection: string): string | undefined {
const match = selection.match(OBSIDIAN_TAG);

return match && match[1] ? match[1].split("|")[0] : undefined;
}

/**
* check if file is allowed.
* checky only based on file ending, not on it's content
*/
export function checkFileType(filePath: string): boolean {
const imageRegex = /\.(jpg|jpeg|png|gif|bmp|pbm|webp)$/i;
return imageRegex.test(filePath);
Expand Down
6 changes: 6 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ export default class ImageToTextOcrPlugin extends Plugin {
await this.saveData(this.settings);
}

/**
* get file path based on filename
*/
async resolveImagePath(fileName: string): Promise<string> {
// todo avoid getFiles
const files = this.app.vault.getFiles();
Expand All @@ -240,6 +243,9 @@ export default class ImageToTextOcrPlugin extends Plugin {
}
}

/**
* get path from selection if possible
*/
async getSelectedImagePath(selection: string): Promise<string> {
let imagePath!: string | undefined;
let fullPath!: string | undefined;
Expand Down

0 comments on commit dc648b0

Please sign in to comment.