Jest tests & plugin use same removeHyperlinks() (#3)

* Add 'ts-jest' dependency - preparing to convert test to TypeScript

* Add minimal jest.config.js

This tells Jest "when you see a .ts file, use ts-jest to transform it"

* Convert Jest test file from JavaScript to TypeScript

* Extract removeHyperlinks() to its own file, so it can be tested

Pure refactoring - done with the Move facility in WebStorm.

* Make tests check the removeHyperlinks() that is used by main.ts

Jest test now imports removeHyperlinks() used by main.ts,
removing the original need to keep the two copies of
removeHyperlinks() in sync.
This commit is contained in:
Clare Macrae 2025-07-01 18:19:17 +01:00 committed by GitHub
parent fb13edaac8
commit ca69e9c411
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 40 additions and 65 deletions

3
jest.config.js Normal file
View file

@ -0,0 +1,3 @@
module.exports = {
preset: 'ts-jest'
};

34
main.ts
View file

@ -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;
}

View file

@ -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"
}

View file

@ -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', () => {

33
removeHyperlinks.ts Normal file
View file

@ -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;
}