Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
feat: add lookup of multiple properties at once
Browse files Browse the repository at this point in the history
  • Loading branch information
peanball committed Nov 25, 2022
1 parent ae86738 commit 2894b68
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 186 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
dist
dist-ssr
*.local
.idea
55 changes: 48 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,41 @@
# Dynamic Lookup Plugin

This plugin allows you to look up properties on a target page and place the value of the property in your text.
This plugin allows you to look up properties on a target page and place their values in your text.

Great for macros that automatically augment links to specific pages or tags.

## Demo
## Usage

The plugin is used by adding a `{{renderer}}` macros with target `:lookup`:

```clojure
{{renderer :lookup, page, property, template, fallbackTemplate}}
{{renderer :lookup, page, propertyNames, template, [fallbackTemplate]}}
```

- `page` is the name (`:block/original-name`, i.e. the name you see in the UI) of the page
- `property` is the name of the property
- `propertyNames` is a list of property names, separated by `:`. Empty values will be ignored. You could write `:prop1:prop2` without negative effect.
- `template` is an optional formatter for the value. This can contain HTML to wrap the property value as needed. By default, the property value is added verbatim in a `<span>`.
- `fallbackTemplate` is an optional formatter for the value. This can contain test that is printed when the page or property value could not be found.

When the `page` could not be found, or the `page` does not have the property defined via `property`, the renderer block uses the `fallbackTemplate` if one is defined.
When the `page` could not be found, or the `page` does not have any of the properties defined via `propertyNames`, the renderer block uses the `fallbackTemplate`, if one is defined.
If none is defined, the block is replaced with nothing. This makes it safe to use in macros, even if e.g. the target page does not exist (yet).

### Use Case
When only some properties requested in `propertyNames` are found on the target page, the ones not found are replaced with nothing (empty string). Any placeholders that is not listed in `propertyNames` will not be touched.

> ⚠️ **Deprecated:**
>
> The placeholder `$value` is replaced by the first property in `propertyNames`.
> This is for backward compatibility with uses of the plugin before 1.2.0.
## Use Cases and Examples

### Appending a single Property Value to a Tag

I've defined a macro `jira` in the custom.edn file that links to my notes to specific JIRA tickets where I use the issue ID as tag:

```clojure
:macros {
"jira" "#$1 {{renderer :lookup, $1, summary, ($value)}}"
"jira" "#$1 {{renderer :lookup, $1, summary, ($summary)}}"
}
```

Expand All @@ -36,6 +47,36 @@ The result looks something like this:

You could also use it to link to the actual issue. In a macro `$1`, `$2`, etc. can be used to define the template dynamically if needed.

### Linking to a custom URL defined in a Property

Another macro looks something like this:

```clojure
:macros {
"supportcase" "[[$1]] {{renderer :lookup, $1, url:summary, / <a href='$url'>$summary</a>, <a href='https://support.example.com/search/$1'>🔍</a>}}"
}
```

This allows me to use:

```clojure
{{supportcase 1234}}
```

which shows as:

```
[[1234]] / Case Summary
```

where `Case Summary` links to `:url` of the page `1234`.

And when there is no page `1234`, it adds a link to search for the term in the target system.

```
[[1234]] 🔍
```

## Building

> 🏷 Currently this only builds on Node <= 16.
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
"@semantic-release/git": "10.0.1",
"@semantic-release/npm": "9.0.1",
"@types/node": "18.11.9",
"@typescript-eslint/eslint-plugin": "5.42.1",
"@typescript-eslint/parser": "5.42.1",
"conventional-changelog-conventionalcommits": "5.0.0",
"eslint": "8.27.0",
"@typescript-eslint/eslint-plugin": "5.44.0",
"@typescript-eslint/parser": "5.44.0",
"conventional-changelog-conventionalcommits": "^5.0.0",
"eslint": "8.28.0",
"semantic-release": "19.0.5",
"typescript": "4.8.4",
"vite": "3.2.3",
"typescript": "4.9.3",
"vite": "3.2.4",
"vite-plugin-logseq": "^1.1.2"
},
"homepage": "https://github.com/peanball/logdseq-dynamic-lookup#readme",
Expand Down
Loading

0 comments on commit 2894b68

Please sign in to comment.