From 42d35e4c7070d2ec9f3bacf7f4a0561d9d7346bb Mon Sep 17 00:00:00 2001 From: red Date: Fri, 20 Dec 2024 09:41:26 +0100 Subject: [PATCH 1/6] Fix: Match links to a single file with their dot extensions --- src/inline/view-plugin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/inline/view-plugin.ts b/src/inline/view-plugin.ts index 9860d0b..355218c 100644 --- a/src/inline/view-plugin.ts +++ b/src/inline/view-plugin.ts @@ -47,7 +47,7 @@ export function createInlineViewPlugin(_plugin: GithubLinkPlugin) { public inlineTags: DecorationSet = Decoration.none; private readonly matcher = new MatchDecorator({ - regexp: /(? { add( from, From ea163a31e0d6e85ee62997aaaa9b626d0df6bb50 Mon Sep 17 00:00:00 2001 From: red Date: Mon, 30 Dec 2024 20:49:05 +0100 Subject: [PATCH 2/6] Fix: More URL subtleties, and some unit tests --- src/inline/view-plugin.spec.ts | 72 ++++++++++++++++++++++++++++++++++ src/inline/view-plugin.ts | 4 +- 2 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 src/inline/view-plugin.spec.ts diff --git a/src/inline/view-plugin.spec.ts b/src/inline/view-plugin.spec.ts new file mode 100644 index 0000000..35f7232 --- /dev/null +++ b/src/inline/view-plugin.spec.ts @@ -0,0 +1,72 @@ +import { expect, jest, test, describe, beforeEach } from "@jest/globals"; +import { matchRegexp } from './view-plugin'; + +// Some potential URLs: +const GITHUB_URLS = [ + // USER/ORG + 'https://github.com/nathonius', + // REPO + 'https://github.com/nathonius/obsidian-github-link', + 'https://github.com/nathonius/obsidian-github-link/tree/main', + 'https://github.com/joshleaves/obsidian-github-link/tree/fix/156-linking-to-file', + 'https://github.com/nathonius/obsidian-github-link/blob/main/src/github/url-parse.ts', + 'https://github.com/nathonius/obsidian-github-link/blob/main/src/inline/view-plugin.ts#L21', + 'https://github.com/nathonius/obsidian-github-link/blob/main/src/inline/view-plugin.ts#L13-32', + // ISSUES + 'https://github.com/nathonius/obsidian-github-link/issues', + 'https://github.com/nathonius/obsidian-github-link/issues/156', + // PULLS + 'https://github.com/nathonius/obsidian-github-link/pulls', + 'https://github.com/nathonius/obsidian-github-link/pull/157', + 'https://github.com/nathonius/obsidian-github-link/pull/157/commits', + 'https://github.com/nathonius/obsidian-github-link/pull/157/commits/42d35e4c7070d2ec9f3bacf7f4a0561d9d7346bb', + 'https://github.com/nathonius/obsidian-github-link/pull/157/files', +] + +describe('matchRegexp', () => { + test('matches GitHub URLs on their own', () => { + GITHUB_URLS.forEach(url => { + const text = `${url}` // (...) + const match = text.match(matchRegexp); + expect(match).not.toBeNull(); + expect(match![0]).toBe(url); + }); + }); + + test('does not match URLs inside markdown links', () => { + GITHUB_URLS.forEach(url => { + const text = `[A link to GitHub](${url})`; + const match = text.match(matchRegexp); + expect(match).toBeNull(); + }); + }); + + test('matches URLs ending a sentence', () => { + GITHUB_URLS.forEach(url => { + const text = `That's the end of the line for ${url}.`; + const match = text.match(matchRegexp); + expect(match).not.toBeNull(); + expect(match![0]).toBe(url); + }); + }); + + test('matches URLs ending a sentence that segues to another', () => { + GITHUB_URLS.forEach(url => { + const text = `That's the end of the line for ${url}. But not for this variable!`; + const match = text.match(matchRegexp); + expect(match).not.toBeNull(); + expect(match![0]).toBe(url); + }); + }); + + test('matches URLs in text with multiple lines', () => { + GITHUB_URLS.forEach(url => { + const text = `Some text + ${url} + more text`; + const match = text.match(matchRegexp); + expect(match).not.toBeNull(); + expect(match![0]).toBe(url); + }) + }); +}); diff --git a/src/inline/view-plugin.ts b/src/inline/view-plugin.ts index 355218c..0a02268 100644 --- a/src/inline/view-plugin.ts +++ b/src/inline/view-plugin.ts @@ -36,6 +36,8 @@ class InlineTagWidget extends WidgetType { } } +export const matchRegexp = /(? { add( from, From 0257f4909f61b1225f2be2120dd26c4617f1ad88 Mon Sep 17 00:00:00 2001 From: red Date: Mon, 30 Dec 2024 20:59:32 +0100 Subject: [PATCH 3/6] Fix: Simplified Regexp --- src/inline/view-plugin.spec.ts | 4 ++-- src/inline/view-plugin.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/inline/view-plugin.spec.ts b/src/inline/view-plugin.spec.ts index 35f7232..2abbf72 100644 --- a/src/inline/view-plugin.spec.ts +++ b/src/inline/view-plugin.spec.ts @@ -1,4 +1,4 @@ -import { expect, jest, test, describe, beforeEach } from "@jest/globals"; +import { expect, test, describe } from "@jest/globals"; import { matchRegexp } from './view-plugin'; // Some potential URLs: @@ -63,7 +63,7 @@ describe('matchRegexp', () => { GITHUB_URLS.forEach(url => { const text = `Some text ${url} - more text`; + more text.`; const match = text.match(matchRegexp); expect(match).not.toBeNull(); expect(match![0]).toBe(url); diff --git a/src/inline/view-plugin.ts b/src/inline/view-plugin.ts index 0a02268..c23877e 100644 --- a/src/inline/view-plugin.ts +++ b/src/inline/view-plugin.ts @@ -36,7 +36,7 @@ class InlineTagWidget extends WidgetType { } } -export const matchRegexp = /(? Date: Sat, 4 Jan 2025 23:05:34 +0100 Subject: [PATCH 4/6] Fix: Don't capture first word after repo --- src/inline/view-plugin.spec.ts | 9 +++++++++ src/inline/view-plugin.ts | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/inline/view-plugin.spec.ts b/src/inline/view-plugin.spec.ts index 2abbf72..c6af686 100644 --- a/src/inline/view-plugin.spec.ts +++ b/src/inline/view-plugin.spec.ts @@ -67,6 +67,15 @@ describe('matchRegexp', () => { const match = text.match(matchRegexp); expect(match).not.toBeNull(); expect(match![0]).toBe(url); + }); + }); + + test('matches URLs in text followed by another text', () => { + GITHUB_URLS.forEach(url => { + const text = `${url} is a cool plugin`; + const match = text.match(matchRegexp); + expect(match).not.toBeNull(); + expect(match![0]).toBe(url); }) }); }); diff --git a/src/inline/view-plugin.ts b/src/inline/view-plugin.ts index c23877e..46374c9 100644 --- a/src/inline/view-plugin.ts +++ b/src/inline/view-plugin.ts @@ -36,7 +36,7 @@ class InlineTagWidget extends WidgetType { } } -export const matchRegexp = /(? Date: Sat, 18 Jan 2025 02:22:37 +0100 Subject: [PATCH 5/6] Fix: Add more edge cases in tests and rework regexp --- src/inline/view-plugin.spec.ts | 42 ++++++++++++++++++++++++++++++++++ src/inline/view-plugin.ts | 2 +- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/inline/view-plugin.spec.ts b/src/inline/view-plugin.spec.ts index c6af686..cb15d77 100644 --- a/src/inline/view-plugin.spec.ts +++ b/src/inline/view-plugin.spec.ts @@ -21,6 +21,11 @@ const GITHUB_URLS = [ 'https://github.com/nathonius/obsidian-github-link/pull/157/commits', 'https://github.com/nathonius/obsidian-github-link/pull/157/commits/42d35e4c7070d2ec9f3bacf7f4a0561d9d7346bb', 'https://github.com/nathonius/obsidian-github-link/pull/157/files', + // MORE EDGE CASES + 'https://github.com/kwsch/pk3DS/blob/e40d3ce5548d75821f31785dc88cd465610530a6/pk3DS.Core/CTR/Images/BCLIM.cs', + 'https://github.com/kwsch/png2bclim/blob/master/png2bclim/BCLIM.cs', + 'https://github.com/kwsch/png2bclim', + 'https://github.com/ihaveamac/3DS-rom-tools/wiki/Extract-a-game-or-application-in-.3ds-or-.cci-format' ] describe('matchRegexp', () => { @@ -50,6 +55,34 @@ describe('matchRegexp', () => { }); }); + test('matches URLs followed by punctuation', () => { + GITHUB_URLS.forEach(url => { + const text = `First ${url}, then some text.`; + const match = text.match(matchRegexp); + expect(match).not.toBeNull(); + expect(match![0]).toBe(url); + }); + }); + + test('matches URLs ending a sentence with an interrogative', () => { + GITHUB_URLS.forEach(url => { + const text = `You heard about ${url}?`; + const match = text.match(matchRegexp); + expect(match).not.toBeNull(); + expect(match![0]).toBe(url); + }); + }); + + test('matches URLs followed by multiple punctuation', () => { + GITHUB_URLS.forEach(url => { + const text = `A test for ${url}...`; + const match = text.match(matchRegexp); + expect(match).not.toBeNull(); + expect(match![0]).toBe(url); + }); + }); + + test('matches URLs ending a sentence that segues to another', () => { GITHUB_URLS.forEach(url => { const text = `That's the end of the line for ${url}. But not for this variable!`; @@ -78,4 +111,13 @@ describe('matchRegexp', () => { expect(match![0]).toBe(url); }) }); + + test('matches URLs quoted', () => { + GITHUB_URLS.forEach(url => { + const text = `Obama chuckled: you mean "${url}" ?`; + const match = text.match(matchRegexp); + expect(match).not.toBeNull(); + expect(match![0]).toBe(url); + }) + }); }); diff --git a/src/inline/view-plugin.ts b/src/inline/view-plugin.ts index 46374c9..63856e6 100644 --- a/src/inline/view-plugin.ts +++ b/src/inline/view-plugin.ts @@ -36,7 +36,7 @@ class InlineTagWidget extends WidgetType { } } -export const matchRegexp = /(? Date: Sun, 19 Jan 2025 18:50:40 -0500 Subject: [PATCH 6/6] remove unnecessary escapes --- src/inline/view-plugin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/inline/view-plugin.ts b/src/inline/view-plugin.ts index 63856e6..8b5d7b7 100644 --- a/src/inline/view-plugin.ts +++ b/src/inline/view-plugin.ts @@ -36,7 +36,7 @@ class InlineTagWidget extends WidgetType { } } -export const matchRegexp = /(?