diff --git a/README.md b/README.md index 4f85289..0e9a29b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Davaview Publisher +# Dataview Publisher ## Overview @@ -90,6 +90,7 @@ ${dv.markdownList(articles)} - [[Article1]] - [[Article2]] + %% DATAVIEW_PUBLISHER: end %% ```` @@ -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 @@ -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) diff --git a/src/dataview-publisher.ts b/src/dataview-publisher.ts index f7bcc95..173e50d 100644 --- a/src/dataview-publisher.ts +++ b/src/dataview-publisher.ts @@ -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; @@ -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, @@ -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); diff --git a/src/operations.ts b/src/operations.ts index ca914cc..a09be68 100644 --- a/src/operations.ts +++ b/src/operations.ts @@ -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); @@ -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); this.app.vault.process(tfile, () => updatedContent); } - private updateContnet(content: string, replacer: Replacer[]) { + private updateContent(content: string, replacer: Replacer[]) { return replacer.reduce( ( c: string,