fix: fixed how links behave and parameter order

This commit is contained in:
Kacper Kula 2025-02-05 17:46:11 +00:00
parent 5cf7f247de
commit c2fe8b0eff
6 changed files with 77 additions and 9 deletions

View file

@ -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.

View file

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

View file

@ -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": {

49
src/utils/ui.test.ts Normal file
View file

@ -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'
})
})
})

View file

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

View file

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