Can I add comments in the dataview query?

What I’m trying to do

I want to add comments in the dataview query. Many times I want to quickly describe the logic of the query and the purpose of it or anything noteworthy that I am going to create this query. In many programming languages, the comments is one of the powerful feature to do that. I don’t know if I can do the same in the dataview query. The comments are just ignored when the code is running. There must be a syntax for one line, inline and multiline comments for the dataview querying language ( that, unfortunately, I don’t know yet). Please help.

Things I have tried

I tried the css comment style which include
“//” for the one line and inline comment and
“/*your comment goes here*/” for the multiline comment.

Somewhere I read that the Obsidian Tasks plugin query allows the comments that start with a “#”. I tried it here in the dataview query.

Didn’t help.

Couldn’t get any result for the “Add comments in the dataview query”

2 Likes

Unfortunately it’s not possible and there is an active feature request on the DataView plugin repo to implement commenting:

The hardcore alternative is to avoid DQL and instead use DataView’s API, which requires more code and knowledge of JavaScript to produce the same results as DQL, but I doubt it’s worth the switch just for the sake of commenting code.

HOWEVER, you could put a markdown comment right above the DQL codeblock as follows (and it will only be visible in source/edit mode, not in live preview):

%% below DQL does this and that %%
… DQL codeblock here … or paragraph with inline DQL …

1 Like

Oh… Thanks for the quick reply.

You have suggested the markdown comment. Can I hide the comment in the edit mode and the comment appears in the source mode only?

1 Like

Sure thing it’s possible using a custom CSS snippet, although with some caveats:

  • you can hide the comment but keep its line in the editor, which may lead to accidental deletion of the comment (choose ONE of the below 3 codes depending on which edit mode you want to hide it from):

    /* hide comments in Source Edit mode ONLY */
    .markdown-source-view:not(.is-live-preview) .cm-comment:not([class*="codeblock"]) {
      visibility: hidden;
    }
    
    /* hide comments in Live Edit mode ONLY */
    .markdown-source-view.is-live-preview .cm-comment:not([class*="codeblock"]) {
      visibility: hidden;
    }
    
    /* hide comments in BOTH Source & Live Edit modes */
    .markdown-source-view .cm-comment:not([class*="codeblock"]) {
      visibility: hidden;
    }
    
  • or you can collapse the comment line altogether and remove it from view simulating a hidden comment, but if the sidebar shows line numbers, you’ll see line numbers being skipped or overlapping hence indicating a hidden comment (choose ONE of the below 3 codes depending on which edit mode you want to hide it from):

    /* hide comments in Source Edit mode ONLY */
    .markdown-source-view:not(.is-live-preview) .cm-comment:not([class*="codeblock"]) {
      display: none;
    }
    
    /* hide comments in Live Edit mode ONLY */
    .markdown-source-view.is-live-preview .cm-comment:not([class*="codeblock"]) {
      display: none;
    }
    
    /* hide comments in BOTH Source & Live Edit modes */
    .markdown-source-view .cm-comment:not([class*="codeblock"]) {
      display: none;
    }
    

And if you want to be explicit about there being a comment but not seeing the actual comment, you can display a placeholder comment that hides the real comment, something like this:

.markdown-source-view .cm-comment:not([class*="codeblock"]):not(.cm-comment-start):not(.cm-comment-end):before {
  content: "hidden comment";
  color: var(--code-comment);;
  visibility: visible;
}

Thanks a lot, woofy31
I’ll try that right away

You could kind of get this functionality by doing dataviewjs instead of pure DQL queries in dataview. Using boilerplate code like the one below, you could write DQL queries within dataviewjs and add whatever comments you’d like in the javascript part of it…

```dataviewjs
// This is a comment describing the query below
const result = await dv.query(`
  TABLE ... rest of query ...
`)

if (result.successful) {
  dv.table(result.value.headers, result.valued.values)
} else
  dv.paragraph("~~~~\n" + result.error + "\n~~~~")
```

Similar boilerplate can be written for TASK and LIST queries. You wouldn’t need to worry about the boilerplate code and just replace the query with what you want. (There might be a need for a little more code if you tend to use queries which references this.*, but there is a workaround for that as well)

See also dataview’s documentation on dv.query().

1 Like

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