diff --git a/CHANGELOG.md b/CHANGELOG.md index ece16a6..65b0cdb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# 0.24.2 (2025-02-05) +- Fixed how links are displayed. Now you can use links as `a(href)` or `a(href, name)`. + # 0.24.1 (2025-02-05) - Added `path` columns to `tags` and `tasks` tables. Thanks to that you can use `NATURAL JOIN`s now to connect join them. diff --git a/manifest.json b/manifest.json index b22b6cf..e316314 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "sqlseal", "name": "SQLSeal", - "version": "0.24.1", + "version": "0.24.2", "minAppVersion": "0.15.0", "description": "Use SQL in your notes to query your vault files and CSV content.", "author": "hypersphere", diff --git a/package.json b/package.json index c437e86..39938be 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sqlseal", - "version": "0.24.1", + "version": "0.24.2", "description": "A plugin for Obsidian that allows you to run SQL queries on your notes.", "main": "main.js", "scripts": { diff --git a/src/utils/ui.test.ts b/src/utils/ui.test.ts new file mode 100644 index 0000000..29b2621 --- /dev/null +++ b/src/utils/ui.test.ts @@ -0,0 +1,49 @@ +import { renderLink } from "./ui" + +const makeApp = () => ({ metadataCache: { getFirstLinkpathDest: jest.fn(p => ({ path: p })) } }) + +describe('UI', () => { + it('should properly render links', () => { + const app = makeApp() + const createEl = jest.fn() + renderLink(app as any, createEl)(['a/b/c.md']) + expect(createEl).toHaveBeenCalledWith('a', { + cls: 'internal-link', + href: 'a/b/c.md', + text: 'c' + }) + }) + + it('should properly render link with name provided', () => { + const app = makeApp() + const createEl = jest.fn() + renderLink(app as any, createEl)(['a/b/c.md', 'Name']) + expect(createEl).toHaveBeenCalledWith('a', { + cls: 'internal-link', + href: 'a/b/c.md', + text: 'Name' + }) + }) + + it('should properly render link with external url', () => { + const app = makeApp() + const createEl = jest.fn() + renderLink(app as any, createEl)(['https://wikipedia.org', 'Wikipedia']) + expect(createEl).toHaveBeenCalledWith('a', { + cls: '', + href: 'https://wikipedia.org', + text: 'Wikipedia' + }) + }) + + it('should properly render link using wikilink tag', () => { + const app = makeApp() + const createEl = jest.fn() + renderLink(app as any, createEl)(['[[a/b/c.md|Alias]]']) + expect(createEl).toHaveBeenCalledWith('a', { + cls: 'internal-link', + href: 'a/b/c.md', + text: 'Alias' + }) + }) +}) \ No newline at end of file diff --git a/src/utils/ui.ts b/src/utils/ui.ts index 95f5a40..8009d8f 100644 --- a/src/utils/ui.ts +++ b/src/utils/ui.ts @@ -1,4 +1,4 @@ -import { App, getLinkpath, parseLinktext } from "obsidian" +import { App } from "obsidian" import { CellParserRegistar } from "src/cellParser" export const displayNotice = (el: HTMLElement, text: string) => { @@ -28,9 +28,21 @@ const renderCheckbox = (app: App) => ([value]: [string]) => { } return el } -const renderLink = (app: App) => ([name, href]: [string, string]) => { - if (!href) { - href = name + +const removeExtension = (filename: string) => { + const parts = filename.split('.') + if (parts.length > 1) { + return parts.slice(0, -1).join('.') + } + return filename +} + + +type LinkType = [string] | [string, string] + +export const renderLink = (app: App, create = createEl) => ([href, name]: LinkType) => { + if (!name) { + name = href } href = href.trim() let cls = '' @@ -54,10 +66,13 @@ const renderLink = (app: App) => ([name, href]: [string, string]) => { if (fileHref.endsWith('.md')) { fileHref = fileHref.slice(0, -3) } - href = href + if (name === href) { + const components = href.split('/') + name = removeExtension(components[components.length - 1]) + } cls = 'internal-link' } - const el = createEl('a', { text: name, href, cls }) + const el = create('a', { text: name, href, cls }) return el } diff --git a/versions.json b/versions.json index 93abd1e..ef6abf9 100644 --- a/versions.json +++ b/versions.json @@ -44,5 +44,6 @@ "0.23.1": "0.15.0", "0.23.2": "0.15.0", "0.24.0": "0.15.0", - "0.24.1": "0.15.0" + "0.24.1": "0.15.0", + "0.24.2": "0.15.0" } \ No newline at end of file