File.link without Project-id, trim display-name in dataview +js

What I’m trying to do

I have project files which unique project number in the title.
The format is of the project number is \d{2}\.\d{2}\.[a-zA-Z]+\d{2})/,
"42.00.T02 Test Project " for example.
When using dataview, I would like to display the file.link without the project nr – trim the display-name, but still link to the file.

Things I have tried

I tried everything to the best of my knowledge, but am stuck. I have 3 queries, one dataview and 2 dataviewjs, everyone with a different problem.

The 3rd option looks exactly like I want it, but unfortunately the link is pointing to the trimed filename rather just displaying the file.link with a different display-name like the function dv.fileLink(path, [embed?], [display-name]) does.

If somebody could point me in the right direction, I’d be truly grateful!

Normal dataview SQL

table without id
    file.link as "File", 
    regexreplace(file.name, "^(\d{2}\.\d{2}\.[a-zA-Z]+\d{2}).*", "$1") as "ID"
from #expc/test

Dataviewjs

Trimed but no link

// Initialize an array to hold the table rows
let tableRows = [];

// Iterate over all pages that have the #expc/test tag
dv.pages('#expc/test').forEach(page => {
    // Extract the ID from the filename (assuming format "42.04.E01" or similar)
    let idMatch = page.file.name.match(/^(\d{2}\.\d{2}\.[a-zA-Z]+\d{2})/);
    let id = idMatch ? idMatch[1] : 'No ID';
    
    // Remove the ID from the filename
    let titleWithoutID = page.file.name.replace(/^(\d{2}\.\d{2}\.[a-zA-Z]+\d{2})\s*/, '').trim();
    
    // Add the row to the table
    tableRows.push([dv.fileLink( titleWithoutID, true), id]);

  
});

// Render the table with a single header row
dv.table(
    ["Filename", "ID"],
    tableRows
);

Trimed, but file.link points to wrong, non existing file
// Initialize an array to hold the table rows
let tableRows = [];

// Iterate over all pages that have the #expc/test tag
dv.pages('#expc/test').forEach(page => {
    // Extract the ID from the filename (assuming format "42.04.E01" or similar)
    let idMatch = page.file.name.match(/^(\d{2}\.\d{2}\.[a-zA-Z]+\d{2})/);
    let id = idMatch ? idMatch[1] : 'No ID';
    
    // Remove the ID from the filename
    let titleWithoutID = page.file.name.replace(/^(\d{2}\.\d{2}\.[a-zA-Z]+\d{2})\s*/, '').trim();
    
    // Add the row to the table
    tableRows.push([dv.fileLink(titleWithoutID), id]);
});

// Render the table with a single header row
dv.table(
    ["Filename", "ID"],
    tableRows
);

In a normal query you could a new link using link, which should allow stuff like: link(file.link, "Your new display name"). In this context you could of course change that fixed text with the result of a regexreplace() or whatever means you choose to produce the display string to use for that link.

When using dv.fileLink you’ll need to provide the full path as the first parameter so in your case it would be something like dv.fileLink(page.file.path, false, titleWithoutId) where titleWithoutId is a string to use as display text.

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