From fcd809221e2a0fd32ca18e467729d4845edc9f0b Mon Sep 17 00:00:00 2001 From: Edrich Chua Date: Thu, 17 Aug 2023 23:02:44 +0800 Subject: [PATCH 1/4] feat: word cloud query file --- src/wordcloud.ts | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/src/wordcloud.ts b/src/wordcloud.ts index 7ccd258..042c080 100644 --- a/src/wordcloud.ts +++ b/src/wordcloud.ts @@ -1,5 +1,6 @@ -import TagCloudPlugin from "./main"; +import TagCloudPlugin, {logger} from "./main"; import {MarkdownPostProcessorContext, TFile} from "obsidian"; +import {getAPI} from "obsidian-dataview"; import {convertToMap, getWords, recordToArray, removeStopwords} from "./functions"; import WordCloud from "wordcloud"; @@ -50,8 +51,34 @@ export class Wordcloud { } } if (options.source === 'query') { - el.createEl("p", {cls: "cloud-error"}).setText("Queries are not supported in a wordcloud"); - return; + const dataviewAPI = getAPI(); + if (dataviewAPI === undefined) { + el.createEl("p", {cls: "cloud-error"}).setText("Dataview is not installed, but is required to use queries"); + return; + } + + try { + const query = options.query; + if(!query) { + el.createEl('p', {cls: "cloud-error"}).setText("query option is required"); + return; + } + + const page = dataviewAPI.page(query); + + if(!page) { + el.createEl('p', {cls: "cloud-error"}).setText("Page not found"); + return; + } + + const rawContent = await dataviewAPI.io.load(page.file.path) + const parsedWords = await getWords(rawContent) + content = await convertToMap(parsedWords); + } catch (error: any) { // eslint-disable-line @typescript-eslint/no-explicit-any + el.createEl('p', {cls: "cloud-error"}).setText(error.toString()); + logger.error(error); + return; + } } /*this part is really not performant, need to find a better solution. if(options.source === 'query') { From 87400a63f863c92587b4d64a4a884789fdcb3541 Mon Sep 17 00:00:00 2001 From: Edrich Chua Date: Thu, 17 Aug 2023 23:03:16 +0800 Subject: [PATCH 2/4] fix: better sanitation of markdown words --- src/functions.ts | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/src/functions.ts b/src/functions.ts index f4c4a98..f3b72a3 100644 --- a/src/functions.ts +++ b/src/functions.ts @@ -10,7 +10,7 @@ export function removeMarkdown(text: string): string { .replace(/\[\[(.*(?=\|))(.*)\]\]/g, '$2') //wikilinks with alias .replace(/\[\[([\s\S]*?)\]\]/gm, '$1') //wikilinks .replace(/- ?\[.?\]/gm, '') //tasks - .replace(/%%.*?%%/gm, '')//Obsidian Comments + .replace(/%%(.|\n)*?%%/gm, '')//Obsidian Comments .replace(/`([\s\S]*?)`/gm, '') //codeblocks, inline & normal .replace(/\[\^[[\s\S]]*\]/g, '') //footnotes .replace(/\^\[([\s\S]*?)\]/g, '$1') //inline footnotes @@ -36,20 +36,13 @@ export function removeStopwords(words: Record, customStopwords: } export async function getWords(text: string): Promise { - const words = text.split(/[\n\s]/g); - const output: string[] = []; - for (let word of words) { - const result = removeMarkdown(word).toLocaleLowerCase(); - if(result.length > 0) { - output.push(result); - } - } - return output; + const cleanText = removeMarkdown(text).toLocaleLowerCase(); + return cleanText.split(/[\n\s]/g); } export async function convertToMap(words: string[]): Promise> { const record: Record = {}; - for (let word of words) { + for (const word of words) { const element = record[word]; if(element) { record[word] = element + 1; From 004555b4fd4f2f7bcce60d61a1610aff3d2e3699 Mon Sep 17 00:00:00 2001 From: Edrich Chua Date: Fri, 18 Aug 2023 18:32:55 +0800 Subject: [PATCH 3/4] feat: include stopwords option --- src/wordcloud.ts | 29 ++++++----------------------- 1 file changed, 6 insertions(+), 23 deletions(-) diff --git a/src/wordcloud.ts b/src/wordcloud.ts index 042c080..d702517 100644 --- a/src/wordcloud.ts +++ b/src/wordcloud.ts @@ -74,35 +74,18 @@ export class Wordcloud { const rawContent = await dataviewAPI.io.load(page.file.path) const parsedWords = await getWords(rawContent) content = await convertToMap(parsedWords); + + if(options.stopwords) { + const tmp = this.plugin.settings.stopwords.split("\n"); + const customStopwords = new Set(tmp); + content = removeStopwords(content, customStopwords) + } } catch (error: any) { // eslint-disable-line @typescript-eslint/no-explicit-any el.createEl('p', {cls: "cloud-error"}).setText(error.toString()); logger.error(error); return; } } - /*this part is really not performant, need to find a better solution. - if(options.source === 'query') { - const dataviewAPI = getAPI(); - if(dataviewAPI === undefined) { - el.createEl("p").setText("Dataview is not installed, but is required to use queries"); - return; - } - - const pages = dataviewAPI.pages(options.query, ctx.sourcePath); - const words : string[] = []; - for (let page of pages) { - const file = this.app.vault.getAbstractFileByPath(page.file.path); - if (file === undefined) return; - if (!(file instanceof TFile)) return; - const fileContent = await this.app.vault.read(file); - words.push(...this.getWords(fileContent)); - } - if(options.stopwords) { - content = this.convertToMap(this.removeStopwords(words)); - }else { - content = this.convertToMap(words); - } - }*/ el.empty(); From 62264df424aab37421bbf6384226b62102b9cbb2 Mon Sep 17 00:00:00 2001 From: Edrich Chua Date: Fri, 18 Aug 2023 18:35:36 +0800 Subject: [PATCH 4/4] chore: readme for query wordcloud --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 0939b37..90f8844 100644 --- a/README.md +++ b/README.md @@ -104,6 +104,15 @@ source: file ``` ~~~ +#### Show all words using a folder/file + +~~~markdown +```wordcloud +source: file +query: 'Folder/File' +``` +~~~ + ### Options | **Name** | **Description** | **Possible Values** | **Default** |