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

Commit

Permalink
feat: Add support for a fallback template
Browse files Browse the repository at this point in the history
  • Loading branch information
peanball committed Nov 4, 2022
1 parent 3e9554f commit 204dec7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ env:
# Controls when the action will run.
on:
push:
tags:
- "*"
branches:
- "main"
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,23 @@ Great for macros that automatically augment links to specific pages or tags.
## Demo

```clojure
{{renderer :lookup, page, property, template}}
{{renderer :lookup, page, property, 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
- `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 is replaced with nothing. This makes it safe to use in macros, even if e.g. the target page
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.
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

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 {
:macros {
"jira" "#$1 {{renderer :lookup, $1, summary, ($value)}}"
}
```
Expand All @@ -34,7 +36,6 @@ 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.


## Building

> 🏷 Currently this only builds on Node <= 16.
Expand All @@ -43,4 +44,5 @@ You could also use it to link to the actual issue. In a macro `$1`, `$2`, etc. c
- `Load unpacked plugin` in Logseq Desktop client.

### License

MIT
21 changes: 13 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ async function main() {
*/
logseq.App.onMacroRendererSlotted(({ slot, payload }) => {
// console.debug(`rendering slot ${slot} with payload:`, payload)
const [type, target, property] = payload.arguments
const [type, target, property, formatTemplate, fallbackTemplate] = payload.arguments
if (!type || type != ':lookup') {
return
}

let template = `<span alt="${target}:${property}" class="dynamic-lookup">$value</span>`
let format = `<span alt="${target}:${property}" class="dynamic-lookup">$value</span>`
if (payload.arguments.length > 3) {
template = payload.arguments[3]
console.trace("setting template to: ", template, payload.arguments)
format = formatTemplate
console.trace("setting template to: ", format)
}

const query = `[
Expand All @@ -73,16 +73,21 @@ async function main() {
const properties = result[0][0]
console.trace(properties)
const value = properties[property]
if (value) {
template = template.replace("$value", value)

logseq.provideUI({
key: slot,
slot, reset: true,
template: format.replace("$value", value)
})
}).catch(_ => {
if (fallbackTemplate) {
logseq.provideUI({
key: slot,
slot, reset: true,
template
template: fallbackTemplate
})
}
}).catch(e => console.log(e))
})
})
}

Expand Down

0 comments on commit 204dec7

Please sign in to comment.