feat: Enhance task processing by removing code blocks from content analysis

This commit is contained in:
Hoang Nam 2026-01-18 18:55:26 +07:00
parent 3db00eae9b
commit a2acae90a5
3 changed files with 49 additions and 8 deletions

View file

@ -469,15 +469,29 @@ export default class TaskProgressBarPlugin extends Plugin {
async activateView(): Promise<void> {
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 });
}
}
}

View file

@ -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) =>

View file

@ -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(/^- \[([^\]]*)\]/);