Can Dataview return files that are PDF/Epub, or only .md?

Trying to list the books in my books folder, which now lives in my vault as its easier to read ebooks in Obsidian where they can side-by-side with the notes I’m taking. But can Dataview only return .md files in its results? That’s what I’m seeing so far but I wonder if I’m missing something.

Things I have tried

List
FROM "Books"
SORT file.name

What I’m trying to do

List the books in my books folder

Topic

1.How to get a_non_md_files from Obsidian API?
2.How to LIST a_non_md_files as links?
3.How to LIST a_non_md_files(image files) as embedded links?

The prefix “a” of the variable a_non_md_files means an array.


1. Overview

Summary

1.1. Notes

1.1.1. Obsidian_API

  • Obsidian_API = app.vault.getFiles()
  • Take one file for example: “999_Test/Q89_test_data/R03_pdf/demo_20150301.pdf”
  • Obsidian_API automatically adds a large amount of metadata to each file. There are at least twelve Implicit Fields as follows. In other word, we can filter by each Implicit Field.

1.1.2. Obsidian_API Implicit Fields

# Obsidian_API Implicit Fields Data Value Dataview Implicit Fields Remark
1 file.basename demo_20150301 file.name
2 file.extension pdf lower(file.ext) lower case
3 file.name demo_20150301.pdf file.name + “.” + lower(file.ext)
4 file.parent.path 999_Test/Q89_test_data/R03_pdf file.folder
5 file.parent.name R03_pdf
6 file.path 999_Test/Q89_test_data/R03_pdf/demo_20150301.pdf file.path the same
7 file.stat.ctime 1425209450275 file.ctime(type:DateTime) unit: milliseconds
8 file.stat.mtime 1425382250275 file.mtime(type:DateTime) unit: milliseconds
9 file.stat.size 24491 file.size the same;
unit: bytes
10 file.parent.parent?.path 999_Test/Q89_test_data Use conditional property access ?.
when file.parent.path=“/”
11 file.parent.parent?.name Q89_test_data Use conditional property access ?.
when file.parent.path=“/”
12 file.parent.parent?.parent?.path 999_Test Use conditional property access ?.
1.when file.parent.path=“/f”
2.when file.parent.path=“999_Test”

1.1.3. Dataview DateTime functions

  • DateTime.fromSeconds(): Create a DateTime from a number of seconds since the epoch (meaning since 1 January 1970 00:00:00 UTC). Uses the default zone.

2. Input

Summary

2.1. dictionary files

location :“999_Test/Q89_test_data”


  • filename : dic_20150306.md
---
Date: 2015-03-06
---
This is a markdown file.

  • filename : demo_20150301.pdf

  • filename : demo_20150401.JPG

  • filename : demo_20150501.png


3. Test

Summary
  • dataview_v0.5.41
  • Obsidian_v0.13.19

4. DVJS10_TABLE_a_non_md_files_from_Obsidian_API

Summary

4.1. Main DVJS

Code Name Data type Group By Purposes Remark
DVJS10_TABLE_a_non_md_files_from_Obsidian_API pdf, epub, jpg or png no 1.To gather all relevant files by Obsidian API
2.TABLE a_non_md_files for designing DVJS

4.2. Code DVJS10_TABLE_a_non_md_files_from_Obsidian_API

