Fix counter syntax to use empty parentheses

- Change syntax from ~ (number) to ~ ( )
- Counter now starts at 0 when using empty parentheses
- Update README with correct syntax examples

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-11-26 10:18:40 +01:00
parent 9295e8055a
commit ff1298c7d3
3 changed files with 10 additions and 9 deletions

View File

@@ -3,7 +3,8 @@
"allow": [ "allow": [
"Bash(git init:*)", "Bash(git init:*)",
"Bash(git add:*)", "Bash(git add:*)",
"Bash(git commit:*)" "Bash(git commit:*)",
"Bash(git remote add:*)"
], ],
"deny": [], "deny": [],
"ask": [] "ask": []

View File

@@ -7,7 +7,7 @@ A simple Obsidian plugin that creates interactive number counters with +/- butto
To create a counter, use the following syntax in your notes: To create a counter, use the following syntax in your notes:
``` ```
~ (0) counter label ~ ( ) counter label
``` ```
This will render as an interactive counter starting at 0, with minus and plus buttons to decrease or increase the value. This will render as an interactive counter starting at 0, with minus and plus buttons to decrease or increase the value.
@@ -15,12 +15,12 @@ This will render as an interactive counter starting at 0, with minus and plus bu
## Examples ## Examples
``` ```
~ (0) Tasks completed ~ ( ) Tasks completed
~ (5) Days streak ~ ( ) Days streak
~ (100) Points earned ~ ( ) Points earned
``` ```
Each counter is independent and maintains its own value in your markdown file. Each counter is independent and maintains its own value in your markdown file. When you click the +/- buttons, the value is updated in the source markdown.
## Installation ## Installation

View File

@@ -24,7 +24,7 @@ export default class CounterPlugin extends Plugin {
} }
processCounters(element: HTMLElement, context: MarkdownPostProcessorContext) { processCounters(element: HTMLElement, context: MarkdownPostProcessorContext) {
const counterRegex = /^~\s*\(\s*(\d+)\s*\)\s*(.*)$/; const counterRegex = /^~\s*\(\s*(\d*)\s*\)\s*(.*)$/;
const walker = document.createTreeWalker(element, NodeFilter.SHOW_TEXT); const walker = document.createTreeWalker(element, NodeFilter.SHOW_TEXT);
const nodesToReplace: Array<{ node: Node; parent: Node }> = []; const nodesToReplace: Array<{ node: Node; parent: Node }> = [];
@@ -50,7 +50,7 @@ export default class CounterPlugin extends Plugin {
const match = line.match(counterRegex); const match = line.match(counterRegex);
if (match) { if (match) {
const currentValue = parseInt(match[1], 10); const currentValue = match[1] === '' ? 0 : parseInt(match[1], 10);
const label = match[2].trim(); const label = match[2].trim();
const counterContainer = this.createCounterElement( const counterContainer = this.createCounterElement(
@@ -109,7 +109,7 @@ export default class CounterPlugin extends Plugin {
const editor = view.editor; const editor = view.editor;
const content = editor.getValue(); const content = editor.getValue();
const counterRegex = /^~\s*\(\s*\d+\s*\)\s*(.*)$/gm; const counterRegex = /^~\s*\(\s*\d*\s*\)\s*(.*)$/gm;
let matchIndex = 0; let matchIndex = 0;
const newContent = content.replace(counterRegex, (match, capturedLabel) => { const newContent = content.replace(counterRegex, (match, capturedLabel) => {