Skip to content

Commit

Permalink
Merge pull request #16 from edrichhans/master
Browse files Browse the repository at this point in the history
  • Loading branch information
joethei committed Sep 9, 2023
2 parents 0c5997c + 62264df commit e21093e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 33 deletions.
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

0 comments on commit e21093e

Please sign in to comment.