Summary_code
title: DVJS10_TABLE_a_non_md_files_from_Obsidian_API => 1.To gather all relevant files(Type: pdf, epub, jpg or png) by Obsidian API 2.TABLE a_non_md_files for designing DVJS
collapse: open
icon: 
color: 
```dataviewjs
// M11. define i_max_amount_of_non_md_files: 
// #####################################################################
let i_max_amount_of_non_md_files = 10;

// M13. define h_extension_non_md_files_of: Type: pdf, epub, jpg or png
// #####################################################################
let h_extension_non_md_files_of = {
    pdf: "pdf",
    epub: "epub",
    jpg: "jpg",
    png: "png",
};

// M21. define a_non_md_files: gather all relevant files by Obsidian API
// a_non_md_files : JavaScript array
// #####################################################################
let a_non_md_files = app.vault
    .getFiles()
    .filter((file) =>
        // Obsidian API:file.parent.path
        // = Dadaview file.folder = "999_Test/Q89_test_data/R03_pdf"
        dv.func.contains(file.parent.path, "999_Test/Q89_test_data")
    )
    // Obsidian file.basename  = Dadaview file.name = "demo_20150301"
    .filter((file) => dv.func.contains(file.basename, "demo"))
    .filter(
        (file) =>
            // Obsidian API:file.extension  = Dadaview file.ext = "pdf"
            h_extension_non_md_files_of[file.extension]
    )
    .filter(
        (file) =>
            // Obsidian API:DateTime.fromMillis(file.stat.ctime)
            // = Dadaview file.file.ctime
            DateTime.fromMillis(file.stat.ctime) >=
                dv.date("2015-03-01T00:00:00") &&
            DateTime.fromMillis(file.stat.ctime) <=
                dv.date("2022-09-30T23:59:59")
    );



// M23. a_non_md_files : 
// transform a JavaScript array into a DataArray by using dv.array
// #####################################################################
a_non_md_files = dv
    .array(a_non_md_files)
    .sort((file) => file.name, "asc")
    .limit(i_max_amount_of_non_md_files);


// M51. TABLE a_non_md_files : for designing DVJS  
// Use a line break "<br>" to concatenate several strings when the table is too wide.
// #####################################################################
dv.header(3, "M51.TABLE `a_non_md_files`");
dv.table(
    [
        "N",
        
        [
            "Link",
            "Name",
            "Basename",
            "Extension",
            "size(bytes)",
            "parent.parent.path",
            "parent.parent.name",
        ].join("<br>"), 
        
        [
            "parent.path",
            "parent.name",  
            "ctime(milliseconds)",
            "mtime(milliseconds)",
            "size(bytes)",
            "path",
            "parent.parent.parent.path",
        ].join("<br>"),
    ],
    
    a_non_md_files.map((file, index) => [
        index + 1,
        
        [
            dv.fileLink(file.path),
            file.name,
            file.basename,
            file.extension,
            file.stat.size,
            
            //=>source:/demo_20150301.pdf
            dv.func.choice(file.parent.parent?.path, file.parent.parent?.path, "-"),
            dv.func.choice(file.parent.parent?.name, file.parent.parent?.name, "-"), 
            
        ].join("<br>"),

        [
            file.parent.path,
            
            //=>source:/demo_20150301.pdf
            dv.func.choice(file.parent.name.length!==0, file.parent.name, '""'),
            DateTime.fromMillis(file.stat.ctime),
            DateTime.fromMillis(file.stat.mtime),
            file.stat.size,
            file.path,
            
            //=>source:999_Test/demo_20150301.pdf  //=>source:/demo_20150301.pdf
            dv.func.choice(file.parent.parent?.parent?.path, file.parent.parent?.parent?.path, "-"),  
        ].join("<br>"),
    ])
);

```

4.2.1. Screenshots(DQJS10)


5. DVJS20_LIST_a_non_md_files_as_links

Summary

5.1. Main DVJS

Code Name Data type Group By Purposes Remark
DVJS20_LIST_a_non_md_files_as_links pdf, epub, jpg or png no 1.To gather all relevant files by Obsidian API
2.LIST a_non_md_files as links

5.2. Notes

Summary

5.2.1. Step M13: define h_extension_non_md_files_of

5.2.1.1. Original Example: Type: pdf, epub, jpg or png
// M13. define h_extension_non_md_files_of: Type: pdf, epub, jpg or png
// #####################################################################
let h_extension_non_md_files_of = {
    pdf: "pdf",
    epub: "epub",
    jpg: "jpg",
    png: "png",
};
5.2.1.2. Another Example: Type: pdf or epub
// M13. define h_extension_non_md_files_of: Type: pdf or epub
// #####################################################################
let h_extension_non_md_files_of = {
    pdf: "pdf",
    epub: "epub",
};

5.2.2. Slice of Step M21: filter by file.extension

  • One of the most efficient multi-conditional judgments is the following expression.
  • h_extension_non_md_files_of[file.extension] : No modification required
// M21. define a_non_md_files: gather all relevant files by Obsidian API
// #####################################################################
let a_non_md_files = app.vault
    .getFiles()
    .filter(
        (file) =>
            // No modification required
            h_extension_non_md_files_of[file.extension]
    )
};


5.3. Code DVJS20_LIST_a_non_md_files_as_links

