Desired Dataview output: the 2 lines above the inline keys

The desired output, when searching for “value 1” in Dataview, is getting the 2 lines above the inline key

Sample Input

Picture 1

Picture 2

Desired Output

Picture 1

Picture 2

Thanks for any help!

Dear all, I have solved the question; with ChatGPT. Simply entered the question; the result came promptly and worked immediately. See below. I am impressed.
Probably many problems that are posted here as questions can be easily solved with ChatGPT. Good luck!

My question to ChatGPT
"I am looking for a code. The language is Dataview Query Language (DQL) used by the Obsidian plugin Dataview. I have the following task for my vault: The desired output, when searching for “value 1” in Dataview, is getting the 2 lines above the inline key.

Sample Input

Picture 1

Picture 2

Desired Output

Picture 1

Picture 2

ChatGPT:
"To achieve this with Dataview in Obsidian, you can use a combination of Dataview’s query capabilities and some scripting within your notes to structure your data appropriately. However, Dataview doesn’t natively support fetching lines directly above a specific key-value pair. But we can leverage some creativity with inline JavaScript to accomplish this.

Here’s a way to structure your notes and a Dataview query to fetch the desired output:

Structure Your Notes:
Ensure your notes follow a consistent structure where the key-value pairs are located predictably.

markdown

Picture 1

Picture 2

Dataview Query with Inline JavaScript:

Place the following Dataview inline script in your Obsidian note to fetch and display the desired lines:

markdown

// Define a function to get the previous two lines before a specific key-value pair
function getPreviousTwoLines(fileContent, searchKey, searchValue) {
    const lines = fileContent.split('\n');
    let result = [];
    
    for (let i = 2; i < lines.length; i++) {
        if (lines[i].includes(`${searchKey}:: ${searchValue}`)) {
            result.push(lines[i-2]);
            result.push(lines[i-1]);
        }
    }
    return result;
}

// Search parameters
const searchKey = "key1";
const searchValue = "value 1";

// Iterate through all notes
for (let file of dv.pages()) {
    let fileContent = dv.io.load(file.file.path);

    fileContent.then(content => {
        let result = getPreviousTwoLines(content, searchKey, searchValue);
        if (result.length > 0) {
            dv.header(2, file.file.name);
            result.forEach(line => dv.paragraph(line));
        }
    });
}

Explanation:
File Structure: Notes are structured with headings for each “Picture” section, and the lines you want to fetch are just before the key-value pairs.
JavaScript Function: The inline JavaScript function getPreviousTwoLines processes the note content, searches for the specified key-value pair, and retrieves the two lines above it.
DataviewJS: The script loads each note, processes its content with the function, and displays the matching lines.

This approach assumes that your notes are relatively simple and well-structured. The script processes each note’s content line by line, looking for the key-value pair and extracting the desired lines above it. Adjustments may be necessary based on your actual note structure and content complexity."

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.