diff --git a/jest.config.js b/jest.config.js new file mode 100644 index 0000000..6743f9d --- /dev/null +++ b/jest.config.js @@ -0,0 +1,3 @@ +module.exports = { + preset: 'ts-jest' +}; diff --git a/main.ts b/main.ts index e7f9231..36cfa3e 100644 --- a/main.ts +++ b/main.ts @@ -1,4 +1,5 @@ import { Editor, MarkdownView, Notice, Plugin } from 'obsidian'; +import { removeHyperlinks } from './removeHyperlinks'; export default class HyperlinkRemover extends Plugin { async onload() { @@ -79,36 +80,3 @@ export default class HyperlinkRemover extends Plugin { } } -function removeHyperlinks(text:string): string { - let result = text; - let match; - const regex = /\[((?:[^\]\\]|\\.|\](?!\())*?)\]\(/g; - - while ((match = regex.exec(text)) !== null) { - const linkText = match[1]; - const startPos = match.index; - const urlStartPos = match.index + match[0].length; - - // Find the matching closing parenthesis - let parenCount = 1; - let urlEndPos = urlStartPos; - - while (urlEndPos < text.length && parenCount > 0) { - if (text[urlEndPos] === '(') { - parenCount++; - } else if (text[urlEndPos] === ')') { - parenCount--; - } - if (parenCount > 0) { - urlEndPos++; - } - } - - if (parenCount === 0) { - const fullMatch = text.substring(startPos, urlEndPos + 1); - result = result.replace(fullMatch, linkText); - } - } - - return result; -} \ No newline at end of file diff --git a/package.json b/package.json index 0d9040b..0f04f53 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "esbuild": "0.17.3", "jest": "^30.0.3", "obsidian": "latest", + "ts-jest": "^29.4.0", "tslib": "2.4.0", "typescript": "4.7.4" } diff --git a/removeHyperlinks.test.js b/removeHyperlinks.test.ts similarity index 72% rename from removeHyperlinks.test.js rename to removeHyperlinks.test.ts index b23a69e..ae00892 100644 --- a/removeHyperlinks.test.js +++ b/removeHyperlinks.test.ts @@ -1,36 +1,6 @@ -function removeHyperlinks(text) { - let result = text; - let match; - const regex = /\[((?:[^\]\\]|\\.|\](?!\())*?)\]\(/g; +import { describe, expect, test } from '@jest/globals'; - while ((match = regex.exec(text)) !== null) { - const linkText = match[1]; - const startPos = match.index; - const urlStartPos = match.index + match[0].length; - - // Find the matching closing parenthesis - let parenCount = 1; - let urlEndPos = urlStartPos; - - while (urlEndPos < text.length && parenCount > 0) { - if (text[urlEndPos] === '(') { - parenCount++; - } else if (text[urlEndPos] === ')') { - parenCount--; - } - if (parenCount > 0) { - urlEndPos++; - } - } - - if (parenCount === 0) { - const fullMatch = text.substring(startPos, urlEndPos + 1); - result = result.replace(fullMatch, linkText); - } - } - - return result; -} +import { removeHyperlinks } from './removeHyperlinks'; describe('Remove Hyper Links Tests', () => { test('remove hyperlinks from text', () => { diff --git a/removeHyperlinks.ts b/removeHyperlinks.ts new file mode 100644 index 0000000..153d946 --- /dev/null +++ b/removeHyperlinks.ts @@ -0,0 +1,33 @@ +export function removeHyperlinks(text:string): string { + let result = text; + let match; + const regex = /\[((?:[^\]\\]|\\.|\](?!\())*?)\]\(/g; + + while ((match = regex.exec(text)) !== null) { + const linkText = match[1]; + const startPos = match.index; + const urlStartPos = match.index + match[0].length; + + // Find the matching closing parenthesis + let parenCount = 1; + let urlEndPos = urlStartPos; + + while (urlEndPos < text.length && parenCount > 0) { + if (text[urlEndPos] === '(') { + parenCount++; + } else if (text[urlEndPos] === ')') { + parenCount--; + } + if (parenCount > 0) { + urlEndPos++; + } + } + + if (parenCount === 0) { + const fullMatch = text.substring(startPos, urlEndPos + 1); + result = result.replace(fullMatch, linkText); + } + } + + return result; +}