Summary_code
title: DVJS20_LIST_a_non_md_files_as_links => 1.To gather all relevant files(Type: pdf, epub, jpg or png) by Obsidian API 2.LIST a_non_md_files as links
collapse: open
icon: 
color: 
```dataviewjs
// M11. define i_max_amount_of_non_md_files: 
// #####################################################################
let i_max_amount_of_non_md_files = 10;

// M13. define h_extension_non_md_files_of: Type: pdf, epub, jpg or png
// #####################################################################
let h_extension_non_md_files_of = {
    pdf: "pdf",
    epub: "epub",
    jpg: "jpg",
    png: "png",
};


// M21. define a_non_md_files: gather all relevant files by Obsidian API
// a_non_md_files : JavaScript array
// #####################################################################
let a_non_md_files = app.vault
    .getFiles()
    .filter((file) =>
        dv.func.contains(file.parent.path, "999_Test/Q89_test_data")
    )
    .filter((file) => dv.func.contains(file.basename, "demo"))
    .filter(
        (file) =>
            // No modification required
            h_extension_non_md_files_of[file.extension]
    )
    .filter(
        (file) =>
            DateTime.fromMillis(file.stat.ctime) >=
                dv.date("2015-03-01T00:00:00") &&
            DateTime.fromMillis(file.stat.ctime) <=
                dv.date("2022-09-30T23:59:59")
    );



// M23. a_non_md_files : 
// transform a JavaScript array into a DataArray by using dv.array
// #####################################################################
a_non_md_files = dv
    .array(a_non_md_files)
    .sort((file) => file.name, "asc")
    .limit(i_max_amount_of_non_md_files);


// M61. LIST a_non_md_files with dv.fileLink() : 
// #####################################################################
dv.header(3, "M61.LIST `a_non_md_files` (links)");
dv.list(a_non_md_files.map((file) => dv.fileLink(file.path)));


```

5.3.1. Screenshots(DQJS20)


6. DVJS30_LIST_a_non_md_files_as_embedded_links

Summary

6.1. Main DVJS

Code Name Data type Group By Purposes Remark
DVJS30_LIST_a_non_md_files_as_embedded_links png, jpg … nine formats no 1.To gather all relevant files by Obsidian API
2.LIST a_non_md_files as embedded links

6.2. Code DVJS30_LIST_a_non_md_files_as_embedded_links

Summary_code
title: DVJS30_LIST_a_non_md_files_as_embedded_links => 1.To gather all relevant files(Type: png, jpg ... nine formats) by Obsidian API 2.LIST a_non_md_files as embedded links
collapse: open
icon: 
color: 
```dataviewjs
// M11. define i_max_amount_of_non_md_files: 
// #####################################################################
let i_max_amount_of_non_md_files = 10;


// M13. define h_extension_non_md_files_of: Type: png, png and so on
// #####################################################################
let h_extension_non_md_files_of = {
    png:  "png",
    jpg:  "jpg",
    jpe:  "jpe",
    jpeg: "jpeg",
    bmp:  "bmp",
    gif:  "gif",
    wmf:  "wmf",
    tif:  "tif",
    tiff: "tiff",
};


// M21. define a_non_md_files: gather all relevant files by Obsidian API
// a_non_md_files : JavaScript array
// #####################################################################
let a_non_md_files = app.vault
    .getFiles()
    .filter((file) =>
        dv.func.contains(file.parent.path, "999_Test/Q89_test_data")
    )
    .filter((file) => dv.func.contains(file.basename, "demo"))
    .filter(
        (file) =>
            // No modification required
            h_extension_non_md_files_of[file.extension]
    )
    .filter(
        (file) =>
            DateTime.fromMillis(file.stat.ctime) >=
                dv.date("2015-03-01T00:00:00") &&
            DateTime.fromMillis(file.stat.ctime) <=
                dv.date("2022-09-30T23:59:59")
    );



// M23. a_non_md_files : 
// transform a JavaScript array into a DataArray by using dv.array
// #####################################################################
a_non_md_files = dv
    .array(a_non_md_files)
    .sort((file) => file.name, "asc")
    .limit(i_max_amount_of_non_md_files);



// M71. LIST a_non_md_files with dv.func.embed() for image files :
// #####################################################################
dv.header(3, "71.LIST `a_non_md_files` (embed) for image files");
dv.list(
    a_non_md_files
        .map((file) => dv.func.embed(dv.fileLink(file.path)))
);


```

6.2.1. Screenshots(DQJS30)


3 Likes

Thank you very much. While at first I was totally lost as to what any of this meant as I’m not a developer, once I pasted the Summary code I was quickly able to modify it and get it working. Thank you!

3 Likes

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