mirror of
https://github.com/nathonius/obsidian-github-link.git
synced 2026-07-22 09:20:25 +00:00
Fix: More URL subtleties, and some unit tests
This commit is contained in:
parent
42d35e4c70
commit
ea163a31e0
2 changed files with 75 additions and 1 deletions
72
src/inline/view-plugin.spec.ts
Normal file
72
src/inline/view-plugin.spec.ts
Normal file
|
|
@ -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);
|
||||
})
|
||||
});
|
||||
});
|
||||
|
|
@ -36,6 +36,8 @@ class InlineTagWidget extends WidgetType {
|
|||
}
|
||||
}
|
||||
|
||||
export const matchRegexp = /(?<!]\()https:\/\/github\.com\/([A-z\d\-_~+#%&=*@?\/]*(.+[^\S\n ])*)(\.[^\n\s. ]+)?/g
|
||||
|
||||
export function createInlineViewPlugin(_plugin: GithubLinkPlugin) {
|
||||
class InlineViewPluginValue implements PluginValue {
|
||||
/**
|
||||
|
|
@ -47,7 +49,7 @@ export function createInlineViewPlugin(_plugin: GithubLinkPlugin) {
|
|||
public inlineTags: DecorationSet = Decoration.none;
|
||||
|
||||
private readonly matcher = new MatchDecorator({
|
||||
regexp: /(?<!\[.*?\]\()https:\/\/github\.com\/[^\s,)]+/g,
|
||||
regexp: matchRegexp,
|
||||
decorate: (add, from, to, match, _view) => {
|
||||
add(
|
||||
from,
|
||||
|
|
|
|||
Loading…
Reference in a new issue