From a2acae90a5b1574f2911b50192d3e9b6c0446b13 Mon Sep 17 00:00:00 2001 From: Hoang Nam Date: Sun, 18 Jan 2026 18:55:26 +0700 Subject: [PATCH] feat: Enhance task processing by removing code blocks from content analysis --- src/main.ts | 18 ++++++++++++++++-- src/utils/helpers.ts | 32 ++++++++++++++++++++++++++++---- src/views/ProgressBarView.ts | 7 +++++-- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/src/main.ts b/src/main.ts index 6bc9763..31cb2ee 100644 --- a/src/main.ts +++ b/src/main.ts @@ -469,15 +469,29 @@ export default class TaskProgressBarPlugin extends Plugin { async activateView(): Promise { const { workspace } = this.app; + // Wait for workspace to be ready + if (!workspace.layoutReady) { + workspace.onLayoutReady(() => this.activateView()); + return; + } + let leaf: WorkspaceLeaf | null = null; const leaves = workspace.getLeavesOfType("progress-tracker"); if (leaves.length > 0) { leaf = leaves[0]; } else { - leaf = workspace.getRightLeaf(false); - if (leaf) { + // Try to get right leaf, with fallback + const rightLeaf = workspace.getRightLeaf(false); + if (rightLeaf) { + leaf = rightLeaf; await leaf.setViewState({ type: "progress-tracker", active: true }); + } else { + // Fallback: create a new split in the right sidebar + leaf = workspace.getLeaf('split', 'vertical'); + if (leaf) { + await leaf.setViewState({ type: "progress-tracker", active: true }); + } } } diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index 3315e39..d9921c3 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -1,3 +1,18 @@ +/** + * Remove code blocks from content to avoid counting tasks inside them + * @param content - Content with potential code blocks + * @returns Content without code blocks + */ +export function removeCodeBlocks(content: string): string { + // Remove fenced code blocks (```) + let result = content.replace(/```[\s\S]*?```/g, ''); + + // Remove inline code blocks (`) + result = result.replace(/`[^`\n]+`/g, ''); + + return result; +} + /** * Safely escape regex special characters in a string * Used by multiple methods that need to create regex patterns @@ -74,8 +89,10 @@ export function validateContent( * @returns true if content contains tasks */ export function hasTasksInContent(content: string): boolean { + // Remove code blocks before checking for tasks + const contentWithoutCodeBlocks = removeCodeBlocks(content); const extendedTaskRegex = /- \[[^\]]*\]/i; - return extendedTaskRegex.test(content); + return extendedTaskRegex.test(contentWithoutCodeBlocks); } /** @@ -87,7 +104,10 @@ export function countTasksByCheckboxState(content: string): { [state: string]: number; } { const taskCounts: { [state: string]: number } = {}; - const lines = content.split("\n"); + + // Remove code blocks before counting tasks + const contentWithoutCodeBlocks = removeCodeBlocks(content); + const lines = contentWithoutCodeBlocks.split("\n"); for (const line of lines) { const match = line.trim().match(/^- \[([^\]]*)\]/); @@ -111,9 +131,13 @@ export function hasTaskContentChanged( oldContent: string, newContent: string ): boolean { + // Remove code blocks before comparing + const oldContentWithoutCodeBlocks = removeCodeBlocks(oldContent); + const newContentWithoutCodeBlocks = removeCodeBlocks(newContent); + // Split content into lines - const oldLines = oldContent.split("\n"); - const newLines = newContent.split("\n"); + const oldLines = oldContentWithoutCodeBlocks.split("\n"); + const newLines = newContentWithoutCodeBlocks.split("\n"); // Find task lines in both contents - support all checkbox states const oldTasks = oldLines.filter((line) => diff --git a/src/views/ProgressBarView.ts b/src/views/ProgressBarView.ts index 0efa9fe..fcdf0af 100644 --- a/src/views/ProgressBarView.ts +++ b/src/views/ProgressBarView.ts @@ -8,7 +8,7 @@ import { import { TaskProgressBarSettings } from "../interfaces/settings"; import { DataviewApi, KanbanBoard } from "../interfaces/types"; import { DebugLogger } from "../utils/logger"; -import { escapeRegExp, extractObsidianLinks, extractMarkdownLinks } from "../utils/helpers"; +import { escapeRegExp, extractObsidianLinks, extractMarkdownLinks, removeCodeBlocks } from "../utils/helpers"; /** * Progress bar view that displays task completion status in the sidebar @@ -1539,7 +1539,10 @@ export class TaskProgressBarView extends ItemView { */ private countTasksByCheckboxState(content: string): { [state: string]: number } { const taskCounts: { [state: string]: number } = {}; - const lines = content.split("\n"); + + // Remove code blocks before counting tasks + const contentWithoutCodeBlocks = removeCodeBlocks(content); + const lines = contentWithoutCodeBlocks.split("\n"); for (const line of lines) { const match = line.trim().match(/^- \[([^\]]*)\]/);