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.
---
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.
I hope it helps somehow.