mirror of
https://github.com/alphahasher/obsidian-remove-links.git
synced 2026-07-22 05:51:41 +00:00
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:
parent
fb13edaac8
commit
ca69e9c411
5 changed files with 40 additions and 65 deletions
3
jest.config.js
Normal file
3
jest.config.js
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
module.exports = {
|
||||
preset: 'ts-jest'
|
||||
};
|
||||
34
main.ts
34
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;
|
||||
}
|
||||
|
|
@ -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"
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
33
removeHyperlinks.ts
Normal 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;
|
||||
}
|
||||
Loading…
Reference in a new issue