Inline properties and reuse of property values, basic implementation

  • Inspiration
  • Usage
    • This uses a macro with the following syntax: {{p property, value}}
      • Should not use comma (,) anywhere else.
        • Though generally the comma can be omitted: {{p property value}}
      • Omitting the value reuses the existing one: {{p property}}
    • Typing something like this (check customization below):
      {{p title "The Gray Picture"}} is a {{p type phenomenon}} discovered by {{p scientist [[Person1]]}}, {{p scientist [[Person2]]}} and {{p scientist [[Person3]]}}. {{p title}} is very rare.
      

      Should produce this:

      Notice the last macro {{p title}} , which reuses the earlier value.
  • Preparation
    • Add a macro inside file config.edn , inside macros{} :
      :p "<span class='kit' data-kit='inlineprop' data-prop='$1' data-val='$2'>$1:: $2</span>"
      
    • The code below requires having kits inside file custom.js .
    • Inside page InlineProp in Logseq, put the following code in a javascript code-block:
      logseq.kits.setStatic(function inlineprop(span){
        console.log("IN")
          const dataset = span.dataset
          var propname = dataset.prop
          if (propname === "$1") propname = ""
          var val = dataset.val
          var noVal = (val === "$2")
      
          const blockId = span.closest(".ls-block").getAttribute("blockid")
          const block = logseq.api.get_block(blockId)
          const props = block.propertiesTextValues
          var old = (props && props[propname])
      
          if (noVal && old === undefined) {
              const index = propname.indexOf(" ")
              if (index > -1) {
                  val = propname.slice(index + 1)
                  noVal = (val === "")
                  propname = propname.slice(0, index)
                  old = (props && props[propname])
              }
          }
      
          const linkTarget = dataset.linkTarget
          const isLink = val.startsWith("[[") && val.endsWith("]]")
          const needsLink = !noVal && (isLink || dataset.link === "force")
          const plain = (isLink) ? val.slice(2, -2) : (noVal) ? old : val
      
          const fixval = (noVal) ? "" : val.replaceAll("[", "\\[").replaceAll("]", "\\]")
          const target = new RegExp("\\{\\{p\\s*" + propname + "\\s*,?\\s*" +
              fixval + "\\s*\\}\\}", "gimu")
          const text = (needsLink && linkTarget !== "prop") ? "[[" + plain + "]]" : plain
          logseq.api.update_block(blockId, block.content.replace(target, text))
          if (noVal || !propname || !plain) return
      
          const proptext = (needsLink && linkTarget !== "text") ? "[[" + plain + "]]" : plain
          if (old && old.split(", ").includes(proptext)) return
      
          logseq.api.upsert_block_property(blockId, propname, (old) ?
              old + ", " + proptext : proptext)
      })
      
  • Customization of values in macro’s definition
    • Default behavior:
      • typing value produces:
        • block text: value
        • property value: value
      • typing [[value]] produces:
        • block text: [[value]]
        • property value: [[value]]
    • With attribute data-link-target='prop':
      • typing [[value]] produces:
        • block text: value
        • property value: [[value]]
    • With attribute data-link-target='text':
      • typing [[value]] produces:
        • block text: [[value]]
        • property value: value
    • With both attributes data-link='force' and data-link-target='prop':
      • typing either value or [[value]] produces:
        • block text: value
        • property value: [[value]]
    • With both attributes data-link='force' and data-link-target='text':
      • typing either value or [[value]] produces:
        • block text: [[value]]
        • property value: value
    • With attribute data-link='force' alone (meaning both):
      • typing either value or [[value]] produces:
        • block text: [[value]]
        • property value: [[value]]

@mentaloid, hello!

I’ve just created similar showcase with 🏛 Full House Templates plugin:

2023-12-01 01.13.08

Setup instructions are here.

2 Likes

This is awesome, after so much discussion about this need that Logseq implements this at the core level and support this natively, I am so enthusiastic about work that is being don at least in the direction of having this functionality via custom.js because it can target very specific needs and implement them with minimal code. I am tight on time these days but will definitely try it and give feedback.