Page Preview for quicknotes

Hi all, I am trying to get a template together to show all my quick notes with a small preview say 50 characters? I had a look around but couldn’t quite get it together myself, could I borrow another pair of eyes? :slight_smile:

The template
Library/Core/Query/Preview

[[{{name}}]] {{readPage(name)}}

The query on the home page

page where name =~ /^Inbox\// render [[Library/Core/Query/Preview]] order by name desc

I tried myself for fun and because I like the idea (the solution is far from perfect). I also beg others to share their ways to do it. :slight_smile:

---
description: A page preview
tags:
  - template
---
# [[{{name}}]]
{{truncate(trim(stripFrontMatter(readPage(name))), 150)}}

-----

The truncate() is my custom function (again, far from perfect, but you can choose from 3 styles of truncating the string with it).

---
description: Returns text truncated to given length
tags:
  - meta
---

```space-script
silverbullet.registerFunction({name: 'truncate'}, (str, maxLength, elipsis, location) => {
  if(str.length <= maxLength) {
    return str;
  }

  let startStr;
  let endStr;
  let elipsisStr = elipsis || '…';
  let elipsisLen = elipsisStr.length;
  let partLength = Math.ceil((maxLength - elipsisLen)/2);

  switch(location) {
    case 'start':
      endStr = str.substring(str.length - maxLength + elipsisLen);
      return elipsisStr + endStr;

    case 'end':
      startStr = str.substring(0, maxLength - elipsisLen);
      return startStr + elipsisStr;

    case 'middle':
    default:
      startStr = str.substring(0, partLength);
      endStr = str.substring(str.length - partLength);
      return startStr + elipsisStr + endStr;
  }
})
```

The trim() custom function removes leading & trailing whitespace characters (including EOLs).

---
description: Returns trimmed text
tags:
  - meta
---

```space-script
silverbullet.registerFunction({name: 'trim'}, (text) => {
    return text.toString().replace(/^\s+|\s+$/, '');
})
```

The stripFrontMatter() is just another custom function that does what it’s name tells.

---
description: Returns page content without front-matter
tags:
  - meta
---

```space-script
silverbullet.registerFunction({name: 'stripFrontMatter'}, (content) => {
  return content.toString().replace(/^---\n(([^\n]|\n)*?)---\n/, '');
})
```

And I chose 150 as better length than your 50. I can imagine other ways to truncate the notes, for example a custom function that does some sort of “smarter” truncating (e.g., on sentence boundaries etc., but perfect function should parse Markdown and make decision based on AST, because the truncation can break something easily, e.g. lists, tables etc., so it should be implemented in SB’s core, I guess).

Finally, the query for my /Fleeting Notes/... pages (tested and it seems to work).

```query
page
  where
    name =~ /^Fleeting Notes\// and
    name != @page.name
  render
    [[Library/Personal/Templates/pagePreview]]
  order by
    lastUpdated
  desc
```

The name != @page.name is there because I tested this under the /Fleeting Notes/... hierarchy and ran into some parsing error without it. :wink:

I hope it helps somehow.

4 Likes

This is exactly what I was after! Thank you so much! I haven’t messed about with space-script too much but thanks for explaining it so clearly.

I can see that it is very powerful and this gives me a good idea on how I can start adjusting more things! :slight_smile: