From dc648b0026d9ea1364feef5712507a05d2c4890a Mon Sep 17 00:00:00 2001 From: Dario Baumberger Date: Fri, 2 Feb 2024 18:27:00 +0100 Subject: [PATCH] added comments --- src/functions.ts | 37 +++++++++++++++++++++++++++++++------ src/main.ts | 6 ++++++ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/functions.ts b/src/functions.ts index 96c74c6..556b681 100644 --- a/src/functions.ts +++ b/src/functions.ts @@ -4,6 +4,9 @@ const MD_TAG = /!\[.*?\]\((.*?)\)/; const OBSIDIAN_TAG = /!\[\[(.*?)\]\]/; const IMG_TAG = //; +/** + * recognize and return text on images + */ export async function getText( imagePath: string, language = "eng" @@ -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#!:.?+=&%@!-/]*)?$", @@ -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); diff --git a/src/main.ts b/src/main.ts index 6fedb39..5cffe6e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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 { // todo avoid getFiles const files = this.app.vault.getFiles(); @@ -240,6 +243,9 @@ export default class ImageToTextOcrPlugin extends Plugin { } } + /** + * get path from selection if possible + */ async getSelectedImagePath(selection: string): Promise { let imagePath!: string | undefined; let fullPath!: string | undefined;