From 69f5cf2e483b5eb808373ef190b0ac91c28e2e0d Mon Sep 17 00:00:00 2001 From: Wray Zheng Date: Fri, 2 Aug 2024 18:30:31 +0800 Subject: [PATCH 1/4] fix: heading regex pattern in getEndOfSection --- src/formatters/helpers/getEndOfSection.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/formatters/helpers/getEndOfSection.ts b/src/formatters/helpers/getEndOfSection.ts index 3e3401e..99dfe8c 100644 --- a/src/formatters/helpers/getEndOfSection.ts +++ b/src/formatters/helpers/getEndOfSection.ts @@ -12,7 +12,7 @@ function getMarkdownHeadings(bodyLines: string[]): Heading[] { const headers: Heading[] = []; bodyLines.forEach((line, index) => { - const match = line.match(/^(#+)[\s]?(.*)$/); + const match = line.match(/^(#+\s)[\s]?(.*)$/); if (!match) return; From 159887f5ccfdd5d7eb50800f89165e048c927f41 Mon Sep 17 00:00:00 2001 From: Wray Zheng Date: Fri, 2 Aug 2024 18:46:55 +0800 Subject: [PATCH 2/4] test: add test case for section containing leading tag --- .../helpers/getEndOfSection.test.ts | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/formatters/helpers/getEndOfSection.test.ts b/src/formatters/helpers/getEndOfSection.test.ts index 2e8ee45..77bf861 100644 --- a/src/formatters/helpers/getEndOfSection.test.ts +++ b/src/formatters/helpers/getEndOfSection.test.ts @@ -184,6 +184,26 @@ test("getEndOfSection - target is heading, should not consider subsections", () expect(result).toBe(5); }); +test("getEndOfSection - capture to end of section with a leading tag, should not consider subsections", () => { + const lines = [ + "# Notes", + "", + "## Topic A", // target (2) + "content a1", + "#TagForA1", + "content a2", // result (5) + "## Topic B", + "content b1", + "", + "", + ]; + + const targetLine = 2; + + const result = getEndOfSection(lines, targetLine, false); + expect(result).toBe(5); +}); + test("getEndOfSection - target is heading, should consider subsections", () => { const lines = [ "# Notes", // target (0) From 7c55eb82c98426f7249130ba935bb8bc89a7e971 Mon Sep 17 00:00:00 2001 From: Christian Bager Bach Houmann Date: Sun, 3 Nov 2024 10:06:57 +0100 Subject: [PATCH 3/4] test: add test casts for getting markdown headings --- .../helpers/getEndOfSection.test.ts | 57 ++++++++++++++++++- src/formatters/helpers/getEndOfSection.ts | 2 +- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/src/formatters/helpers/getEndOfSection.test.ts b/src/formatters/helpers/getEndOfSection.test.ts index 77bf861..1fe0a7a 100644 --- a/src/formatters/helpers/getEndOfSection.test.ts +++ b/src/formatters/helpers/getEndOfSection.test.ts @@ -1,5 +1,5 @@ import { test, expect } from "vitest"; -import getEndOfSection from "./getEndOfSection"; +import getEndOfSection, { getMarkdownHeadings } from "./getEndOfSection"; test("getEndOfSection - find the end of a section", () => { const lines = [ @@ -310,3 +310,58 @@ test("getEndOfSection - capture to last line, shouldConsiderSubsections OFF", () const result = getEndOfSection(lines, targetLine, false); expect(result).toBe(2); }); + + +test("getMarkdownHeadings - correctly identifies headings", () => { + const lines = [ + "# Heading 1", + "## Heading 2", + "### Heading 3", + "#### Heading 4", + "##### Heading 5", + "###### Heading 6", + "Normal text", + "#Not a heading", + "# Heading with #hash in text", + "##Invalid heading", + "", + " # Heading with leading spaces", + ]; + + const result = getMarkdownHeadings(lines); + + expect(result).toEqual([ + { level: 1, text: "Heading 1", line: 0 }, + { level: 2, text: "Heading 2", line: 1 }, + { level: 3, text: "Heading 3", line: 2 }, + { level: 4, text: "Heading 4", line: 3 }, + { level: 5, text: "Heading 5", line: 4 }, + { level: 6, text: "Heading 6", line: 5 }, + { level: 1, text: "Heading with #hash in text", line: 8 }, + ]); +}); + +test("getMarkdownHeadings - handles empty input", () => { + const lines: string[] = []; + + const result = getMarkdownHeadings(lines); + + expect(result).toEqual([]); +}); + +test("getMarkdownHeadings - correctly ignores Obsidian tags", () => { + const lines = [ + "# Real Heading", + "#tag", + "#anothertag", + "Text with #inline_tag", + "## Heading with #tag in it", + ]; + + const result = getMarkdownHeadings(lines); + + expect(result).toEqual([ + { level: 1, text: "Real Heading", line: 0 }, + { level: 2, text: "Heading with #tag in it", line: 4 }, + ]); +}); diff --git a/src/formatters/helpers/getEndOfSection.ts b/src/formatters/helpers/getEndOfSection.ts index 99dfe8c..124a3b8 100644 --- a/src/formatters/helpers/getEndOfSection.ts +++ b/src/formatters/helpers/getEndOfSection.ts @@ -8,7 +8,7 @@ function isSameHeading(heading1: Heading, heading2: Heading): boolean { return heading1.line === heading2.line; } -function getMarkdownHeadings(bodyLines: string[]): Heading[] { +export function getMarkdownHeadings(bodyLines: string[]): Heading[] { const headers: Heading[] = []; bodyLines.forEach((line, index) => { From 99bc7ef08b0ad443d41f2aba197719ad0b6ed329 Mon Sep 17 00:00:00 2001 From: Christian Bager Bach Houmann Date: Sun, 3 Nov 2024 10:07:27 +0100 Subject: [PATCH 4/4] fix: modify heading regex pattern to change optional capture of whitespace to match one or more --- src/formatters/helpers/getEndOfSection.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/formatters/helpers/getEndOfSection.ts b/src/formatters/helpers/getEndOfSection.ts index 124a3b8..1005c34 100644 --- a/src/formatters/helpers/getEndOfSection.ts +++ b/src/formatters/helpers/getEndOfSection.ts @@ -12,7 +12,7 @@ export function getMarkdownHeadings(bodyLines: string[]): Heading[] { const headers: Heading[] = []; bodyLines.forEach((line, index) => { - const match = line.match(/^(#+\s)[\s]?(.*)$/); + const match = line.match(/^(#+)[\s]+(.*)$/); if (!match) return;