Skip to content

Instantly share code, notes, and snippets.

@serpro69
Created December 31, 2022 10:24
Show Gist options
  • Save serpro69/c3adbd3b1bff5f2c03c59a6453ab883c to your computer and use it in GitHub Desktop.
Save serpro69/c3adbd3b1bff5f2c03c59a6453ab883c to your computer and use it in GitHub Desktop.
Obsidian Dataview - display text from header section
let pages = dv.pages('#daylog');
// Loop through pages
for (let p of pages) {
let noteText = await dv.io.load(p.file.path);
// define headers to look for
const headers = ["🧭 Personal", "💻 Work"];
headers.map((header) => {
// https://regex101.com/r/liL65A/1
let regexPattern = new RegExp("#{1,6}\\s(" + header + ")\\n(.+?)(?:\\n#{1,6}|$)", "sg");
let matches = regexPattern.exec(noteText);
// check if we got a match and only then insert
if (matches != null) {
let fileName = p.file.name;
let headerName = matches[1];
let headerText = matches[2];
// remove trailing line delimiter, if any, and trim whitespaces
headerText = headerText
.replace(new RegExp("\n---\n$"), "")
.trim();
// I have '- ...' placeholders in each header, so first check if the header section actually has any text
if (headerText != "- ...") {
// Insert into document however you like
dv.paragraph(
`> [!note] [[${p.file.name}#${headerName}]]` +
"\n>" +
"\n" +
headerText.split("\n").map((text) => `> ${text}`).join("\n>")
);
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment