but when I try and use it in some Javascript, an error is generated:
Evaluation Error: SyntaxError: Invalid or unexpected token
Why do I want it? Because I have a header section (Key Points, where I write a summary of the content) in some pages that I want to pull out into another page, kind of an overall summary page. I currently do this with the following code (based upon something a kind soul left somewhere else in this forum):
const regex = new RegExp(\n${header}\r?\n(.*?)(\n#+ |\n---|$), ‘s’)
for (const page of pages) {
const file = app.vault.getAbstractFileByPath(page.file.path)
const contents = await app.vault.read(file)
const summary = contents.match(regex)
if (summary) {
dv.header(2, file.basename)
dv.paragraph(summary[1].trim())
}
}
Works perfectly! The problem is that I want to be more selective and not just process all the pages in one folder, I want to build a list of files and then process them, extracting the Key Points section.
Building on the code you presented here, I would rather switch it around a little, and let the start of your script look like:
[“/Summaries/C/c.md”];
const header = '## Key Points'
const regex = new RegExp(\n${header}\r?\n(.*?)(\n#+ |\n---|$), 's')
const pageNames = [
"/Misc/A/a.md",
"/Projects/B/b.md",
"/Summaries/C/c.md"
];
for (const pageName of pageNames) {
const page = dv.page(pageName)
const file = app.vault.getAbstractFileByPath(page.file.path)
... same as before ...
}
I’ve not tested this, but it should work. And it could possibly work with just doing this at the start of the for loop:
for (const pageName of pageNames) {
const file = app.vault.getAbstractFileByPath(pageName)
...
}
And then you wouldn’t actually need to do the dv.page() read first at all.
Bonus tip: How to present code properly in a forum post
If you want to showcase either markdown, or code blocks, or dataview queries properly in a forum post, be sure to add one line before and one life after what you want to present with four backticks, ````. This will ensure that any other backticks (like for code blocks or queries) is properly shown.
Evaluation Error: SyntaxError: Invalid or unexpected token
at DataviewInlineApi.eval (plugin:dataview:18370:21)
at evalInContext (plugin:dataview:18371:7)
at asyncEvalInContext (plugin:dataview:18378:16)
at DataviewJSRenderer.render (plugin:dataview:18402:19)
at DataviewJSRenderer.onload (plugin:dataview:17986:14)
at e.load (app://obsidian.md/app.js:1:631178)
at DataviewApi.executeJs (plugin:dataview:18921:18)
at DataviewPlugin.dataviewjs (plugin:dataview:19423:18)
at eval (plugin:dataview:19344:124)
at app://obsidian.md/app.js:1:1186678
What I am doing wrong?
BTW, while were on the subject, I don’t understand the relation between the line numbers in the error message and my lines of code. Which line tells me where the error is being generated?
I’ve not found a reliable relationship between the line numbers in the error messages, and the actual line of the errors. So sometimes I just know which area I was working within, and see the error easily, and sometimes I need to add other code (or even introduce a known error to locate it.
In this case, I think it was introduced when I copied your code from plain text, so I lost the backticks in the regex line. Try with the following:
const regex = new RegExp(`\n${header}\r?\n(.*?)(\n#+ |\n---|$)`, 's')
I’m not able to test it out today, as I’m rather busy, but if this fix fails, let us know, and we’ll figure it out.