Skip to content

Truncate note name and point to the original note link #426

Answered by wenlzhang
wenlzhang asked this question in Q&A
Truncate note name and point to the original note link #426
Aug 19, 2021 · 2 answers · 8 replies

Basically, I would like to display note names in the format of [[Note A - 202108131821]] in a Dataview table as [[Note A]].

With the following code, the items would display as note names, instead of links.

table regexreplace(rows.file.name, " - \d+", "") as Film
from #Film
where rating != null
group by rating as Rating
sort rating desc

I tried to the following code to make it as note links, but I got some errors.

table link(rows.file.link, regexreplace(rows.file.name, " - \d+", "")) as Film
from #Film
where rating != null
group by rating as Rating
sort rating desc

The error:

Dataview: Every row during final data extraction failed with an error; first 3:

            - No implementation of 'link' found for arguments: link, array

I also made a similar thing with DataviwJS by modifying the example from the Dataview documentation, but not sure how to convert the note names as links either.

for (let group of dv.pages("#NoteDataview").groupBy(p => p["film-genre"])) {
    dv.header(3, group.key);
    dv.table(["Name", "Rating", "Watched"],
        group.rows
			.where(k => "film-rating" in k)
            .sort(k => k["film-rating"], 'desc')
            .map(k => [k.file.name.replace(/ - \d+/, ''), k["film-rating"], k["date-watched"]]))
}

Any help would be appreciated.

Thanks for the suggestion. To figure out what has happened, I did the following tests, but did not find out why.

  1. Test 1: Print out all related pages with the field film-genre included and no empty film-genre exists.
  2. Test 2: Include explicitly all desired film-genre in the where part, to filter out (potential) empty film-genre.

The code for the second case looks like the following:

for (let group of dv.pages("#NoteDataview").groupBy(p => p["film-genre"])) {
    dv.header(3, group.key);
    dv.table(["Name", "Rating", "Watched"],
        group.rows
	    .where(k => "film-rating" in k && (k["film-genre"] == "Genre1" || k["film-genre"] == "Genre2"))
            .sort(k => k["film-rating"], …

Replies

2 suggested answers
·
8 replies

For dataview, the error is because rows.file.name is an array, and link was not vectorized in the second argument. I've fixed this for the next release, but you can also do

map(rows, (row) => link(row.file.path, regexreplace(row.file.name, " - \d+", "")))

For dataviewjs, you can use dv.func.link() just like in dataview.

3 replies
@wenlzhang

Thanks for the help!

I managed to make it work for DataviewJS with the following code:

for (let group of dv.pages("#NoteDataview").groupBy(p => p["film-genre"])) {
    dv.header(3, group.key);
    dv.table(["Name", "Rating", "Watched"],
        group.rows
			.where(k => "film-rating" in k)
            .sort(k => k["film-rating"], 'desc')
            .map(k => [dv.func.link(k.file.path, k.file.name.replace(/ - \d+/, '')), k["film-rating"], k["date-watched"]]))
}

However, I got kind of an empty table in Dataview. The code, which is similar to the following:

table map(rows, (row) => link(row.file.path, regexreplace(row.file.name, " - \d+", ""))) as Film
from #Film
where rating != null
group by rating as Rating
sort rating desc

The result with Dataview:
Screenshot 2021-08-20 at 09 54 42

Did I miss anything in the Dataview code?

@wenlzhang

I realized a small issue for the generated table with DataviewJS. Before categorizing the films by Genre, there is an empty table (head) without anything inside. I am not sure why this would be generated.
Screenshot 2021-08-20 at 10 34 16

@blacksmithgu

Perhaps there is an empty film-genre? You can try printing out what your group prints out to see if there is some mysterious empty group being created that you can filter out.

Thanks for the suggestion. To figure out what has happened, I did the following tests, but did not find out why.

  1. Test 1: Print out all related pages with the field film-genre included and no empty film-genre exists.
  2. Test 2: Include explicitly all desired film-genre in the where part, to filter out (potential) empty film-genre.

The code for the second case looks like the following:

for (let group of dv.pages("#NoteDataview").groupBy(p => p["film-genre"])) {
    dv.header(3, group.key);
    dv.table(["Name", "Rating", "Watched"],
        group.rows
	    .where(k => "film-rating" in k && (k["film-genre"] == "Genre1" || k["film-genre"] == "Genre2"))
            .sort(k => k["film-rating"], 'desc')
            .map(k => [dv.func.link(k.file.path, k.file.name.replace(/ - \d+/, '')), k["film-rating"], k["date-watched"]]))
}

Still, this Test 2 gives the empty table.

5 replies
@wenlzhang

Anther finding is that there would be some extra empty tables even if only one film-genre is included in the where part:

for (let group of dv.pages("#NoteDataview").groupBy(p => p["film-genre"])) {
    dv.header(3, group.key);
    dv.table(["Name", "Rating", "Watched"],
        group.rows
	    .where(k => "film-rating" in k && (k["film-genre"] == "Story"))
            .sort(k => k["film-rating"], 'desc')
            .map(k => [dv.func.link(k.file.path, k.file.name.replace(/ - \d+/, '')), k["film-rating"], k["date-watched"]]))
}

For example, the query above would give an empty table as follows:
Screenshot 2021-08-22 at 10 40 41

From this finding, I feel that it might be related to the fact the where part is within the dv.table? If this is the case, is it possible to include a where before for or in this part (let group of dv.pages("#NoteDataview").groupBy(p => p["film-genre"]))?

@blacksmithgu

You can filter before grouping:

for (let group of dv.pages("#NoteDataview").where(...).groupBy(...)) {
}
@wenlzhang

Great! It works like a charm now! May I ask another question? Is it possible to sort based on group names, i.e. film-genre?

Answer selected by M-bot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants