Skip to content

Commit

Permalink
Merge pull request #52 from udus122/feature/enable-to-use-file-in-eval
Browse files Browse the repository at this point in the history
  • Loading branch information
udus122 authored Nov 16, 2024
2 parents d6460d5 + 2d9f440 commit e55f223
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Davaview Publisher
# Dataview Publisher

## Overview

Expand Down Expand Up @@ -90,6 +90,7 @@ ${dv.markdownList(articles)}
- [[Article1]]
- [[Article2]]
%% DATAVIEW_PUBLISHER: end %%
````

Expand All @@ -98,6 +99,8 @@ Dataview JS is executed using the [eval() function](https://developer.mozilla.or
It doesn't render HTML, so it won't be output even if you use the [Dataview's Render functions](https://blacksmithgu.github.io/obsidian-dataview/api/code-reference/#render).
Please output a Markdown string as the last expression or value.

You can use [`DataviewAPI`](https://github.com/blacksmithgu/obsidian-dataview/blob/6d9030ef1df9c3f310f42e3502149dc71792dc4d/src/api/plugin-api.ts#L77) object as `dv` and [`TFile`](https://docs.obsidian.md/Reference/TypeScript+API/TFile) object of current file as `file`.

**Warning: It can be dangerous to execute arbitrary codes from untrusted sources. Only run codes that you understand, from trusted sources.**

### Settings
Expand All @@ -123,7 +126,7 @@ Please output a Markdown string as the last expression or value.

## Inspirations

This plugin highly inspirated by this article.
This plugin highly inspired by this article.

[Using Dataview on Obsidian Publish](https://joschua.io/posts/2023/09/01/obsidian-publish-dataview)

14 changes: 8 additions & 6 deletions src/dataview-publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,23 @@ export async function executeBlock(
if (
["dataviewjs", "javascript", "js"].some((x) => block.language.startsWith(x))
) {
// Define to access the current file object as file within eval
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const file = tfile;
const evalResult = eval(block.query);
return evalResult.trim();
}
// languageが指定されていない場合は、DQLとして実行する
const result = await executeQueryMarkdown(block.query, dv, tfile?.path);

const result = await executeQueryMarkdown(block.query, dv, tfile);
return result.trim();
}

export async function executeQueryMarkdown(
query: string,
dv: DataviewApi,
originFile?: string
originFile?: TFile
) {
const result = await dv.tryQueryMarkdown(query, originFile);
const result = await dv.tryQueryMarkdown(query, originFile?.path);
// ref. https://github.com/udus122/dataview-publisher/issues/41#issuecomment-2208610505
const snitizedResult = result.replaceAll("\\\\|", "\\|");
return snitizedResult;
Expand All @@ -105,7 +107,7 @@ export function parseBlock(block: string): BlockInfo {
const startBlock = extractStartBlock(block);
const { language, query } = extractMarkdownCodeBlock(startBlock);

const output = extractoutput(block);
const output = extractOutput(block);

return {
content: block,
Expand Down Expand Up @@ -155,7 +157,7 @@ export function extractMarkdownCodeBlock(text: string) {
return { language, query };
}

export function extractoutput(text: string) {
export function extractOutput(text: string) {
// NOTE: initialize regex to reset lastIndex
const regex = new RegExp(BLOCK_REGEX.source);

Expand Down
7 changes: 3 additions & 4 deletions src/operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class Operator {
return;
}

const updatedContent = this.updateContnet(content, replacer);
const updatedContent = this.updateContent(content, replacer);

editor.setValue(updatedContent);
editor.setCursor(cursor);
Expand Down Expand Up @@ -68,13 +68,12 @@ export class Operator {
private async updateDataviewPublisherOutput(tfile: TFile) {
const content = await this.app.vault.cachedRead(tfile);

const replacer = await createReplacerFromContent(content, this.dv, tfile);
const updatedContent = this.updateContnet(content, replacer);
const updatedContent = this.updateContent(content, replacer);

Check failure on line 71 in src/operations.ts

View workflow job for this annotation

GitHub Actions / tagpr

Cannot find name 'replacer'.

this.app.vault.process(tfile, () => updatedContent);
}

private updateContnet(content: string, replacer: Replacer[]) {
private updateContent(content: string, replacer: Replacer[]) {
return replacer.reduce(
(
c: string,
Expand Down

0 comments on commit e55f223

Please sign in to comment.