Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow query for word cloud #16

Merged
merged 4 commits into from
Sep 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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** |
Expand Down
15 changes: 4 additions & 11 deletions src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -36,20 +36,13 @@ export function removeStopwords(words: Record<string, number>, customStopwords:
}

export async function getWords(text: string): Promise<string[]> {
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<Record<string, number>> {
const record: Record<string, number> = {};
for (let word of words) {
for (const word of words) {
const element = record[word];
if(element) {
record[word] = element + 1;
Expand Down
54 changes: 32 additions & 22 deletions src/wordcloud.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -50,32 +51,41 @@ export class Wordcloud {
}
}
if (options.source === 'query') {
el.createEl("p", {cls: "cloud-error"}).setText("Queries are not supported in a wordcloud");
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");
if (dataviewAPI === undefined) {
el.createEl("p", {cls: "cloud-error"}).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);
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);

if(options.stopwords) {
const tmp = this.plugin.settings.stopwords.split("\n");
const customStopwords = new Set<string>(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;
}
}*/
}

el.empty();

Expand Down