Skip to content
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
2 changes: 1 addition & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
custom: https://www.buymeacoffee.com/decaf_dev
custom: https://ko-fi.com/decaf_dev
25 changes: 16 additions & 9 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,27 @@ jobs:
with:
bun-version: latest

- name: Install dependencies
run: bun install

- name: Fix tsconfig.json
run: |
sed -i '/"esModuleInterop"/d' node_modules/@tsconfig/svelte/tsconfig.json
sed -i '/"verbatimModuleSyntax"/d' node_modules/@tsconfig/svelte/tsconfig.json
sed -i '/"moduleResolution"/c\ "moduleResolution": "node",' node_modules/@tsconfig/svelte/tsconfig.json

- name: Build
id: build
run: |
bun install
bun run build
run: bun run build

# Create the release on GitHub
- name: Release
uses: softprops/action-gh-release@v1
if: startsWith(github.ref, 'refs/tags/')
with:
name: ${{ github.ref_name }}
tag_name: ${{ github.ref }}
files: |
dist/main.js
dist/manifest.json
dist/styles.css
name: ${{ github.ref_name }}
tag_name: ${{ github.ref }}
files: |
dist/main.js
dist/manifest.json
dist/styles.css
128 changes: 118 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ View count is an [Obsidian.md](https://obsidian.md) plugin for desktop and mobil

- [Installation](#installation)
- [Usage](#usage)
- [DataView](#dataview)
- [API](#api)
- [Settings](#settings)

## Installation

1. In Obsidian, open **Settings**
2. Go to **Community plugins**
3. Select **Browse**
4. Search for **View Count** by DecafDev
4. Search for **View Count** by **DecafDev**
5. Select **Install**
6. Then select **Enable**

Expand All @@ -29,13 +31,119 @@ Once you enable the plugin, view count data will start being tracked. You can se

There are 2 different definitions for a view count. Please see **View count type** setting section below.

If you would like to view the view count on mobile, you will need to enable the **Save view count to frontmatter** setting.
If you would like to view the view count on mobile, you will need to enable the [Sync view count to frontmatter](#settings) setting.

### Most viewed notes
### View count view

To see a list of the 50 most viewed notes in descending order, open the sidebar and click on the eye icon
By default, the plugin will add a view to the sidebar called **View count**. You may access this view by opening the sidebar and clicking on the eye icon.

![](/readme/list.png)
If the view is not open, you may run **Open view count view** from the command palette.

There are 2 different lists within the view count:

- A list of the 50 most viewed notes in your vault sorted in descending order. Click on the eye icon to see this list.

- A list of the 50 notes with the highest trending weight sorted in descending order. Click on the trending icon to see this list.

https://github.com/decaf-dev/obsidian-view-count/assets/40307803/1f97f34a-bc72-45c4-90ef-d71ab0ea6347

## DataView

You may dynamically query view count data using the [DataView plugin](https://obsidian.md/plugins?id=dataview)

### Example 1 - Query view count using DataView

If you have **Sync view count to frontmatter** enabled you may query the view count property from the frontmatter of each markdown note.

````markdown
```dataview
TABLE view-count AS "View Count" SORT view-count DESC LIMIT 10
```
````

Let's analyze this codeblock:

1. Render a table using the [Dataview Query Language](https://blacksmithgu.github.io/obsidian-dataview/queries/structure/)
2. Query the `view-count` property in each markdown note
3. Display that property in a column called "View Count"
4. Sort the results in descending order (highest to lowest)
5. Limit the results to 10 notes

### Example 2 - Query trending notes from the last 7 days using DataviewJS

````markdown
```dataviewjs
const plugin = this.app.plugins.plugins["view-count"];
const cache = plugin.viewCountCache;

const DURATION = "7-days";

dv.table(["Name", "Trending Weight"],
dv.pages().sort(p => cache.getTrendingWeight(p.file, DURATION), "desc")
.map(p => [p.file.name, cache.getTrendingWeight(p.file, DURATION)])
.slice(0,10)
);
```
````

Let's analyze this codeblock:

1. Render a table using the [DataviewJS](https://blacksmithgu.github.io/obsidian-dataview/api/intro/)
2. Display 2 columns in the table: "Name" and "Trending Weight"
3. Query all markdown files
4. Sort the files based on the trending weight in descending order (highest to lowest)
5. Format an array of data that includes object with just the file name and the trending weight
6. Limit the results to 10 notes

The duration can be updated with various values. See the [Duration options](#duration-options) section below.

## API

The view count plugin exposes an API that can be used to fetch the view count or trending weight for any file.

To start, you need to access the view count cache.

```javascript
//Get the view count plugin
const plugin = this.app.plugins.plugins["view-count"];

//Get the view count cache
const cache = plugin.viewCountCache;
```

Then you can use the cache to get a view count or trending weight.

```javascript
//Get the trending weight
const weight = cache.getTrendingWeight(file, duration);
console.log(weight);
//output: 22

//Get the view count
const viewCount = cache.getViewCount(file);
console.log(viewCount);
//output: 5
```

Here are the typescript definitions for these functions

```javascript
getViewCount: (file: TFile) => number;
getTrendingWeight: (file: TFile, duration: DurationFilter) => number;
```

### Duration options

The `getTrendingWeight` function accepts a duration string. Here are the following options:

| Duration | Description |
| ---------- | ------------------------------------- |
| `month` | The start of the month e.g. January 1 |
| `week` | The start of the week i.e. Sunday |
| `week-iso` | The start of the iso week i.e. Monday |
| `30-days` | The last 30 days |
| `14-days` | The last 14 days |
| `7-days` | the last 7 days |

## Settings

Expand All @@ -47,14 +155,14 @@ A unique day is considered an opening of a file after 12 am your local time.

**Excluded paths** - The folder paths that should be excluded from view count tracking. Please separate individual paths by commas. e.g. `folder1,folder2/inner`

**Save view count to frontmatter** - Save the view count to a frontmatter property in each note. This is useful if you want to query for the view count using the DataView plugin.
**Sync view count to frontmatter** - For each markdown note, save the current view count to a property in its frontmatter.

This setting makes view count available on mobile. In the future, this setting will not be needed for mobile viewing.
This setting makes view count available on mobile. In the future, this toggling this setting will not be needed for mobile.

The view count information for all files is stored in `.obsidian/view-count.json`. This setting is optional, as it duplicates data that already exists. However, it makes the view count more accessible for [DataView](https://github.com/blacksmithgu/obsidian-dataview) and other plugins because it is available in the frontmatter of the note.
The view count information for all files is stored in `.obsidian/view-count.json`. This setting is optional, as it duplicates data that already exists into the frontmatter of your markdown notes.

**View count property name** - The name of the property that the view will be stored in.

Please rename the existing property before updating this setting. You can use the rename option in the All Properties view in the sidebar to do this.
> Please rename the existing property before updating this setting. You can use the rename option in the `All Properties` view in the sidebar to do this.

**Templater Delay** - The delay in milliseconds before inserting view count frontmatter. Increase this value if you're using the Templater plugin and your template is being overwritten.
**Templater delay** - The delay in milliseconds before inserting view count frontmatter. Increase this value if you're using the Templater plugin and your template is being overwritten.
Binary file modified bun.lockb
Binary file not shown.
10 changes: 9 additions & 1 deletion esbuild.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import process from "process";
import builtins from "builtin-modules";
import fs from "fs";
import path from "path";
import esbuildSvelte from "esbuild-svelte";
import sveltePreprocess from "svelte-preprocess";

const banner = `/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
Expand Down Expand Up @@ -56,7 +58,13 @@ const context = await esbuild.context({
sourcemap: prod ? false : "inline",
treeShaking: true,
outfile: "dist/main.js",
plugins: [rebuildPlugin],
plugins: [
rebuildPlugin,
esbuildSvelte({
compilerOptions: { css: "external" },
preprocess: sveltePreprocess(),
}),
],
});

if (prod) {
Expand Down
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"id": "view-count",
"name": "View Count",
"version": "2.1.0",
"version": "2.2.0",
"minAppVersion": "1.4.0",
"description": "Tracks view count for each vault file.",
"author": "DecafDev",
"authorUrl": "https://github.com/decaf-dev",
"fundingUrl": "https://buymeacoffee.com/decaf_dev",
"fundingUrl": "https://ko-fi.com/decaf_dev",
"isDesktopOnly": false
}
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-view-count",
"version": "2.1.0",
"version": "2.2.0",
"description": "Tracks view count for each vault file",
"main": "main.js",
"scripts": {
Expand All @@ -12,18 +12,22 @@
"author": "DecafDev",
"license": "MIT",
"devDependencies": {
"@tsconfig/svelte": "^5.0.4",
"@types/lodash": "^4.17.0",
"@types/node": "^16.11.6",
"@typescript-eslint/eslint-plugin": "5.29.0",
"@typescript-eslint/parser": "5.29.0",
"builtin-modules": "3.3.0",
"esbuild": "0.17.3",
"esbuild-svelte": "^0.8.1",
"obsidian": "latest",
"svelte-preprocess": "^5.1.4",
"tslib": "2.4.0",
"typescript": "4.7.4"
},
"dependencies": {
"js-logger": "^1.6.1",
"lodash": "^4.17.21"
"lodash": "^4.17.21",
"svelte": "^4.2.17"
}
}
Binary file removed readme/list.png
Binary file not shown.
2 changes: 1 addition & 1 deletion src/event/event-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default class EventManager {
// Method to trigger all callbacks associated with an event
public emit(eventName: PluginEvent, ...data: any[]): void {
Logger.trace("EventManager emit");
Logger.debug("Emiting event", eventName);
Logger.debug("Emiting event", { event: eventName });
if (!this.eventListeners[eventName]) {
return;
}
Expand Down
Loading