Hello! I’m trying to create an index for my book notes. The index would comprise the title and the page numbers of wherever I’ve written the notes, e.g.
So far searching online, I’ve managed to make the relevant titles appear on one line. However, I’d like to incorporate the page numbers in brackets next to the titles as well. I haven’t figured out a way to do this. Here’s the current code, what it renders as, and what the properties look like.
let labels = pages
.map(note => ${note.file.name} (${note.file.frontmatter.pages});
dv.el(“p”, labels.join(', ‘));
‘’’
This script uses JavaScript template literals to separately create the display labels for each note,then concatenates them as before for the output paragraph.
This worked, thank you so much! The only thing is that it renders the file name as text rather than a link, so I borrowed from Holroy’s solution and used “dv.fileLink(note.file.path, false)” to keep the link. Thank you!
Thank you so much! This didn’t work at first (there was a syntax error of a missing ), and then when I added it where I think it should be, nothing rendered at all) but after troubleshooting a bit with chatgpt it worked:
let bookLinks = dv.pages()
.where(l => l.location == "M1")
.sort(b => b.pages)
.map(p => {
// Ensure p.file.link is a valid string and handle it safely
const fileLink = typeof p.file.link === "string" ? p.file.link : p.file.path;
// Return the formatted string with file link and pages (as string)
return `${dv.fileLink(fileLink, false)} (${p.pages || "No pages"})`;
})
.join(", ");
dv.paragraph(bookLinks);
I think there was an issue with some dependency on .replaceAll(), although I’m not sure where from. But thanks very much!