Skip to content

Commit

Permalink
Add support for dataview js and inline queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Ole Eskild Steensen committed Mar 21, 2023
1 parent e9bf4ed commit 92c5f91
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 7 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "digitalgarden",
"name": "Digital Garden",
"version": "2.41.1",
"version": "2.42.0",
"minAppVersion": "0.12.0",
"description": "Publish your notes to the web for others to enjoy. For free.",
"author": "Ole Eskild Steensen",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
"js-base64": "^3.7.2",
"luxon": "^3.2.1",
"lz-string": "^1.4.4",
"obsidian-dataview": "^0.5.41"
"obsidian-dataview": "^0.5.55"
}
}
77 changes: 73 additions & 4 deletions src/Publisher.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { DateTime } from 'luxon';
import { MetadataCache, TFile, Vault, Notice, getLinkpath } from "obsidian";
import { MetadataCache, TFile, Vault, Notice, getLinkpath, Component } from "obsidian";
import DigitalGardenSettings from "src/DigitalGardenSettings";
import { Base64 } from "js-base64";
import { Octokit } from "@octokit/core";
import { arrayBufferToBase64, generateUrlPath, kebabize } from "./utils";
import { arrayBufferToBase64, escapeRegExp, generateUrlPath, kebabize } from "./utils";
import { vallidatePublishFrontmatter } from "./Validator";
import { excaliDrawBundle, excalidraw } from "./constants";
import { getAPI } from "obsidian-dataview";
Expand Down Expand Up @@ -280,11 +280,25 @@ export default class Publisher {

async convertDataViews(text: string, path:string): Promise<string> {
let replacedText = text;
const dataViewRegex = /```dataview(.+?)```/gsm;
const dataViewRegex = /```dataview\s(.+?)```/gsm;
const dvApi = getAPI();
const matches = text.matchAll(dataViewRegex);
if(!matches)return;

const dataviewJsPrefix = dvApi.settings.dataviewJsKeyword;
const dataViewJsRegex = new RegExp("```" + escapeRegExp(dataviewJsPrefix) + "\\s(.+?)```", "gsm");
const dataviewJsMatches = text.matchAll(dataViewJsRegex);

const inlineQueryPrefix = dvApi.settings.inlineQueryPrefix;
const inlineDataViewRegex: RegExp = new RegExp("`" + escapeRegExp(inlineQueryPrefix) + "(.+?)`", "gsm");
const inlineMatches = text.matchAll(inlineDataViewRegex);

const inlineJsQueryPrefix = dvApi.settings.inlineJsQueryPrefix;
const inlineJsDataViewRegex: RegExp = new RegExp("`" + escapeRegExp(inlineJsQueryPrefix) + "(.+?)`", "gsm");
const inlineJsMatches = text.matchAll(inlineJsDataViewRegex);

if(!matches && !inlineMatches && !dataviewJsMatches && !inlineJsMatches)return;

//Code block queries
for(const queryBlock of matches){
try{
const block = queryBlock[0];
Expand All @@ -297,6 +311,61 @@ export default class Publisher {
return queryBlock[0];
}
}

for(const queryBlock of dataviewJsMatches){
try{
const block = queryBlock[0];
const query = queryBlock[1];

const div = createEl('div');
const component = new Component();
await dvApi.executeJs(query, div, component, path)
component.load();

replacedText = replacedText.replace(block, div.innerHTML);
}catch(e){
console.log(e)
new Notice("Unable to render dataviewjs query. Please update the dataview plugin to the latest version.")
return queryBlock[0];
}
}

//Inline queries
for(const inlineQuery of inlineMatches){
try{
const code = inlineQuery[0];
const query = inlineQuery[1];
const dataviewResult = dvApi.tryEvaluate(query,{this: dvApi.page(path)});
if(dataviewResult) {
replacedText = replacedText.replace(code, dataviewResult.toString());
}
}catch(e){
console.log(e)
new Notice("Unable to render inline dataview query. Please update the dataview plugin to the latest version.")
return inlineQuery[0];
}
}

for(const inlineJsQuery of inlineJsMatches){
try{
const code = inlineJsQuery[0];
const query = inlineJsQuery[1];

const div = createEl('div');
const component = new Component();
await dvApi.executeJs(query, div, component, path)
component.load();

replacedText = replacedText.replace(code, div.innerHTML)

}catch(e){
console.log(e)
new Notice("Unable to render inline dataviewjs query. Please update the dataview plugin to the latest version.")
return inlineJsQuery[0];
}
}


return replacedText;

}
Expand Down
6 changes: 5 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@ function kebabize(str: string){
return ((value % size) + size) % size;
};

export { arrayBufferToBase64, extractBaseUrl, generateUrlPath, generateBlobHash, kebabize, wrapAround};
function escapeRegExp(string: string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}

export { arrayBufferToBase64, extractBaseUrl, generateUrlPath, generateBlobHash, kebabize, wrapAround, escapeRegExp};
1 change: 1 addition & 0 deletions versions.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"2.42.0": "0.12.0",
"2.41.1": "0.12.0",
"2.41.0": "0.12.0",
"2.40.0": "0.12.0",
Expand Down

0 comments on commit 92c5f91

Please sign in to comment.